Button for Mode Select

I see everyone do it in youtube videos... What I want to do is have a mode select button. When you press it, it goes to mode 2, press again for mode 3, ect... and after the last omde, it returns to mode 1. How do I do this?

I know how to wire a button to arduino. I dont know what code I need... Please guide me through this... I know that you guys are all good at it...

http://cprogramminglanguage.net/c-increment-operators.aspx

What you want to do is increment or decrement the variable then test the variable with something like select to do what you want. If the variable gets past your limit you reset it to a known default.

Look at the examples switchCase for how select works.

IF is a simple operation as well

The youtube videos should have a link to the code. In general the Arduino IDE uses C language though with some hand holding and predefined functions.

I recommend reading the examples built in and learning why they work. :slight_smile:

haven't checked thhis thread in a while... i dont know how to write this... like how do i do the mode counter? and then how do i reset it back to 1?

int counter = 0;

void setup(){}

void loop()
{
    if(counter < 100)
    {
        counter++;
    }
    else counter = 0;
}

so where would the code go?

    if(counter < 100)
    {
          // if counter is less than 100 do something
        counter++; //advance the counter by 1
    }
    else counter = 0; // else ignore and reset counter

or

    if(counter < 100)  counter++; //advance the counter by 1
    else 
    { 
         // if counter is greater than 100 do something
         counter = 0; // reset the counter
    }

so i could change if counter < 100 to say 5 or whatever amount of mored i want?

and then how whould i cycle the modes with a button?

you can use that variable besides a loop

ie

if button is pressed counter = 5

if counter == 5 do this

its all the same if type statements, if something is happening set this variable, if this variable equals "whatever" do this sequence

here is what i know how to write... i don't know how to read a momentary pushbutton though... also, I don't know how to make the button cycle the modes... ignore the led definitions... theyre just for my modes...

int counter = 0;

int buttonPin = 13;
int LED1 = 12;
int LED2 = 11;
int LED3 = 10;

void setup()
{
 pinMode(buttonPin, INPUT);
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);
}

void loop()
{
 digitalRead(buttonPin); 
}

ok, i wrote some more code, and it compiled... i can't test if it works, though...

int counter = 0;

int buttonPin = 13;
int LED1 = 12;
int LED2 = 11;
int LED3 = 10;

void setup()
{
 pinMode(buttonPin, INPUT);
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);
}

void loop()
{
 {
  digitalRead(buttonPin);
   if(buttonPin = HIGH){
    counter+1; 
   }
   else{}
 }
 {
  if(counter == 1){
   digitalWrite(LED1, HIGH);
   delay(250);
   digitalWrite(LED1, LOW);
   digitalWrite(LED2, HIGH);
   delay(250);
   digitalWrite(LED2, LOW);
   digitalWrite(LED3, HIGH);
   delay(250);
   digitalWrite(LED3, LOW);
   delay(250);
  }
  else{}

  if(counter == 2){
   digitalWrite(LED1, HIGH);
   delay(100);
   digitalWrite(LED1, LOW);
   digitalWrite(LED2, HIGH);
   delay(100);
   digitalWrite(LED2, LOW);
   digitalWrite(LED3, HIGH);
   delay(100);
   digitalWrite(LED3, LOW);
   delay(100);
  }
  else{}  
 }  
}

I think you have your if, else if statements setup wrong.

It should be like this:

if(<condition>)
{
}
else if(<another condition)
{
}
else //This will happen if neither of the two previous conditions are true
{
}

Also you don't 'need' the final else. You can stop after any of the } brackets.

i changed it, and it still compiles... i never knew about else if... thanks!

int counter = 0;

int buttonPin = 13;
int LED1 = 12;
int LED2 = 11;
int LED3 = 10;

void setup()
{
 pinMode(buttonPin, INPUT);
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);
}

