Basic problems with counting, buttons and lights

Hi All

I have written some fairly basic code, but the logic does not make much sense to me. I have two LEDs and one button.

I would like to press the button and one light turns off, and the other on. I would like this state to be permanent until I next press the button.

When I up load the code one LED turns on, when I press the button both turn off. I do not understand why one does not turn off, and the other on.

If I change

if (buttonState == HIGH) {
count +=1;

}

to

if (buttonState == LOW) {
count +=1;

}

Then the lights alternate flashing on and off. I am therefore sure that these are wired correctly.

// variables will change:

const int buttonPin = 2;     // the number of the pushbutton pin
const int greenPin =  13;      // the number of the green pin
const int redPin =  5;      // the number of the red pin
int buttonState = 0;         // variable for reading the pushbutton status

int count = 0;            // the number of times the button has been pressed

void setup() {
  // initialize the LED pin as an output:
  pinMode(greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600); 
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    count +=1;
    
  } 
  

  if (count%2 == 1){
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, LOW);
    delay(500);
    }

    if(count% 2 != 1) {
      digitalWrite(greenPin, LOW);
      digitalWrite(redPin, HIGH);
      delay(500);
      }
  
}

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it not an Introductory Tutorial

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

consider

    byte but = digitalRead (butPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            digitalWrite (ledPin, ! digitalRead (ledPin));
    }

1 Like

Thank you for this, it makes a lot of sense.

The only issue now is that pushing the button stops serial output. Is there a reason for this?

What serial output are you expecting? I do not see any use of Serial.print() in your code, only Serial.begin(), which does not send anything to serial monitor.

by now you should realize you need to post your code to answer the question

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

i get following after several button presses with my hardware (using pin A1)

on
number of button pushes: 1
off
on
number of button pushes: 2
off
on
number of button pushes: 3
off
on
number of button pushes: 4
off
on
number of button pushes: 5
off
on
number of button pushes: 6
off
on
number of button pushes: 7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.