I have some code that runs a sequence of events. It waits for a signal on A0 then runs the events at specific times based on millis(). This all works fine with my basic code for a few events. I have a variable that allows each event to only happen once then resets the variables and waits for then next trigger. In the final code I might need 300 events. Is there a better way of allowing each event to only happen once or will I end up with 300 check variables and 300 'if' statements?
unsigned long timesincestart; //how long since the start
unsigned long time; // general time variable
unsigned long starttime; // when the start was triggered
int starter =0;
int cyclestarted =0;
int event1=0;
int event2=0;
int event3=0;
void setup() {
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop(){
if (cyclestarted == 0){
starter = analogRead(A0);
if (starter > 500 ){
cyclestarted=1;
starttime = millis(); //get time now
}
}
if (cyclestarted == 1){
time = millis();
timesincestart = time - starttime;
if (timesincestart > 30000 && event1==0){
Serial.println("event1");
event1=1; //don't run this again
}
if (timesincestart > 35000 && event2==0){
Serial.println("event2");
event2=1; //don't run this again
}
if (timesincestart > 38000 && event3==0){
Serial.println("event3");
event3=1; //don't run this again
}
if (timesincestart > 60000){
Serial.println("finished");
cyclestarted=0;
event1=0;
event2=0;
event3=0;
}
}
}