Any idea why the no go on this simple three button/three LED FSM?

Simple three button, three led FSM. Weird part is the serial monitor is showing the "green mode" on the blue button press.

Any ideas why no button press will illuminate and flash any LED?

Code looks okay, but again, I'm just starting to understand the finer points of FSM.

int red = 11;
int green = 10;
int blue = 9;
int redButton = 6;
int greenButton = 5;
int blueButton = 3;
int redState;
int greenState;
int blueState;
int state;
unsigned long startTime;

void setup()
{
  Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(redButton, INPUT_PULLUP);
  pinMode(greenButton, INPUT_PULLUP);
  pinMode(blueButton, INPUT_PULLUP);
  digitalWrite(red,LOW);
  digitalWrite(green,LOW);
  digitalWrite(blue,LOW);
}

void loop()
{
  int redButtonState = digitalRead(redButton);
  {
    if (redButtonState)
    {
      if (redButtonState != redState)
      {
        state=0;
        startTime = millis();
      }
    }
  }
  int greenButtonState = digitalRead(greenButton);
  {
    if (greenButtonState)
    {
      if (greenButtonState != greenState)
      {
        state=1;
        startTime = millis();
      }
    }
  }
  int blueButtonState = digitalRead(blueButton);
  {
    if (blueButtonState)
    {
      if (blueButtonState != blueState)
      {
        state=2;
        startTime = millis();
      }
    }
  }
  if (state == 0)
  {
    redFunction();
  }
  else if (state == 1)
  {
    greenFunction();
  }
  else if (state == 2)
  {
    blueFunction();
  }
}

void redFunction()
{
  Serial.println("Red Mode");
  digitalWrite(green,LOW);
  digitalWrite(blue,LOW);
  if (millis() - startTime >=1000UL)
  {
    digitalWrite(red, !digitalRead(red));
    startTime = millis();
  }
}
void greenFunction()
{
  Serial.println("Green Mode");
  digitalWrite(red,LOW);
  digitalWrite(blue,LOW);
  if (millis() - startTime >=1000UL)
  {
    digitalWrite(green, !digitalRead(green));
    startTime = millis();
  }
}
void blueFunction()
{
  Serial.println("Blue Mode");
  digitalWrite(red,LOW);
  digitalWrite(green,LOW);
  if (millis() - startTime >=1000UL)
  {
    digitalWrite(blue, !digitalRead(blue));
    startTime = millis();
  }
}

you are not recording the current state after the test in your state changes

void loop()
{
  int redButtonState = digitalRead(redButton);
  {
    if (redButtonState)
    {
      if (redButtonState != redState)
      {
        state=0;
        startTime = millis();
      }
    }
  }
  redState = redButtonState;//<<<<<<<<<<<<<<<<<<<<<<<<<
  int greenButtonState = digitalRead(greenButton);
  {
    if (greenButtonState)
    {
      if (greenButtonState != greenState)
      {
        state=1;
        startTime = millis();
      }
    }
  }
  greenState = greenButtonState;//<<<<<<<<<<<<<<<<<<<<<<<<<
  int blueButtonState = digitalRead(blueButton);
  {
    if (blueButtonState)
    {
      if (blueButtonState != blueState)
      {
        state=2;
        startTime = millis();
      }
    }
  }
  blueState = blueButtonState;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  if (state == 0)
  {
    redFunction();
  }
  else if (state == 1)
  {
    greenFunction();
  }
  else if (state == 2)
  {
    blueFunction();
  }
}

BulldogLowell:
you are not recording the current state after the test in your state changes

void loop()

{
  int redButtonState = digitalRead(redButton);
  {
    if (redButtonState)
    {
      if (redButtonState != redState)
      {
        state=0;
        startTime = millis();
      }
    }
  }
  redState = redButtonState;//<<<<<<<<<<<<<<<<<<<<<<<<<
  int greenButtonState = digitalRead(greenButton);
  {
    if (greenButtonState)
    {
      if (greenButtonState != greenState)
      {
        state=1;
        startTime = millis();
      }
    }
  }
  greenState = greenButtonState;//<<<<<<<<<<<<<<<<<<<<<<<<<
  int blueButtonState = digitalRead(blueButton);
  {
    if (blueButtonState)
    {
      if (blueButtonState != blueState)
      {
        state=2;
        startTime = millis();
      }
    }
  }
  blueState = blueButtonState;//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  if (state == 0)
  {
    redFunction();
  }
  else if (state == 1)
  {
    greenFunction();
  }
  else if (state == 2)
  {
    blueFunction();
  }
}

Hey Lowell, i’m looking at some of the ButtonStateChange figures to see how to record what the last state was where you had //<<<<<<<<<<< in the code. I’m getting something like (redState != redButtonState) Does that sound correct?

If i’m understanding that correctly its saying redState isnt equal to red button state… So that records the state?

no, look at your code… those three lines I added and highlighted with <<<<<<<<<<<<<<<<<<<<<<<<

BulldogLowell:
no, look at your code… those three lines I added and highlighted with <<<<<<<<<<<<<<<<<<<<<<<<

Awesome! I missed it. Im still not sure why I needed to record the state when my traffic light FSM didn’t have that coded in. Could it be because there is more than two states?

Also why cant you give a state a letter instead of a number?

mgttrottier:
Also why cant you give a state a letter instead of a number?

You can. Character constants are also integer constants.

However, an enum is a better choice than either.