Push the button goto next function. etc..

Hey all, I'm looking into how to make "goto commands"? kind of like push the button goto sweep. Push the button again, goto strobe and so on.
Below I've got some of the code (trying) using if.. then goto. but it keeps telling me "LED_sweep was not declared in this scope"
Whats this mean?

 const int SW1 = 2;     // the number of the pushbutton pin
 // variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {                
 
  pinMode(SW1, INPUT);  
  pinMode(12, OUTPUT);     
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
}



void loop() {
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(SW1);
  
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
Butons:
{
  if (digitalRead (SW1) == HIGH)  {led_sweep;}
}




led_sweep:
  {
  digitalWrite (12,HIGH);
  digitalWrite (9, HIGH);
  delay (350);
  
  digitalWrite (11,HIGH);
  digitalWrite (8, HIGH);
  delay (350);
  
  digitalWrite (10,HIGH);
  digitalWrite (7, HIGH);
  delay (350);
  
  if (digitalRead (SW1) == HIGH)  {led_strobe;}
}
  
  
led_strobe:
  {
  digitalWrite (12, HIGH);
  digitalWrite (11, HIGH);
  digitalWrite (10, HIGH);
  delay (50);
  
  digitalWrite (12, LOW);
  digitalWrite (11, LOW);
  digitalWrite (10, LOW);
  delay (50);
  
  digitalWrite (12, HIGH);
  digitalWrite (11, HIGH);
  digitalWrite (10, HIGH);
  delay (50);
  
   digitalWrite (12, LOW);
  digitalWrite (11, LOW);
  digitalWrite (10, LOW);
  delay (50);
  
  digitalWrite (7, HIGH);
  digitalWrite (8, HIGH);
  digitalWrite (9, HIGH);
  delay (50);
  
  digitalWrite (7, LOW);
  digitalWrite (8, LOW);
  digitalWrite (9, LOW);
  delay (50);
  
  digitalWrite (7, HIGH);
  digitalWrite (8, HIGH);
  digitalWrite (9, HIGH);
  delay (50);
  
  
}

Call a function instead:

if (digitalRead (SW1) == HIGH)  {led_sweep[glow]()[/glow];}

...

[glow]void led_sweep()[/glow]
{
  digitalWrite (12,HIGH);
void loop() {
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(SW1);
  
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
 //Butons:
{
  if (digitalRead (SW1) == HIGH)  {led_sweep;}
}

}

You were missing a closing curly brace too. And you didn't comment out the 'buttons' comment

There has been a long time since I've done this, but I just sat down with your code, scratched my head and thought about how I would do it.

This is the result:

/*
  THIS CODE IS ABSOLUTELY NOT TESTED
  it does compile
  it might do nothing and anything it does do is not my responsibility
  (if it gains consciousness or an evolving AI I might want some credit though)
*/
//http://www.arduino.cc/playground/Code/Button
#include <Button.h> 
//http://www.arduino.cc/playground/Code/LED
#include <LED.h> 
//http://www.arduino.cc/playground/Code/FiniteStateMachine
#include <FiniteStateMachine.h> 
//http://www.arduino.cc/playground/Code/TimedAction
#include <TimedAction.h>

/*
  This button will control the state of our program
*/
const int SW1 = 2;// the number of the pushbutton pin
Button sw1 = Button(SW1,PULLDOWN); //the button object for reading the button

//define our LEDs here
LED led1a = LED(12);
LED led2a = LED(11);
LED led3a = LED(10);
LED led1b = LED(9);
LED led2b = LED(8);
LED led3b = LED(7);

const int NUMBER_OF_STATES = 2;
//define our states
State states[NUMBER_OF_STATES] = {
  State(ledSweepStateUpdate),
  State(ledStrobeStateUpdate)
};
State noop = State(nooperation);

int currentState = 0;

//define our state machine
FSM stateMachine = FSM(noop);

//define our timed actions
//these are used so that tou don't have to use delay()
//this will be great when you want this program to do more than just blink six leds
TimedAction ledSweepTimer = TimedAction(350,ledSweep); //update the sweep every 350ms
TimedAction ledStrobeTimer = TimedAction(50,ledStrobe);//update the strobe every 50ms


void setup() {                
  //the libraries takes care of the setup for us
  //you can add setup for your other stuff here
}

void loop() {
  if (sw1.uniquePress()) { //true once for each press
      stateMachine.transitionTo(states[currentState]);
      currentState++; //increment to next state
    if (currentState>=NUMBER_OF_STATES) { 
      currentState = 0;
    }
  }
  stateMachine.update(); //update the state machine, it's the heart of this program  
}

//used by the state machine for updating the sweep state
void ledSweepStateUpdate() {
  ledSweepTimer.check(); //check to see if it is time to trigger the ledSweep
}

//called by ledSweepTimer
//do not use delay in this function
void ledSweep()
{
  static int currentPhase = 0;
  
  //shut all LEDs off to see the effect
  led1a.off(); led2a.off(); led3a.off();
  led1b.off(); led2b.off(); led3b.off();
  
  //sweep through the LEDs according to currentPhase
  switch (currentPhase) {
    case 0:
      led1a.on();
      led1b.on();
    break;
    case 1:
      led2a.on();
      led2b.on();
    break;
    case 2:
      led3a.on();
      led3b.on();
      currentPhase = 0; //reset the phase at the end of the sweep
    break;
    //you could add steps here if you want
  }
  currentPhase++; //increment the phase
}

//used by the state machine for updating the strobe state
void ledStrobeStateUpdate() {
  ledStrobeTimer.check(); //check to see if it is time to trigger the ledSweep
}  
  
//called by ledStrobeTimer
//do not use delay in this function
void ledStrobe()
{
  static int currentPhase = 0;
  
  //shut all LEDs off to see the effect
  led1a.off(); led2a.off(); led3a.off();
  led1b.off(); led2b.off(); led3b.off();
  
  //sweep through the LEDs according to currentPhase
  //you could add functions for each of these steps if they seem similar or become very long
  switch (currentPhase) {
    case 0:
      led1a.on();
      led2a.on();
      led3a.on();
      break;
    case 1:
      led1a.off();
      led2a.off();
      led3a.off();
      break;
    case 2:
      led1a.on();
      led2a.on();
      led3a.on();
      break;
    case 3:
      led1a.off();
      led2a.off();
      led3a.off();
      break;
    case 4:
      led1b.on();
      led2b.on();
      led3b.on();
      break;
    case 5:
      led1b.off();
      led2b.off();
      led3b.off();
      break;
    case 6:
      led1b.on();
      led2b.on();
      led3b.on();
      break;
    case 7:
      led1b.off();
      led2b.off();
      led3b.off();
      currentPhase = 0; //reset the phase at the end of the strobe
    break;
    //you could add steps here if you want
  }
  currentPhase++; //increment the phase
}

void nooperation() {/*the state machine has nothing to do, do something else with this time*/}

That is probably over engineered, but it's one way to do it (maybe).

Do not hesitate to ask! :slight_smile:

Well, it is always fun to pull the leg of a seasoned BASIC programmer....

What he was missing in the first place was "goto".

I myself do not consider "goto" harmful when used in style and ESPECIALLY to make FSA algorithms more readable!!!