hello everyone
my goal is to run a code that would respond to inputs by initiating a function , but while performing the function still "listen" to inputs to see if another function should initiate .
for example , i receive a 16 bit input , 4 bits are saying i should run the reward function and the 12 remaining imply the amount of time a certain pin should remain HIGH .
so far i used delay for that time , but its occupying the program while i need it to be attentive to new assignments coming in , for example another 16 bit input with a different function to operate .
i looked online and encountered the millis() for multitasking but not sure how to use it . this is what i got so far
do you think it can work ? i can't test it yet cause the other functions are not ready yet , but i need to know if this is a problem i'm capable of overcoming . i couldn't find anywhere a proper explanation about order of events in the software . i mean there is the void loop and the intervalTimer that are both supposed to run infintely , how does it work?
my goal is to run a code that would respond to inputs by initiating a function , but while performing the function still "listen" to inputs to see if another function should initiate .
for example , i receive a 16 bit input , 4 bits are saying i should run the reward function and the 12 remaining imply the amount of time a certain pin should remain HIGH .
so far i used delay for that time , but its occupying the program while i need it to be attentive to new assignments coming in , for example another 16 bit input with a different function to operate .
i looked online and encountered the millis() for multitasking but not sure how to use it . this is what i got so far
Code:
// PTA (12 13) = [3 4] // ready signal and XS2
// PTB (0 1 2 3) = [16 17 19 18] // For address bits
// PTC (0 1 2 3 4 5 6 7 8 9 10 11) = [15 22 23 9 10 13 11 12 35 36 37 38] // For digital inputs from NI cards
// PTD (0 1 2 3 4 5 6 7 ) = [2 14 7 8 6 20 21 5] // For digital output to marker
// PTE (25) = [34] // for digital output to reward
byte inputPins[] = {3, 4, 16, 17, 19, 18, 15, 22, 23, 9, 10, 13, 11, 12, 35, 36, 37, 38};
byte outputPIns[] = {2, 14, 7, 8, 6, 20, 21, 5, 34};
unsigned long previousMillis = 0;
IntervalTimer myTimerOne;
void reward(void) {
if (!digitalReadFast(3) && ((GPIOB_PDIR & 0xF) == 4)){// Received command
Serial.println("test");
Serial.println(GPIOC_PDIR,HEX);
digitalWriteFast(34, HIGH);//reward
previousMillis = millis(); //start counting until stop
}
}
void setup() {
Serial.begin(9600); while(!Serial);
pinMode(34,OUTPUT);
for (int pinnum = 0; pinnum < 18; pinnum++) {
pinMode(inputPins[pinnum], INPUT_PULLDOWN);
//pinMode(inputPins[pinnum], INPUT);
}
for (int pinnum = 0; pinnum < 9; pinnum++) {
pinMode(outputPIns[pinnum], OUTPUT);
}
myTimerOne.priority(0);
myTimerOne.begin(reward,10);
myTimerOne.priority(0);
}
void loop() {
unsigned long currentMillis=millis();
if ( digitalReadFast(34) && currentMillis - previousMillis > GPIOC_PDIR) {
digitalWriteFast(34, LOW);//reward
}
}
do you think it can work ? i can't test it yet cause the other functions are not ready yet , but i need to know if this is a problem i'm capable of overcoming . i couldn't find anywhere a proper explanation about order of events in the software . i mean there is the void loop and the intervalTimer that are both supposed to run infintely , how does it work?