choosing patterns with one button

Hey. i am new at this so please be patient with me:P i just bought an arduino because i am really interested in such things. and i decided to make a little project. i wanted to make a led light show. kind off. so i put together 8 led. and i programmed some patterns. and it was easier then i though all worked well. now i wanted to put all of them together, and chose between them with only one button. and for some its easy. but i am busting my head the whole day trying to figure out how to do it. i know how to do it with two. and when i have to i have to hold the button down for the second one to go on. i would really appreciated your help thanks a lot:)

so through the night i was thinking... is switch the right choice?

Switch is fine. Mighty Abraxas posted some code recently that did this and had the same delay problem you do. Here is the thread: toggling between LED patterns - #7 by wildbill - Programming Questions - Arduino Forum

int timer = 40;
int val = 0;
int pin12 =12;

void setup() {

for (int thisPin = 2; thisPin < 10; thisPin++) {
pinMode(thisPin, OUTPUT);}
pinMode(pin12, INPUT);
}

void loop() {

val = digitalRead(pin12);

if(val==HIGH){

for (int thisPin = 2; thisPin < 10; thisPin++) {

digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
hold HIGH;
}
}
else
{

for (int thisPin =9 ; thisPin > 1; thisPin--) {
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW); }
}
}

for example how can choese with one button bettwen these two with the function switch... tnx :slight_smile:

int timer = 40;
int val = 0;
int pin12 =12;
boolean switchPattern = false ; // some sort of variable to keep track of which pattern we are at boolean takes up less memory
void setup() {

DDRD = B11111100; // sets pin 2-7 as outputs
DDRB = B000011; // sets pin 8 and 9 as outputs
//turotial @ http://www.arduino.cc/en/Reference/PortManipulation

//which is faster then this loop you have below
/* for (int thisPin = 2; thisPin < 10; thisPin++) {
pinMode(thisPin, OUTPUT);}
pinMode(pin12, INPUT); */
}

void loop() {

val = digitalRead(pin12);
if(val==HIGH)
{
switchPattern = !switchPattern;
}

if (switchPattern == 1 )
pattern1();
else
pattern2();
}

void pattern1()
{
for (int thisPin = 2; thisPin < 10; thisPin++)
{

digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
}
}
void pattern2()
{

for (int thisPin =9 ; thisPin > 1; thisPin--)
{
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
}

}

probalby could do this in a nicer cleaner way but this will work of you. learn from it do not just copy and paste
...i did not remove your delay functions im sure you can handle that.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

awesome thanks for the help:) yeah that's my main point to learn how to do those things:P :slight_smile:

one more question.. :stuck_out_tongue: what if i have more patterns then 2? i would like to use switch function and that i dont understand. how does that work.

for a swtich case method you would remove the

if (switchPattern == 1 )
pattern1();
else
pattern2();

and add this

switch (switchPattern)
{
case 1:
pattern1();
break;
case 2:
pattern2();
break;
case 3:
pattern4();
break;
case 4:
pattern4();
break;

}

how ever you would have to change your switchPattern from boolean to an integer , and when you check the state of your button increment switchPattern++;
as well as check that it does not go over the total amount of patterns you have

val = digitalRead(pin12);
if(val==HIGH)
{
switchPattern++;
if(switchPattern > totalPatterns)
switchPattern = 1;
}

Switch Case:
http://www.arduino.cc/en/Reference/SwitchCase

HelpFull Reading: