I'm not sure what you know about electronics or coding, but with swithces you need to know about contact bounce: Switch - Wikipedia , and methods for supressing that. Otherwise you most likely will get multiple button presses for one.
A look at the Arduino playground under Input - mechancial - switches has some software to look at: Arduino Playground - InterfacingWithHardware Also there is a Debounce example in the Arduino IDE, under "File - Sketchbook - Examples - Digital".
With two LED's and buttons there's not a hole lot you can do, but its a start to a hole lot You could look at what this guy is trying to do with two LED's (one red and one blue) and a button: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249624955/14#14
I hope you didn't mix the Buttons library with the Debounce example?
I've never used the Buttons lib, so maybe others can correct me if I'm wrong. I agree its not very well explained. And also it seems he forgot a line in his example (in the somewhat confusing image, which shows the header file with black background to the left, listing the methods and constants quickly, and an example in the Arduino IDE to the right).
But it does say so below the image:
Don't forget to link the Arduino pins to the library with ButtonName.assign...
So I think he forgot to include a "B.assign(pin);" in the seup() function in that example. Where "pin" is a byte value (or simply a constant) of the Arduino pin nr for the button.
I don't know how far/short you've gone, but maybe for a first tiny test, make it so that one of the LEDs turns on when one of the buttons are pressed down, and off when not (very easy, no library, debounce or nothing needed really. In fact no Arduiono needed either, but you could still test it with one )
Another easy test, turn the LED(s) off again if both buttons are pressed down.
Then move on to what you said in your original post; at the first press of a button, turn the LED on, and then at the next press of the same (or the other?) button, turn it off. Just to get the debounce button-thing going. Whether you use the Buttons library or not. Then worry about your blinker.
I hope you didn't mix the Buttons library with the Debounce example?
No, I haven't done anything with the Button library. I need someone to explain it to me.
I don't know how far/short you've gone, but maybe for a first tiny test, make it so that one of the LEDs turns on when one of the buttons are pressed down, and off when not
Yeah that is one of the examples. I have done all I can do with that for now.
I some how managed to get it to work with button push > led on , button push > led off. but it is weird. if you don't time it right it doesn't work... i dont know. here is my code
const int buttonPIN = 2;
const int ledPIN = 13;
int ledState = HIGH;
int buttonState;
void setup () {
pinMode (buttonPIN, INPUT);
pinMode (ledPIN, OUTPUT);
}
void loop () {
int ledState = digitalRead(ledPIN);
int buttonState = digitalRead(buttonPIN);
if (buttonState == HIGH) {
if (ledState == LOW) {
digitalWrite(ledPIN, HIGH);
}
}
if (buttonState == HIGH) {
if (ledState == HIGH) {
digitalWrite(ledPIN, LOW);
}
}
}
please tell me everywhere i messed up .. haha remember im just getting started. ;D
ok so I need some more help .... I have a RGB Tricolor LED. I want it hook up so that I push the button and it is one color. push the same button, different color, so on and so on.
i have the code worked out i think ... it works with three different LEDs but i just dont want to have wasted 2.50 on this stupid thing.
the red part seems to always come on with the blue and the green is just find. so please any help would be GREAT!
Well, you should probably assign variables to control states, instead of reading an Output port to determine...
This will do as you asked,"I want it hook up so that I push the button and it is one color. push the same button, different color, so on and so on."
However, if you have a bouncy switch, you may skip colors. See the debouncing libraries and samples in the playground for more... (I couldn't take away all your fun, now, could I?)
#define buttonPIN 2
#define ledPING 10
#define ledPINB 12
#define ledPINR 11
int buttonState;
int lastButtonState;
void setup ()
{
pinMode (buttonPIN, INPUT);
pinMode (ledPING, OUTPUT);
pinMode (ledPINB, OUTPUT);
pinMode (ledPINR, OUTPUT);
buttonState='R';
lastButtonState=LOW;
adjustLEDs();
}
void adjustLEDs()
{
digitalWrite(ledPINB, buttonState == 'B');
digitalWrite(ledPING, buttonState == 'G');
digitalWrite(ledPINR, buttonState == 'R');
}
void loop ()
{
int iCurrentState = digitalRead(buttonPIN);
// this changes colors on release of button. Reverse
// HIGH and LOW in this next line to detect it on the initial press of the button.
if(lastButtonState==HIGH && iCurrentState==LOW)
{
if(buttonState=='B')
buttonState='G';
else if(buttonState=='G')
buttonState='R';
else if(buttonState=='R')
buttonState='B';
adjustLEDs();
}
lastButtonState = iCurrentState;
}
I haven't compiled this myself, but it should work...