Pages: [1] 2   Go Down
Author Topic: Button for Mode Select  (Read 8324 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Greenwood, Indiana
Offline Offline
God Member
*****
Karma: 0
Posts: 508
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

If it was designed by man it can be repaired by man.

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int counter = 0;

void setup(){}

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

« Last Edit: June 15, 2010, 10:49:48 pm by Osgeld » Logged


Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so where would the code go?
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   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:
   if(counter < 100)  counter++; //advance the counter by 1
    else
    {
         // if counter is greater than 100 do something
         counter = 0; // reset the counter
    }
« Last Edit: June 15, 2010, 11:04:38 pm by Osgeld » Logged


Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: June 15, 2010, 11:06:36 pm by Osgeld » Logged


Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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{}  
 }  
}
« Last Edit: June 15, 2010, 11:46:59 pm by Jeremy1998 » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It should be like this:
Code:
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.
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
  }  
 }  
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Greenfield, NH
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: