I'm trying to use a pushbutton as mode switch to choose between two patterns to blink 5 LEDs. I had some help from this previous forum topic: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1275018531/7
The code uses a variable 'counter', and everytime the switch pulls the input pin high, it advances the counter by one. The hardware is working, it turns on the built in LED on pin 13.
The problem is, that the mode is changing on its own. It cycles once through one pattern, then once through the other. And repeats. Pushing the button doesn't seem to change anything. Either the counter variable is advancing itself, or the software is ignoring the if/else statements that choose the pattern.
Have a gander:
int counter = 0;
int val = 0;
int buttonPin = 13;
int x;
int ledPins[] = {2,3,4,5,6};
int ledTime = 150;
int sampleTime = 0;
int knightRider[] = {2,3,4,5,6,5,4,3};
int randPattern[] = {2,4,2,4,3,5,3,6,2,6,4,5,4,5,3,6};
void setup()
{
for(int i = 0; i < 5; i++){
pinMode(ledPins[i],OUTPUT);
}
pinMode(buttonPin, INPUT);
}
void loop()
{
val = digitalRead(buttonPin);
if(val = HIGH)
{
counter++;
if(counter == 2)
{
counter = 0;
}
}
if(counter == 0)
{
x = analogRead(0);
if (x+5 < 300)
{
allOn();
delay(sampleTime);
return;
}
else
{
allOff();
}
for(int j = 0; j < 8; j++){
digitalWrite(knightRider[j], HIGH);
x = analogRead(0);
if (x+5 < 300)
{
allOn();
delay(sampleTime);
return;
}
else
{
delay(ledTime);
digitalWrite(knightRider[j], LOW);
}
}
}
else if(counter == 1)
{
x = analogRead(0);
if (x+5 < 300)
{
allOn();
delay(sampleTime);
return;
}
else
{
allOff();
}
for(int j = 0; j < 16; j++){
digitalWrite(randPattern[j], HIGH);
x = analogRead(0);
if (x+5 < 300)
{
allOn();
delay(sampleTime);
return;
}
else
{
delay(ledTime);
digitalWrite(randPattern[j], LOW);
}
}
}
}
void allOff(){
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
digitalWrite(ledPins[4], LOW);
}
void allOn(){
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
digitalWrite(ledPins[4], HIGH);
}
Your help is appreciated.