Different Modes and Arduino?

Hello!

I had a question. 

I want to have like 5 buttons and every time I press one button it runs a certain block of code over and over until I press another button. This way I can have different modes etc. mode 1, mode 2. . .
Can somebody help me with the code please?

Thanks

sort by: active | newest | oldest
You code will have the typical Void setup and void loop. Then you will have different voids for each mode. Like void mode1 and so on. In the void setup create a tracking variable. In the void loop check the button press. If button 1 is pressed store 1 to the tracking variable. If button 2 is pressed store 2 to the tracking variable and so on. Now you set up several if statements. If the tracking variable equals 1 then mode1. Which will send the sketch to the mode1 section. It then goes back to the void loop after running the LED flashes set up in void mode1. It will then see no other button has been pressed and that the tracking variable still equals 1 and goes back to mode1. Now when you press another button that button number will be stored to the tracking variable and point to the appropriate mode you want to run.
HavocRC (author)  mpilchfamily4 years ago
Thank you very much! I was going to ask you for some code but after deciding upon using my noggin I was able to type up this! It works great! Now I just have to figure out how to add delays and still have it read the button input :( Any advice?
int button1 = A1;
int button2 = A2;

int mode1;
int mode2;

int mode;

int redLED = 13;
int blueLED = 12;

void setup()
{
  pinMode(redLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  mode1 = digitalRead(button1);
  mode2 = digitalRead(button2);
  
  
  if (mode1 == HIGH) { mode = 1; }
  if (mode2 == HIGH) { mode = 2; }
   
  if (mode == 1)
    {
      digitalWrite(blueLED, LOW);
      digitalWrite(redLED, HIGH);
    }
    
  if (mode == 2)
    {
      digitalWrite(redLED, LOW);
      digitalWrite(blueLED, HIGH);      
    }
   
      
      
  Serial.println(mode);

  
}

you can use this code but it won't works if you use a delay because then you have to press the button at the right time aka after the code of the mode ended !!!

Not quite what i meant. More like this...

If (mode1 = = HIGH) {
mode1;
}

void mode1()
{
digitalWrite(blueLED, LOW);
digitalWrite(redLED, HIGH);
}

Think outside the typical void loop(). You are able to make calls to sections outside the void loop(). When that section is don't running it goes back to where the program left off in the void loop(). Have a look at my Simon Says instructable. Very little is happening in the void loop() there and most of it is running between other sets. The void loop gives you the intro music then calls to the input. the game pretty much runs from the void input() and calls to the void fail() if you loose which does it's thing then takes you back to the void loop() to start the game again. 

Working on a better bit of code for you to start with. Will post a reply when it's ready. 
I have not tested this code on the arduino yet but it does compile and should work as advertised.

What i've done is created some for loops to simplify the coding a bit. As you can see in the main loop it first calls to the buttoncecker() loop. Which looks for a button to be pressed, figures out what button that is and assigns a value to the mode tracker. When that is complete it picks back up at the main loop and checks what mode was assigned. It then directs the code to the proper mode which will flash the LEDs as you would like. i've created examples in mode1 and mode2. As you can see i have 2 additional calls to buttonchecker in the middle of the modes so button presses can be detected and a new mode set during the LED flashes. When the mode has run through once its back to the top of the main loop to check button press again and assign the mode. It may seem a bit complex but allows you to be flexible in your coding of the LED flashes. 

int redLED = 13;
int blueLED = 12;
boolean button[]= {2,3,4,5,6}; //pin assignment for buttons

boolean buttonstate = 0;
int mode = 0;

void setup()
{
  for(int x=0; x<5; x++) //starts a loop to set the pin modes for all button pins
  {
    pinMode(button[x], INPUT);
  }
  for(int x=0; x<5; x++) //starts a loop to set the buttons to a default of HIGH
  {
    pinMode(button[x], HIGH);
  }
  pinMode(redLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
}
 
  void loop()
  {
    buttoncheck(); //calls to the void buttoncheck() section for checking the buttons
    if(mode == 1)
    {
      mode1(); //directs teh program to teh section with the flashes you selected
    }
    if(mode == 2)
    {
      mode2(); //directs teh program to teh section with the flashes you selected
    }
    if(mode == 3)
    {
      mode3(); //directs teh program to teh section with the flashes you selected
    }
    if(mode == 4)
    {
      mode4(); //directs teh program to teh section with the flashes you selected
    }
    if(mode == 5)
    {
      mode5(); //directs teh program to teh section with the flashes you selected
    }   
  }
 
  void buttoncheck()
  {
    for(int x=0; x<5; x++) //loop for checking all the buttons
    {
      buttonstate = digitalRead(button[x]);
      if(buttonstate == LOW && button[x] == 2) //number equals the buttoon the pin is connected too
      {
        mode = 1; // number equals the mode you want to run
      }
      if(buttonstate == LOW && button[x] == 3)
      {
        mode = 2;
      }
      if(buttonstate == LOW && button[x] == 4)
      {
        mode = 3;
      }
      if(buttonstate == LOW && button[x] == 5)
      {
        mode = 4;
      }
      if(buttonstate == LOW && button[x] == 6)
      {
        mode = 5;
      }
    }
  }
 
  void mode1()
  {
    //flash your LEDs how you want in here for this mode
    //a call to buttoncheck can be added in anywhere you want
    //so that the program can continue to check for a button
    //press even while it's in the middle of the mode.
    //When a button is pressed it stores what mode it needs to
    //go to next and will move to that mode once the sequence is over.
    buttoncheck;
    digitalWrite(blueLED, LOW);   
    digitalWrite(redLED, HIGH);
    delay (500);
    buttoncheck;
    digitalWrite(blueLED, HIGH);
    digitalWrite(redLED, LOW);   
  }
 
  void mode2()
  {
    //flash your LEDs how you want in here for this mode
    //a call to buttoncheck can be added in anywhere you want
    //so that the program can continue to check for a button
    //press even while it's in the middle of the mode.
    //When a button is pressed it stores what mode it needs to
    //go to next and will move to that mode once the sequence is over.
    buttoncheck;
    digitalWrite(blueLED, LOW);   
    digitalWrite(redLED, HIGH);
    delay (500);
    buttoncheck;
    digitalWrite(redLED, LOW);      
  }
 
void mode3()
  {
    //flash your LEDs how you want in here for this mode
    //a call to buttoncheck can be added in anywhere you want
    //so that the program can continue to check for a button
    //press even while it's in the middle of the mode.
    //When a button is pressed it stores what mode it needs to
    //go to next and will move to that mode once the sequence is over.        
  }
 
  void mode4()
  {
    //flash your LEDs how you want in here for this mode
    //a call to buttoncheck can be added in anywhere you want
    //so that the program can continue to check for a button
    //press even while it's in the middle of the mode.
    //When a button is pressed it stores what mode it needs to
    //go to next and will move to that mode once the sequence is over.       
  }
 
  void mode5()
  {
    //flash your LEDs how you want in here for this mode
    //a call to buttoncheck can be added in anywhere you want
    //so that the program can continue to check for a button
    //press even while it's in the middle of the mode.
    //When a button is pressed it stores what mode it needs to
    //go to next and will move to that mode once the sequence is over.          
  }

HavocRC (author)  mpilchfamily4 years ago
Oh my. . .

Ok thank you VERY much! I'm going to have to work through this.
What you are trying to do is something called a "state machine", use that as a search topic, and you should find good resources.
caitlinsdad4 years ago
HavocRC (author)  caitlinsdad4 years ago
Yeah I looked at that but it doesn't look like it would keep running a code until I hit another button. This is what I want

Mode 1:
LED on
delay(100);
LED off
Different LED on
delay
LED off

and to just keep running that until I go to Mode 2:
led blink etc.

Is that what you can do with switch and case?