Hello. I'm doing a college electronics project and nearly have it complete. When finished, it should do the following:
Mine does everything except when turned on, all three LEDs are lit, and when turned off, the alternating LED 1 or 2 remains lit (depending on the potentiometer). I know I'm close, but can't figure out what's wrong. Any help would be appreciated. Here is my code:
This code, while not very complicated is not doing quite what you probably think it is.
First lets look at the debounce function. It does the following:
read the button and assign the state to "current"
If that value was different than the last one:
Read the button again and assign the state to "current"
The problem is that the two reads will almost always return the same value because they are occurring mere microseconds after one another. This function might do some debouncing if you added a delay between these two reads, but it is still not a great way to debounce. Check out the debounce example posted on this site and in the Arduino IDE for a better way to debounce. In you specific application it will work because of a delay outside of the function, but that means that your debounce function could just as easily be replaced by the line
currentButton=digitalRead(buttonPin);
To answer your main question, you are indeed very close, but it looks like you are missing one thing. You check to see that the LED is on, but if it is not on you need to write both of the other LEDs LOW also so that they do not remain on from before. With that done, check to make sure that you wired the LEDs so that they turn on with a high signal, as it is possible to wire them up the other way.
And some general code style issues/tips:
You may have noticed you never used the Boolean variable ledOff, and for good reason because a false value for ledOn means that the LED is off.
You also have a (ledOn != false), this could be stated more elegantly as if(ledOn == true), or better yet as if(ledOn). This last one works because ledOn is a Boolean type the if statement will look at ledOn and decide if it is true or false without any need to compare it to anything else.
Clear these things up and post again if you still have issues. While many of these things likely are not causing you problems fixing them will make your code far easier to read, and thus will make it more likely for you to find people willing to help you here.
I updated my debounce function but am still having trouble with the lights. I've tried a couple different coding techniques but none seem to link all 3 LEDs to turn off. :~
Despite it's many quirks YOUR CODE WORKS! I couldn't find anything wrong with it so I wired up your circuit and downloaded your code as posted above. I did not change a thing, and it worked exactly as you described, so now we have a hardware problem. (Or a discrepancy between Arduino 1.0 and whatever you are running, but I doubt that is the case)
Here is how it should be wired.
Pins 9-11 each go to the positive side of their own LED. The other side of those led's should connect to ground through a resistor in the 270 ohm range.
The potentiometer should have the wiper connected to analog pin 0 and the other two leads go to power and ground. Make sure you have the correct pin going to the Arduino by watching the output voltage with a meter. If the voltage changes when you move the slide you have the right one.
The button is the part that is the easiest to screw up. Connect one terminal(A) to 5V and the other (B) to ground through a resistor (10K or whatever you feel like). Then connect the digital pin 6 to the button at terminal B. If you do this right the Arduino should see 5V directly if the button is pushed, and ground through the resistor if it is not.
One other note, as a college student myself I never take for granted that anything in our labs is working as it should be. It would not surprise me if your Arduino is a bit fried. Try running the blink sketch to see if each digital pin is working correctly, and use the serial monitor to or an LED indicator to check the inputs. Alternativly try a different Arduino if there are more around.