/*
*
Forum: https://forum.arduino.cc/t/variable-types-used-in-state-machine/1405843
Wokwi: https://wokwi.com/projects/441640275104091137
Changed the declaration of
time_limit[]
base_time_increment
base_time_offset
from int (back) to unsigned long
ec2021
Pins 3,4 & 5 connected to "+" leg of 3 LEDs with 330ohm resistor connecting "-" to Gnd
a push button connects A0 to Ground to determine led flash direction
*
*/
// using 4 place arrays and only using the 1-3 positions for loops
// that are more readable
unsigned long time_limit[4];
unsigned long base_time_increment = 150; // increment of time between LEDs
unsigned long base_time_offset = 200; // time added to all of the time_limits
int left_or_right_button; // decides signal direction R=1, L=0
int lastDirection; // last way the LEDs are going "now"
int lastLEDStatus[] = {0,1,1,1}; // on/off status of LEDs
unsigned long overall_time_limit; // overall cycle time
unsigned long currentMillis;
unsigned long previousMillis[] = {0,0,0,0};
const uint8_t analogPins[] = {A0, A3, A4, A5}; // analog pin names for the 3 LEDs
void setup() {
Serial.begin(115200);
Serial.println(" state_testing_turn_signal_on_the_file_changing.ino ");
// setup A0 to determine direction of LED strobe
pinMode(A0,INPUT_PULLUP); // if NOT grounded - Right
left_or_right_button = digitalRead(A0); // get initial state of button
lastDirection = left_or_right_button; // remember last direction to see if button state
// changes in future
// setup 3 pins as output to control LEDs and turn on LEDs based on initial lastLEDStatus
for (int count = 1; count < 4; count++) {
pinMode(analogPins[count], OUTPUT);
digitalWrite(analogPins[count], lastLEDStatus[count]); // initialize the state of the LEDs
}
// initialize time_limit
for (int count = 1; count < 4; count++) {
time_limit[count] = base_time_increment * count + base_time_offset;
Serial.print(count); Serial.print(" "); Serial.println(time_limit[count]);
}
// initialize overall_time_limit
overall_time_limit = time_limit[3] * 2; // overall cycle time
Serial.println(overall_time_limit);
} // end of setup()
void loop() {
left_or_right_button = digitalRead(A0); // get current status of button
if (left_or_right_button != lastDirection) { // is button status different from last pass thru loop
for (int pass = 1; pass < 4; pass++ ) { // reset previousMillis & lastLEDStatus
previousMillis[pass] = 0;
lastLEDStatus[pass] = 0;
}
lastDirection = left_or_right_button;
}
currentMillis = millis();
for (int pass = 1; pass < 4; pass++ ) { // trigger pass for each LED
is_it_time(pass);
}
} // end of loop()
void is_it_time(int whichLED) { // state machine which decides to turn on/off LEDs
if (lastLEDStatus[whichLED] == left_or_right_button) { // LED is on, check to see it it should be turned off
if (currentMillis - previousMillis[whichLED] > time_limit[whichLED]) {
// previousMillis[whichLED] = millis();
previousMillis[whichLED] = currentMillis;
toggle_led(whichLED);
}
}
else { // LED is off, check to see if it should be turned on
if (currentMillis - previousMillis[whichLED] > overall_time_limit - time_limit[whichLED]) {
previousMillis[whichLED] = millis();
previousMillis[whichLED] = currentMillis;
toggle_led(whichLED);
}
}
} // end of is_it_time()
void toggle_led(int whichLEDx){ // if the LED is on, turn off, if it is off, turn on
if (lastLEDStatus[whichLEDx] == 1) {
lastLEDStatus[whichLEDx] = 0;
digitalWrite(analogPins[whichLEDx],lastLEDStatus[whichLEDx]);
}
else {
lastLEDStatus[whichLEDx] = 1;
digitalWrite(analogPins[whichLEDx],lastLEDStatus[whichLEDx]);
}
} // end of toggle_led()