Reversing a Sketch with a button

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.

Street_lights_with_pot_switch.ino (1.44 KB)

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?

It looks like analogRead() is for the timing.

The code seems to be missing a line

buttonState = digitalRead(buttonPin);

...R

Thanks for the help. Would I place the line you sent me into the loop section of the Sketch?

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.

The Blink Without Delay is a great way to see what to "delay" things without using the delay() function:
http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Depending on what you would like to do, you could also map the sensor value so you can have a minimum and a maximum delay time:

well, just a couple of ideas.
:wink:

Thanks for the advice. I added the missing line to the sketch and I cleaned up the "if" and "else" state but I got an error

Arduino: 1.5.6-r2 (Windows XP), Board: "Arduino Uno"

Street_lights_with_pot_switch:45: error: expected unqualified-id before 'else'
Street_lights_with_pot_switch:59: error: expected declaration before '}' token

What have I missed?

Street_lights_with_pot_switch.ino (1.53 KB)

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); 
  }
}
  if(buttonState == HIGH);

That's not doing anything useful

You have a inappropriate semi-colon here where you actually need an opening brace - {

 if(buttonState == HIGH); //turn LED on

Thanks - I think that did the trick. Regarding the AWOL I don't know how to embed the code into the message.

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!
:wink:

So, this (without the spaces):
[ code ]
Your code here...
[ /code ]

will be displayed as:

Your code here...

Thats Great. I didn't notice that I could do that. A bit of a Newb (Noob)"Next time I will enter my sketch here"