void loop()
{
 {
  digitalRead(buttonPin);
   if(buttonPin = HIGH){
    counter+1; 
   }
   else{}
 }
 {
  if(counter == 1)
  {
   digitalWrite(LED1, HIGH);
   delay(250);
   digitalWrite(LED1, LOW);
   digitalWrite(LED2, HIGH);
   delay(250);
   digitalWrite(LED2, LOW);
   digitalWrite(LED3, HIGH);
   delay(250);
   digitalWrite(LED3, LOW);
   delay(250);
  }

  else if(counter == 2)
  {
   digitalWrite(LED1, HIGH);
   delay(100);
   digitalWrite(LED1, LOW);
   digitalWrite(LED2, HIGH);
   delay(100);
   digitalWrite(LED2, LOW);
   digitalWrite(LED3, HIGH);
   delay(100);
   digitalWrite(LED3, LOW);
   delay(100);
  }
  else if(counter == 3)
  {
   digitalWrite(LED1, HIGH);
   delay(50);
   digitalWrite(LED1, LOW);
   digitalWrite(LED2, HIGH);
   delay(50);
   digitalWrite(LED2, LOW);
   digitalWrite(LED3, HIGH);
   delay(50);
   digitalWrite(LED3, LOW);
   delay(50);
  }  
 }  
}

I modified your code a bit to simplify it a bit. I also noticed that you had some extra brackets {} that you didn't need.

Notice that I used another variable for delayTime so that you don't have to repeat the digitalWrites for each mode.

int counter = 1;

int buttonPin = 13;
int LED1 = 12;
int LED2 = 11;
int LED3 = 10;

int delayTime;

void setup()
{
 pinMode(buttonPin, INPUT);
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);
}

void loop()
{
  //Handle input
  digitalRead(buttonPin);
  if(buttonPin = HIGH)
  {
    counter + 1;
    //Reset count if over max mode number
    if(counter == 4)
    {
      counter = 1;
    }
  }
  
  //Change mode
  if(counter == 1)
  {
    delayTime = 250;
  }
  else if(counter == 2)
  {
    delayTime = 100;
  }
  else if(counter == 3)
  {
    delayTime = 50;
  }
  
  //Light show!
  digitalWrite(LED1, HIGH);
  delay(delayTime);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, HIGH);
  delay(delayTime);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, HIGH);
  delay(delayTime);
  digitalWrite(LED3, LOW);
  delay(delayTime);
}

very nice, thatk you! i never even thought about that delayTime and then just jsing that variable... thank you much!

First wanted to say thanks for all the input on this thread as it's exactly what I was wanting to do with a mode select switch and some LED patterns. That said, I'm having troubles getting mine to work correctly and was wondering if someone could offer some input on the code:

int counter = 1;

int buttonPin = 13;
int LEDB = 11;
int LEDG = 10;
int LEDR = 9;


void setup()
{
 pinMode(buttonPin, INPUT);
 pinMode(LEDR, OUTPUT);
 pinMode(LEDG, OUTPUT);
 pinMode(LEDB, OUTPUT);
}

void loop()
{
  //Handle input
  digitalRead(buttonPin);
  if(buttonPin = HIGH)
  {
    counter + 1;
    //Reset count if over max mode number
    if(counter == 8)
    {
      counter = 1;
    }
  }

  
  //Change mode
  if(counter == 2)
  {
    analogWrite(LEDR, 255);
  }
  
  else if(counter == 3)
  {
    analogWrite(LEDR, 127);
  }
  
  else if(counter == 4)
  {
    analogWrite(LEDB, 255);
  }
  
  else if(counter == 5)
  {
    analogWrite(LEDB, 127);
  }
  
  else if(counter == 6)
  {
    analogWrite(LEDR, 255);
    analogWrite(LEDG, 255);
    analogWrite(LEDB, 255);
  }
    
  else if(counter == 7)
  { 
    analogWrite(LEDR, 127);
    analogWrite(LEDG, 127);
    analogWrite(LEDB, 127);
  }
}

I'm using a QT100 as the input for the switch if that makes any difference (which i don't think it should since it outputs a high (3vdc in this case) to pin 13 when touched.

