Decrement is not working

Hello everyone,

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;

count += 1; ?

Similar for decrement.

It doesnt work...
I've tried that, but unfortunately without any results.

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.

count = +1;

should be:

count += 1;

or

count++;

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.

I would recommend not using pull down resistors and use INPUT_PULLUP with your buttons wired to GND. This way pressed is active LOW.

ooh,yes i changed it afterwards, maybe if you reload your page you will see it.

Your pulldown resistors need to be wired from GND to the input.

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.

Fix your wiring and do what Idahowalker said.

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.

Your code is incomplete

I have to press the up button and then the down button to get something from the serial monitor.

So this is what it shows:

Please, look into the following diagram (Fig-1) that contains your buttons and answer to the questions:
swbb.png
Figure-1:

1. Place DVM in tone mode between 1-2, press the button; is it a closed contact/hearing tone? Yes/No.

2. Place DVM in tone mode between 3-4, press the button; is it a closed contact/hearing tone? Yes/No.

3. Place DVM in tone mode between 1-3, press the button; is it a closed contact/hearing tone? Yes/No.

4. Place DVM in tone mode between 2-4, press the button; is it a closed contact/hearing tone? Yes/No.

swbb.png

How to connect buttons / switches, S3 is preferred:

TheMemberFormerlyKnownAsAWOL, what should I add to the code?

@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.

faissal-b:
TheMemberFormerlyKnownAsAWOL, what should I add to the code?

The rest of it.

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.

TheMemberFormerlyKnownAsAWOL:
The rest of it.

What's the rest?