I took a simple code from the example menu in Arduino, it's the ''button'' code which lets you turn an LED on by pushing a button wired in a simple circuit with a pull-down resistor.
I'm now trying to play around with the code and, of course, I'm having some problems...
I want to have 2 buttons and 1 LED and if I push one of the buttons, the LED turns on.
I do this because I want to try the boolean logic operators (here I play with ''or'' ( || ) ).
I wired the buttons to pin 2 and 8 and the LED to pin 13. Both buttons are powered by 5v from Arduino and grounded to Arduino as well.
I verify the code and no problem there, I upload the code, push button 1 and the LED turns on, push button 8 and nothing AND THEN push button 2 again and nothing!!!!
Push button 2 about 10 times, LED flickers and gets to working again??? Ground problem?
But then If I reload the sketch and try button 8 FIRST, still nothing!
Am I using the right tools to do this?
here is the code :
http://www.arduino.cc/en/Tutorial/Button
*/
const int buttonPin2 = 2;
const int buttonPin8 = 8;
const int ledPin = 13;
int buttonState2 = 0;
int buttonState8 = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin8, INPUT);
}
void loop()
{
buttonState2 = digitalRead(buttonPin2);
buttonState8 = digitalRead(buttonPin8);
if (buttonState8 == HIGH || buttonState2 == HIGH )
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
The code looks fine, so let's ask some questions about the hardware.
You have a pull-down resistor for the button on pin 8 too, right?
Many of the mini-pushbuttons only operate when they are oriented correctly. You can check yours with a multimeter to make sure you have button 8 connected the right way. If it is turned 90 degrees, it won't work properly.
I do have a pull down for each buttons and as for the buttons themselves their not minis (those with 4 pins, I know, I've learned that too...)
And gbulmer, you got me lost a little here... well I tried tu put your code up like this but it doesn't work at all, so I guess I must have misplaced something...
const int buttonPin2 = 2;
const int buttonPin8 = 8;
const int ledPin = 13;
int buttonState2 = 0;
int buttonState8 = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin8, INPUT);
}
void loop(){
buttonState2 = digitalRead(buttonPin2);
buttonState8 = digitalRead(buttonPin8);
boolean buttonstate = true;
if (buttonState8 == HIGH)
{
digitalWrite(ledPin, buttonstate);
buttonstate = ! buttonstate; // flip the state
}
if (buttonState2 == HIGH)
{
digitalWrite(ledPin, buttonstate);
buttonstate = ! buttonstate; // flip the state
}
delay(10);
}
const int buttonPin2 = 2;
const int buttonPin8 = 8;
const int ledPin = 13;
int buttonState2 = 0;
int buttonState8 = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin8, INPUT);
}
void loop()
{
buttonState2 = digitalRead(buttonPin2);
buttonState8 = digitalRead(buttonPin8);
if (buttonState8 == HIGH)
{
digitalWrite(ledPin, HIGH);
}
if (buttonState2 == HIGH)
{
digitalWrite(ledPin, LOW);
}
delay(10);
}
One button turns the LED on, the other turns the LED off.
I assume the hardware is not working in some way, and this should make it easier to see what is not working.
This is going somewhere, but please don't apologize!
... now both buttons are working, except that button 2 is only turning off the LED and button 8 turning it on. I can see that in your new code, you set digitalWrite to HIGH for pin 8 only and digitalWrite to LOW for pin 2 only, Is it intended? (ok I just saw now it was intended, sorry, I missed the end.))
Could you tell me what
boolean buttonstate = true;
means, and more generally what your code is doing compared to mine, I want to learn this and comparing wrong with right is always good,
... now both buttons are working, except that button 2 is only turning off the LED and button 8 turning it on. I can see that in your new code, you set digitalWrite to HIGH for pin 8 only and digitalWrite to LOW for pin 2 only, Is it intended?
Yes this was intended.
I'd hoped that you would be able to see that the hardware is working, or if it isn't this would make it easier to see what is broken. Your previous post suggested somethings weren't working.
boolean buttonstate = true;
A nice way to make a button work is to make a LED come on when a button is clicked, then off when clicked again.
This means the state of the LED must be flipped each time.
A boolean (and an int) can hold the value HIGH and LOW, which are the same as true and false.
So I could have written that as
int buttonstate = HIGH;
So buttonstate = ! buttonstate;
flips the value from true to false, or false to true. (the ! means not)
So then doing a digitalWrite(pin, buttonstate); will flip the LED on and off.
The effect of something using
if (buttonState8 == HIGH || buttonState2 == HIGH )
{
digitalWrite(ledPin, buttonstate);
buttonstate = ! buttonstate; // flip the state
}
would then be like the switches on the top and bottom of stairs.
Clicking either switch would flip the state of the LED.