Any help would be greatly appreciated.

    counter + 1;

Syntactically correct, but pointless. counter++; if you actually want to affect the counter variable.

  digitalRead(buttonPin);
  if(buttonPin = HIGH)
  {

The digitalRead function returns the state of the pin, which you then discard.

Next, you assign the value of HIGH to the pin number. The result of that assignment will be the value assigned, which, being non-zero, is considered true, so the if block will be executed.

You probably want == in there, to compare the values. And the value you want to compare is not the pin number, but the value read from the pin (that you didn't save).

The whole if/else if/else if... mess shoudl be replaced by a switch statement. Much shorter, much cleaner, and much easier to understand.

Thanks for the quick reply Paul.
Ok, so hopefully I understood what you are telling me and I looked at the examples in the included sketch files for switch statements...hopefully this is correct :-[

int counter = 0;

int buttonPin = 13;
int LEDB = 11;
int LEDG = 10;
int LEDR = 9;


void setup()
{
 pinMode(buttonPin, INPUT);
 pinMode(LEDR, OUTPUT);
 pinMode(LEDG, OUTPUT);
 pinMode(LEDB, OUTPUT);
}

void loop() {
  //Handle input
    if(buttonPin == HIGH)
  {
    counter ++;
    //Reset count if over max mode number
    if(counter == 7)
    {
      counter = 0;
    }
  }

  
  //Change mode
  switch (counter) {
  case 1:
    analogWrite(LEDR, 255);
    break;
  case 2:
    analogWrite(LEDR, 127);
    break;
  case 3:
    analogWrite(LEDB, 255);
    break;
  case 4:
    analogWrite(LEDB, 127);
    break;
  case 5:
    analogWrite(LEDR, 255);
    analogWrite(LEDG, 255);
    analogWrite(LEDB, 255);
    break;
  case 6:  
    analogWrite(LEDR, 127);
    analogWrite(LEDG, 127);
    analogWrite(LEDB, 127);
    break;
 }
}

You've assigned a value to button pin, here:

int buttonPin = 13;

Then, you have this:

if(buttonPin == HIGH

Well, I'm almost positive that HIGH is not defined as having the value of 13, so that test will almost assuredly return false, and the if block will be skipped.

I think you want something like this:

int switchVal = digitalRead(buttonPin);
if(switchVal == HIGH)
{
   // Do something
}

Really, you probably do not have a button connected to the Arduino. It's a switch. So, the number of the pin it is attached to should be stored in a variable named switchPin. Just a pet peeve of mind.

Ok, Paul. Thanks again. Most of the tutorials/examples I was reading used button, so I just used that. Added a delay before counter ++ also, since my switch seems to be excessively sensitive, so acts a little like a debounce function...or just a lazy way to do it. Anyways, works great now and thanks for the help.

Final setup:

int counter = 0;

int switchPin = 13;

int LEDB = 11;
int LEDG = 10;
int LEDR = 9;


void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  //Handle input
  int switchVal = digitalRead(switchPin);
  if(switchVal == HIGH)
  {
    delay(500);  
    counter ++;
    //Reset count if over max mode number
    if(counter == 8)
    {
      counter = 0;
    }
  }

  else
    //Change mode
    switch (counter) {
    case 1:
      analogWrite(LEDR, 255);
      break;
    case 2:
      analogWrite(LEDR, 127);
      break;
    case 3:
      analogWrite(LEDR, 000);
      analogWrite(LEDB, 255);
      break;
    case 4:
      analogWrite(LEDB, 127);
      break;
    case 5:
      analogWrite(LEDR, 255);
      analogWrite(LEDG, 255);
      analogWrite(LEDB, 255);
      break;
    case 6:  
      analogWrite(LEDR, 127);
      analogWrite(LEDG, 127);
      analogWrite(LEDB, 127);
      break;
    case 7:
      analogWrite(LEDR, 000);
      analogWrite(LEDG, 000);
      analogWrite(LEDB, 000);
      break;
    }
}