5293Views20Replies

Author Options:

Different Modes and Arduino? Answered

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

Discussions

You could possible make a principle code and use the IF statement for example-

void setup(){

enter all constants, variables and stuff here (for all the codes)

}

void loop(){

IF (condition button press 1){

your code

}

ELSE IF (condition button press 2){

your code

}

and so on

the only disadvantage i see is that it takes too much space, but it is possible

Hello,

I need to make a project but my zero knowledge in programming does not allow me to go far.. For someone with the knowledge and experience may be a code for children :)

My code needs to have three different modes just to activate two relays in a different way at each mode.

Can anyone help please?? I will send a brief description of what I need.

Hi,

So, let me get this straight:

You want three modes(inputs) to control two relays. Right?

I can help. Let me know a little more about the input mode. Is it a pushbutton? Also what output(Result) you want for each input.

Hi,

Thanks a lot for offering to help me on that. I can send you a brief description via email if you like to take a full view off what I need.

Alright go ahead.

You can send me the details through personal message on my instructables account.

Hi Anshu,

Any news? Do you think that you can figure out something of this?

Hi,

Sorry that setup turned out to be lot complicated (for me)

I can write a program to perform a simpler function.

The main obstacle is that the Arduino needs to store some data on which button was pressed earlier.

Apart from this everything is cool.

I can code for a setup that turns on or off the relays and even display the outputs and inputs on the LCD.

But, nothing that complex. Sorry! :(

First of all I am really appreciate your time.

What I have seen so far is that many use multiple void () functions or what they call them. So 3 different void(), one for each mode. And then you call the in void loop() or in a main void(). And I think a counter function to count the times that the button has pressed.

Why Arduino has to know what button has pressed earlier? I am asking because after you select and finish a mode it has to go back in the beginning and can select any mode you want, even the same as previews.

Something like this:

void loop() {

if (){ //Condition for A

outA();

}

if (){ //Condition for B

outB();

}

if (){ //Condition for C

outC();

}

}



void outA(){

//Output of A

}

void outB(){

//Output of B

}

void outC(){

//Output of C

}

THIS IS JUST AN EXAMPLE ACTUAL CODE MAY VARRY!

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.

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;

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

voidloop()
{
  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.          
  }

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.

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?