SWITCH COUNTER (INCREMENT)

Hello there! I just wanted to ask if how can I solve this problem:

I connected a push button and LCD to my Arduino board. Now, every time I press the button, “count” will be incremented by 10 and will be displayed in LCD. However, when I pressed the push button longer, count increments continuously.

Here’s what I wanted:

I just wanted to count it only once. When I pressed it longer, it should only increment when I released the push button.

Thanks in advance! :slight_smile:

By the way, the code below is for increment and decrements. If switch A (connected to digital Pin 18) is pressed, count will increment. If switch B (connected to digital Pin 30) is pressed, count decrements. Count also signifies the intensity of brightness of LED (attached to digital pin 24).

Here’s the code:

#include <popx2.h>	// POP-X2 Board

int count = 0;

void setup() {
  OK();
  setTextSize(7);
  setTextColor(GLCD_ORANGE);
  pinMode(18, INPUT);
  pinMode(30, INPUT);
  pinMode(20, OUTPUT);
  
  if (digitalRead(18) == HIGH){
      count = 0;
  }
  
}

void loop() {
  int button = digitalRead(18);
  int button2 = digitalRead(30);

  if (button == HIGH)
  {
    
    if (count < 250) {
              count = count + 10;
              glcd(1, 0, "%d ", count);
              analogWrite(20, count);
              delay(500);
        }

    }


  if (button2 == HIGH)
  {
    if (count > 0) {
      count = count - 10;
      glcd(1, 0, "%d ", count);
      analogWrite(20, count);
      delay(500);
    }
      
    }
    
  }

Theres an example built in to the IDE called State Change Detection - it does what you need.

Here's the online version.

Two things:

1) Why did you found it necessary to scream? That's considered very rude in the online world ;)

2) Let me get that straight, you have a code problem, you want help with your code, but you don't think it's necessary to post your code? Interesting...

And don't come with the excuse you're new, that's why there is a big fat sticky at the top of each board called How to use the forum so it's really plain laziness ;)