How do i add a button to my arduino code so when i press it it will change to another code?

so i have my program and it is a 4 led chaser and i want to push a button and and make it do a different pattern this is my code i works


int pinled1 = 12;
int pinled2 = 11;
int pinled3 = 10;
int pinled4 = 9;

int tim = 50;
void setup()
{
pinMode(pinled1, OUTPUT);
delay(50);
pinMode(pinled2, OUTPUT);
delay(50);
pinMode(pinled3, OUTPUT);
delay(50);
pinMode(pinled4, OUTPUT);
delay(50);

}

void loop()
{

digitalWrite(pinled1, LOW);
delay(tim);
digitalWrite(pinled1, HIGH);
delay(tim);
digitalWrite(pinled2, LOW);
delay(tim);
digitalWrite(pinled2, HIGH);
delay(tim);
digitalWrite(pinled3, LOW);
delay(tim);
digitalWrite(pinled3, HIGH);
delay(tim);
digitalWrite(pinled4, LOW);
delay(tim);
digitalWrite(pinled4, HIGH);
delay(tim);
digitalWrite(pinled1, LOW);
delay(tim);
digitalWrite(pinled1, HIGH);
delay(tim);
digitalWrite(pinled2, LOW);
delay(tim);
digitalWrite(pinled2, HIGH);
delay(tim);
digitalWrite(pinled3, LOW);
delay(tim);
digitalWrite(pinled3, HIGH);
delay(tim);
digitalWrite(pinled4, LOW);
delay(tim);
digitalWrite(pinled4, HIGH);
delay(tim);

}



6 answers
sort by: active | newest | oldest
Dec 30, 2009. 6:15 PMTitanTechRobotics says:
You could make each pattern in a "if" statement, like lemonie said, and also have the led pattern inside a "while" statement so that it will keep repeating until the other button is pressed. the code would look something like this:

int pinled1 = 12;
int pinled2 = 11;
int pinled3 = 10;
int pinled4 = 9;
int button1 = whatever pin you want it on;
int button2 = whatever pin you want it on;
int buttonstate1 = 0; // this variable will change
int buttonstate2 = 0; // this variable will change

int tim = 50;
void setup()
{
pinMode(pinled1, OUTPUT);
delay(50);
pinMode(pinled2, OUTPUT);
delay(50);
pinMode(pinled3, OUTPUT);
delay(50);
pinMode(pinled4, OUTPUT);
delay(50);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}

void loop()
{
buttonstate1 = digitalread(button1);
buttonstate2 = digitalread(button2);

if (buttonstate1 == HIGH){
while(buttonstate2 ==  LOW)
{
buttonstate1 = digitalread(button1);
buttonstate2 = digitalread(button2);
//put your led pattern here
}}

else if (buttonstate2 == HIGH){
while(buttonstate1 == LOW)
{
buttonstate1 = digitalread(button1);
buttonstate2 = digitalread(button2);
//put second led pattern here
}}}


You could shorten it up by making a "check buttons" subroutine.  Also, why did you put delays after you set the pins to input or out put?  The code may not be completely right(a few semi-colons or brackets missing)


if you have any other questions, just let me know
Dec 30, 2009. 7:22 PMTitanTechRobotics says:
I don't have an arduino available right now, but I might try in when I get another one.  
Thank you  for select my answer for best answer!
Dec 30, 2009. 3:18 PMlemonie says:
Some king of "if" statement?

L

Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!