Hi there,
I've taken the blink sketch and changed it to have 3 LEDs blinking in sequence and I can control the timing by using a potentiometer... What Iwant to do is add a button to change the direction of the sequence and then change it back with another click. What do I need to add to the sketch to fix it? Thanks in advance,
Michael.
How does buttonState get changed? Try looking at what comes back from the switch. Also, why an analogRead() when you're just looking for a switch closure?
mbvaisman:
Thanks for the help. Would I place the line you sent me into the loop section of the Sketch?
you could put it in the loop function after
sensorValue = analogRead(sensorPin);
Something else to consider...
If you want to be able to read a button, using so often delay() functions isn't a very good idea.
Whenever you use a delay() function it is kind of like "pausing" the arduino, so if you would press a button during that "pause" it wouldn't be read.
You missed posting your code in the most convenient manner
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
const int buttonPin = 2;
int redled = 11;
int yelled = 12;
int greled = 13;
int sensorPin = A0;
int sensorValue = 0;
int buttonState = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(redled, OUTPUT);
pinMode(yelled, OUTPUT);
pinMode(greled, OUTPUT);
pinMode(buttonPin, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
sensorValue = analogRead(sensorPin);
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH); //turn LED on
digitalWrite(redled, HIGH);
delay(sensorValue);
digitalWrite(redled, LOW);
delay(sensorValue);
digitalWrite(yelled, HIGH);
delay(sensorValue);
digitalWrite(yelled, LOW);
delay(sensorValue);
digitalWrite(greled, HIGH);
delay(sensorValue);
digitalWrite(greled, LOW);
delay(sensorValue);
}
else {
digitalWrite(greled, HIGH);
delay(sensorValue);
digitalWrite(greled, LOW);
delay(sensorValue);
digitalWrite(yelled, HIGH);
delay(sensorValue);
digitalWrite(yelled, LOW);
delay(sensorValue);
digitalWrite(redled, HIGH);
delay(sensorValue);
digitalWrite(redled, LOW);
delay(sensorValue);
}
}
mbvaisman:
I don't know how to embed the code into the message.
On the Message Icons, on the bottom row, the third on the right is an Number Sign ("#"). Click it and then insert your code inside the [ code ] which will apear. Nice and easy!
So, this (without the spaces):
[ code ]
Your code here...
[ /code ]