I'm very very new to Arduino and I want to make one simple project,nothing too hard.
So I got an Arduino Nano for my job project.I want to light on LED with 2 buttons when they are pressed.First I searched forum for similar project but I didn't get lucky.
I'm having problem which I don't understand.I don't know what to do about the code,it looks fine to me.I'm currently not in situation to check if it works until tomorrow so if it helps I will thank you later.
Buttons are on pins 10 and 11,I use builtin LED so it's on pin 13,both of button's wires are connected on pins(10 and 11) and other wires from buttons are connected to ground.
If someone could help me,it would be great.And if you could say 2 solutions if you know more than one.
OP: It looks like it should "work". I assume the "buttons" are tactile switches. Be sure to wire them by attaching any pin of the button to the Arduino pin, and the button pin that is diagonally opposite to it to ground. Then you will need to invert the state in software. Also using smaller data types in a "good thing".
Try these mods: (Note proper indenting. Use 'control T' to format your code in the IDE. It makes it easier to debug later.)
boolean buttonState1; // boolean variable takes only 1 byte
boolean buttonState2;
const byte buttonPin1=10; // byte constant takes only 1 byte
const byte buttonPin2=11;
const byte ledPin = 13; // named variable allows easy changes later
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
buttonState1 = !digitalRead(buttonPin1); // Pull up digitalRead is HIGH when not pushed, LOW when pushed
buttonState2 = !digitalRead(buttonPin2);
if (buttonState1 and buttonState2){ // since they are booleans, you don't need "== HIGH"
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}
Referring to the modifications I posted, see the two lines right after loop()? See the exclamation points in front of digitalRead()? Those are a C keyword, and the reason your buttons seem to be working backward. Look it up on this Reference page.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Because you are using your buttons to switch to gnd, when your buttons are not pressed the two inputs are pulled HIGH by the internal pullup resistors.
You pull the inputs LOW when you press your buttons.