I am a beginner in Arduino and I want to add a decrement button to my project. But whatever I try, it doesn't seem to work.
My buttons are both wired with a 330 Ω resistor to digital pins 5 and 6 and I declared them as INPUT.
I have tried many things, like adding another variable for the decrement. But that didn't seem to work either. I have added a schematic to be more clear. Can anyone please help me.
It would be appreciated.
Here's my code:
const int buttonUp = 5;
const int buttonDown = 6;
const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
int count = 0;
int firstTime = 1;
void setup() {
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
Serial.begin(9600);
}
void loop() {
while (!digitalRead(buttonUp) == HIGH){
if (firstTime) {
if (count < 3) {
count = +1;
}
}
delay(10);
firstTime = 0;
//Serial.println(count);
}
if (!firstTime) {
if (count == 1) {
digitalWrite(led1, HIGH);
digitalWrite(led3, LOW);
} else if (count == 2) {
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
} else if (count == 3) {
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
count = 0;
}
}
Serial.print(count);
Serial.print(" ");
Serial.println(firstTime);
firstTime = 1;
while (!digitalRead(buttonDown) == HIGH){
if (firstTime) {
if (count < 3) {
count = -1;
}
}
firstTime = 0;
delay(10);
}
if (!firstTime) {
if (count == 1) {
digitalWrite(led1, HIGH);
digitalWrite(led3, LOW);
} else if (count == 2) {
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
} else if (count == 3) {
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
count = 0;
}
}
Serial.print(count);
Serial.print(" ");
Serial.println(firstTime);
firstTime = 1;
Your schematic shows +5V and GND connected to the bottom of your breadboard. Did you intend to have those connected to the top? If you do you need to look closely at your pulldown resistors to make sure they are wired correctly.
Have you confirmed that the controller is receiving a button press? Such as write a simple program to detect the button press and display a message on the screen.
Idahowalker:
Have you confirmed that the controller is receiving a button press? Such as write a simple program to detect the button press and display a message on the screen.
ToddL1962 I just tried what you said, but I can't control the LED lights individually with one button. I have to press the other one to get them work. And this is only working for the increment button. the other one doesn't do anything.
@GolamMostafa:
That plan won't work if the button is turned 90 degrees, something that newbies are not always aware of. The pullup should be connected to the same hole column as the wire to the input pin.
JCA34F: @GolamMostafa:
That plan won't work if the button is turned 90 degrees, something that newbies are not always aware of. The pullup should be connected to the same hole column as the wire to the input pin.
Based on the current orientations of the buttons as marked by 1 2 3 4, let me have the answers of my questions of Post#12.
faissal-b:
TheMemberFormerlyKnownAsAWOL, what should I add to the code?
You have wiring AND code issues. I would correct the wiring per response #12 and #13 and write a test sketch that does nothing but verify the buttons are working. Then you will know the hardware is correct and you can debug your original sketch.