Go Down

Topic: Button for Mode Select (Read 8872 times) previous topic - next topic

Jeremy1998

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...

mrmeval

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

http://msdn.microsoft.com/en-us/library/fh88ctk1%28VS.80%29.aspx

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. :)
If it was designed by man it can be repaired by man.

Jeremy1998

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?

Osgeld

#3
Jun 16, 2010, 05:47 am Last Edit: Jun 16, 2010, 05:49 am by Osgeld Reason: 1
Code: [Select]

int counter = 0;

void setup(){}

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


http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Jeremy1998

so where would the code go?

Osgeld

#5
Jun 16, 2010, 06:01 am Last Edit: Jun 16, 2010, 06:04 am by Osgeld Reason: 1
Code: [Select]
   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
Code: [Select]

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

http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Jeremy1998

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?

Osgeld

#7
Jun 16, 2010, 06:05 am Last Edit: Jun 16, 2010, 06:06 am by Osgeld Reason: 1
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
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Jeremy1998

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...

Code: [Select]
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);
}  

Jeremy1998

#9
Jun 16, 2010, 06:46 am Last Edit: Jun 16, 2010, 06:46 am by Jeremy1998 Reason: 1
ok, i wrote some more code, and it compiled... i can't test if it works, though...

Code: [Select]
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{}  
}  
}

artjumble

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

It should be like this:
Code: [Select]

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.

Jeremy1998

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

Code: [Select]
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);
 }  
}  
}

artjumble

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.

Code: [Select]

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);
}

Jeremy1998

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

jgueydan

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:

Code: [Select]

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.

Go Up