PaulS:
if i past in the code i have so far could you help me apply your solutions into it?
After you try, if you have issues, by all means post and ask for help. You need to try though.
Fair enough, Thanks for the motivation! I understand about setting the switches with the state change, but a problem i came across was not being able to press the button thereby changing the function in the middle of the function itself. I had to hold the button until the loop had restarted, this is because in the functions i am using I have delays between LED illumination... So i want to try and overcome that too... This is what i Had Previously:
//6 LED Chaser Pins 8 - 13
const int LED1 = 13;
const int LED2 = 12;
const int LED3 = 11;
const int LED4 = 10;
const int LED5 = 9;
const int LED6 = 8; //PIns 8 -13 can be referred to as LEDs 1 - 6
void setup() {
pinMode(LED1, OUTPUT); //Sets all the LED's as outputs.
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
}
void loop()
{
chaser(); //Runs Function 1 - Chaser
centerchase(); //Runs Function 2 - Centerchase
}
void chaser() //The first Function
{
digitalWrite(LED1, HIGH);
delay(100);
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(100);
digitalWrite(LED3, LOW);
digitalWrite(LED4, HIGH);
delay(100);
digitalWrite(LED4, LOW);
digitalWrite(LED5, HIGH);
delay(100);
digitalWrite(LED5, LOW);
digitalWrite(LED6, HIGH);
delay(100);
digitalWrite(LED6, LOW);
digitalWrite(LED5, HIGH);
delay(100);
digitalWrite(LED5, LOW);
digitalWrite(LED4, HIGH);
delay(100);
digitalWrite(LED4, LOW);
digitalWrite(LED3, HIGH);
delay(100);
digitalWrite(LED3, LOW);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
}
void centerchase() // The second function
{
digitalWrite(LED1,HIGH);
digitalWrite(LED6,HIGH);
delay(100);
digitalWrite(LED1,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
delay(100);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED1,HIGH);
digitalWrite(LED6,HIGH);
delay(100);
digitalWrite(LED1,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
delay(100);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
}
This is what i had after attempting to add a button in, I used a slihgtly different method but i still dont see why it won't work properly....
//6 LED Chaser Pins 8 - 13
const int LED1 = 13;
const int LED2 = 12;
const int LED3 = 11;
const int LED4 = 10;
const int LED5 = 9;
const int LED6 = 8; //PIns 8 -13 can be referred to as LEDs 1 - 6
const int BUTTON = 7; //Pin 7 can be referred to as BUTTON
int val = 0; //Val used to store input of Switch or pin 7
int state = 0; //0 Shows LED off 1 is LED on
int old_val = 0; // Previous version of val
void setup()
{
pinMode(LED1, OUTPUT); //Sets all the LED's as outputs.
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(BUTTON, INPUT); //Sets Button as input
}
void loop()
{
val =digitalRead(BUTTON); //Reads the value of the button and stores
if ((val == HIGH) && (old_val == LOW))
{
state = 1 - state; //Check if state has changed
delay(10);
}
old_val = val; // Val is now old val so its stored
if (state == 1)
{
chaser(); //Runs Function 1 - Chaser
}
if (state == 0)
{
centerchase(); //Runs Function 2 - Centerchase
}
}
void chaser() //The first Function
{
digitalWrite(LED1, HIGH);
delay(100);
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(100);
digitalWrite(LED3, LOW);
digitalWrite(LED4, HIGH);
delay(100);
digitalWrite(LED4, LOW);
digitalWrite(LED5, HIGH);
delay(100);
digitalWrite(LED5, LOW);
digitalWrite(LED6, HIGH);
delay(100);
digitalWrite(LED6, LOW);
digitalWrite(LED5, HIGH);
delay(100);
digitalWrite(LED5, LOW);
digitalWrite(LED4, HIGH);
delay(100);
digitalWrite(LED4, LOW);
digitalWrite(LED3, HIGH);
delay(100);
digitalWrite(LED3, LOW);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
}
void centerchase() // The second function
{
digitalWrite(LED1,HIGH);
digitalWrite(LED6,HIGH);
delay(100);
digitalWrite(LED1,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
delay(100);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED1,HIGH);
digitalWrite(LED6,HIGH);
delay(100);
digitalWrite(LED1,LOW);
digitalWrite(LED6,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
delay(100);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED5,HIGH);
delay(100);
digitalWrite(LED2,LOW);
digitalWrite(LED5,LOW);
}
What the above does is getting there, when i press the button it will switch to the other function, BUT i have to HOLD ON the button until the end of each function cycle before it will register the state change. I took a video of the circuit using 6 leds. Shown below: (Photobucket upload)