Hello people!
I am new here, so I hope with this post I will do everything correct and be within the guidelines for the forum.
I would need a little bit of guidance for my project I guess. I am trying to synchronize four systems truly randomized (at least fairly, as requirement) all using interrupts. The code was first written and used for Arduino so there might be still also some things that need to be changed, as for example a random register that helps with the entropy of the randomness.
What I am stuck with, is the stability. Sometimes the output just freezes for a few seconds. Also I am trying to implement a watchdog which I don't really touch yet before the rest is not in an acceptable state.
The code looks like this:
Any help is highly appreciated
I am new here, so I hope with this post I will do everything correct and be within the guidelines for the forum.
I would need a little bit of guidance for my project I guess. I am trying to synchronize four systems truly randomized (at least fairly, as requirement) all using interrupts. The code was first written and used for Arduino so there might be still also some things that need to be changed, as for example a random register that helps with the entropy of the randomness.
What I am stuck with, is the stability. Sometimes the output just freezes for a few seconds. Also I am trying to implement a watchdog which I don't really touch yet before the rest is not in an acceptable state.
The code looks like this:
Code:
// Random seed includes
#define randomSeed(s) srandom(s)
volatile uint32_t seed; // These two variables can be reused in your program after the
volatile int8_t nrot; // function CreateTrulyRandomSeed()executes in the setup() function
// define output pins
// The LED_BUILTIN pin is helpful as an indicator that the program is running
#define N_PINS 3
const int pins[N_PINS] = { 13, 14, LED_BUILTIN };
// define baud rate
int bd = 9600;
// define interrupt timer
IntervalTimer dogTimer;
IntervalTimer onTimer;
IntervalTimer randomOffTimer;
IntervalTimer projectionTimer;
volatile int randomDelay = 0;
int tElapsed = 0;
//define pulse parameters
#define INTERVAL_MIN 1000 //define minimum interval length [ms]
#define INTERVAL_MAX 1000 //define maximum interval length [ms]
#define PULSE_LEN 1000 //define the pulse length [ms]
volatile bool LEDstate = 0;
volatile int counter = 0;
void CreateTrulyRandomSeed() {
seed = 0;
nrot = 32; // Must be at least 4, but more increased the uniformity of the produced
// seeds entropy.
while (nrot > 0)
; // wait here until seed is created
}
void kickDog() {
noInterrupts();
WDOG_REFRESH = 0xA602;
WDOG_REFRESH = 0xB480;
interrupts();
// Serial.println("KICK");
}
void on() {
tElapsed = elapsedMillis();
//switch LED to HIGH
LEDstate = 1;
//change the signal to on (high, 3.3V)
for (int i = 0; i < N_PINS; i++) {
digitalWrite(pins[i], LEDstate); //turn the pins on (high, 3.3V)
}
//finding random delay for LED LOW
randomDelay = random(INTERVAL_MIN, INTERVAL_MAX);
tElapsed = elapsedMillis() - tElapsed;
randomOffTimer.begin(randomOff, PULSE_LEN * 1000 - tElapsed);
onTimer.begin(on, (PULSE_LEN + randomDelay) * 1000 - tElapsed);
// Serial.println("ON");
}
void randomOff() {
//switch LED to LOW
LEDstate = 0;
//change the signal to off (low, 0V)
for (int i = 0; i < N_PINS; i++) {
digitalWrite(pins[i], LOW); //turn the pins off (low, 0V)
}
// Serial.println("OFF");
}
//function called by the interrupt sending the signal through USB port
void sendSerial60() {
if (nrot < 0) {
counter++;
noInterrupts();
if (LEDstate)
Serial.print("1");
else
Serial.print("0");
Serial.print("#");
interrupts();
Serial.println(counter);
}
nrot--;
seed = seed << 8;
// seed = seed ^ TCNT1L; #############################################################################################################################
}
void setup() {
//start Serial connection
Serial.begin(bd);
//define interrupt function to refesh the watchdog
dogTimer.begin(kickDog, (INTERVAL_MAX + PULSE_LEN + 1) * 1000);
dogTimer.priority(70);
//define interrupt function and interrupt timing for the pulse length
onTimer.begin(on, PULSE_LEN * 1000);
onTimer.priority(100);
//define random interrupt function and interrupt timing for the interval pulse length
// randomOffTimer.begin(randomOff, PULSE_LEN * 1000);
randomOffTimer.priority(100); //setting timer priority higher than average priority and higher than projectionTimer
//define projection interrupt function and interrupt timing 59Hz -> 16949.15254us (projectors @59Hz, but listener on other side sampling at 60Hz -> 16666.66667)
projectionTimer.begin(sendSerial60, 16666.66667); //59.81: 16719.61211, 60: 166666.6667, 120: 8333.3333333
projectionTimer.priority(128); //setting timer priority to mid level 0-255, lower priorty than randomTimer
for (int i = 0; i < N_PINS; i++) {
pinMode(pins[i], OUTPUT);
digitalWrite(pins[i], LOW);
}
// WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
// WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
// // delayMicroseconds(1);
// WDOG_STCTRLH |= WDOG_STCTRLH_WAITEN | WDOG_STCTRLH_STOPEN | WDOG_STCTRLH_ALLOWUPDATE | WDOG_STCTRLH_CLKSRC | WDOG_STCTRLH_WDOGEN; // WDOG_STCTRLH_WINEN | (3rd from right)
// WDOG_TOVALL = (INTERVAL_MAX + PULSE_LEN) * 1.1;
// WDOG_TOVALH = 0;
// WDOG_PRESC = 0;
// Serial.println("WDOG SET");
// Serial.println(WDOG_STCTRLH_CLKSRC);
CreateTrulyRandomSeed();
randomSeed(seed);
}
void loop() {
}
Any help is highly appreciated