I am trying to eliminate several extra hardware features as I figure out more programming. I had been using a 555 timer circuit to run my clock pin, but after reading the tutorial on blink without delay, I have successfully "copied" enough of the code in the example to get a get a 1-sec toggle code in place.
I am struggling a bit on implementing a for loop to handle a "penalty" code that would basically use the exact code with the only difference being the Interval is smaller, so the clock will toggle faster. My trouble is coming because I only want the "penalty" code to run for 1 sec (Interval value should produce 5 rapid toggles in the 1 sec.)
I have only been able to find for loop examples where an int variable is used to intialize and then the for loop is ran for so many "cycles". I need to have it run for a set amount of time.
I have attempted to accomplish this below. I have not included an initiation or increment (I thought the learning section mentioned they could be left out). Please let me know if/what I have missed.
The first portion of the code is the 1-sec toggle I have tested. It works great. Starts counting after both PB's are pushed.
if (easyMode == 1 && startMode == 1){
//start the 1 sec toggle after both easy and start PB's are pressed
unsigned long currentTimerMillis = millis();
if(currentTimerMillis - previousTimerMillis > timerInterval) { // timerInterval is 500
// save the last time you changed the timer
previousTimerMillis = currentTimerMillis;
// if the timer is off turn it on and vice-versa:
if (timerState == LOW)
timerState = HIGH;
else
timerState = LOW;
// set the timer with the timerState of the variable:
digitalWrite(timer, timerState);
}
}
ldr1Read = analogRead(ldr1);
if (ldr1Read > 900){
digitalWrite(siren, HIGH);
delay(20);
digitalWrite(siren, LOW);
unsigned long penStart = millis();
for (; millis() - penStart < 1000;){
unsigned long currentTimerMillis = millis();
if(currentTimerMillis - previousTimerMillis > penaltyInterval) { //penaltyInterval is 100
// save the last time you changed the timer
previousTimerMillis = currentTimerMillis;
// if the timer is off turn it on and vice-versa:
if (timerState == LOW)
timerState = HIGH;
else
timerState = LOW;
// set the timer with the timerState of the variable:
digitalWrite(timer, timerState);
}
}
}