FSM question

In the following code how do I get the SM running with a HIGH input to pin 8? It will run(blink) if I start it in state 2, But I can't seem to get it to start with the button push.

Thanks

Tom

[quote]
[color=#CC6600]int[/color] inPin = 8;   [color=#7E7E7E]// pushbutton connected to digital pin 8[/color]

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{
  [color=#CC6600]pinMode[/color](13,[color=#006699]OUTPUT[/color]);
  [color=#CC6600]pinMode[/color](inPin,[color=#006699]INPUT[/color]);
}
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
{
  [color=#CC6600]static[/color] [color=#CC6600]int[/color] state = 1; [color=#7E7E7E]// initial state is 1, the "idle" state.[/color]
  [color=#CC6600]static[/color] [color=#CC6600]unsigned[/color] [color=#CC6600]long[/color] ts;  [color=#7E7E7E]// To store the "current" time in for delays.[/color]
  [color=#CC6600]switch[/color](state)
  {
    [color=#CC6600]case[/color] 1:
       [color=#7E7E7E]//???[/color]
      [color=#CC6600]break[/color];
    [color=#CC6600]case[/color] 2:
      [color=#CC6600]digitalWrite[/color](13,[color=#006699]HIGH[/color]);  [color=#7E7E7E]// Turn on the light[/color]
      ts = [color=#CC6600]millis[/color]();  [color=#7E7E7E]// Remember the current time[/color]
      state = 3;  [color=#7E7E7E]// Move to the next state[/color]
      [color=#CC6600]break[/color];
    [color=#CC6600]case[/color] 3:
      [color=#7E7E7E]// If one second has passed, then move on to the next state.[/color]
      [color=#CC6600]if[/color]([color=#CC6600]millis[/color]() > ts + 1000)
      {
        state = 4;
      }
      [color=#CC6600]break[/color];
    [color=#CC6600]case[/color] 4:
      [color=#CC6600]digitalWrite[/color](13,[color=#006699]LOW[/color]);  [color=#7E7E7E]// Turn off the light[/color]
      ts = [color=#CC6600]millis[/color]();  [color=#7E7E7E]// Remember the current time[/color]
      state = 5;
      [color=#CC6600]break[/color];
    [color=#CC6600]case[/color] 5:
      [color=#7E7E7E]// If one second has passed, then go back to state 2.[/color]
      [color=#CC6600]if[/color]([color=#CC6600]millis[/color]() > ts + 1000)
      {
        state = 2;
      }
      [color=#CC6600]break[/color];
    [color=#CC6600]case[/color] 6:
      [color=#7E7E7E]// We only get here when forced from outside.[/color]
      [color=#CC6600]digitalWrite[/color](13,[color=#006699]LOW[/color]);  [color=#7E7E7E]// Turn off the light[/color]
      state = 1;  [color=#7E7E7E]// Return to the "idle" state.[/color]
      [color=#CC6600]break[/color];
    [color=#CC6600]default[/color]:
      state = 1;
      [color=#CC6600]break[/color];
  }
 
}

[/quote]

Does your code really look like that? Try again.

Oops! Noob :blush:

int inPin = 8;   // pushbutton connected to digital pin 8

void setup()
{
  pinMode(13,OUTPUT);
  pinMode(inPin,INPUT);
}
void loop()
{
  static int state = 1; // initial state is 1, the "idle" state.
  static unsigned long ts;  // To store the "current" time in for delays.
  switch(state)
  {
    case 1:
       //???
      break;
    case 2:
      digitalWrite(13,HIGH);  // Turn on the light
      ts = millis();  // Remember the current time
      state = 3;  // Move to the next state
      break;
    case 3:
      // If one second has passed, then move on to the next state.
      if(millis() > ts + 1000)
      {
        state = 4;
      }
      break;
    case 4:
      digitalWrite(13,LOW);  // Turn off the light
      ts = millis();  // Remember the current time
      state = 5;
      break;
    case 5:
      // If one second has passed, then go back to state 2.
      if(millis() > ts + 1000)
      {
        state = 2;
      }
      break;
    case 6:
      // We only get here when forced from outside.
      digitalWrite(13,LOW);  // Turn off the light
      state = 1;  // Return to the "idle" state.
      break;
    default:
      state = 1;
      break;
  }

}

But I can't seem to get it to start with the button push.

I don't see you reading a switch state.

I tried a bunch of stuff but nothing worked, Thought this would?

(digitalRead(8,HIGH)) { state = 2; }

I don't have my head wraped around this yet, thanks for you patients

Tom

tesart: In the following code how do I get the SM running with a HIGH input to pin 8? It will run(blink) if I start it in state 2, But I can't seem to get it to start with the button push.

Have you connected a switch to pin 8, with a pull-up or pull-down resister?

If so, is seems to me that in state 1 you would need to read the state of pin8 and if it indicates the switch has been operated then change to state 2.

Please give your states meaningful names, by the way.

Also, this code is poor practice:

if(millis() > ts + 1000)

In order to cope correctly with timer rollover, it should be coded as:

if(millis() - ts > 1000)

I tried a bunch of stuff but nothing worked, Thought this would?

(digitalRead(8,HIGH)) { state = 2; }

I don't have my head wraped around this yet, thanks for you patients

It's missing the if statement, for one thing. For another, the digitalRead() function does not take two arguments.

if(digitalRead(8) == HIGH)