Go Down

Topic: LCD counter up and down (Read 761 times) previous topic - next topic

jmsgarton

Hello guys,

I am newish to arduino and i'm not sure how to go abouts making a script for the project i am wanting to build. Ive got a 16x2 LCD display what i want to use to count a score so it would say something like score : * then display the number of counts. I want the counter to count when a button is pushed but depending if another input is high or low I want it to count up or down. for example if a user presses the button and the other input was low the counter would count up. if the same other input was high and the user presses the button it needs to count down one. I have managed to do something which counted up but when you keep the button down it would just continue counting up rather than just counting up once. could someone help me on this. thanks in advanced.

AWOL

Quote
could someone help me on this.

You need to post your code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

elac

You need a debounce when the button is pressed.
Most easiest is
Code: [Select]
delay(your time here);
Better is following the blink without delay example.
It's all about the skills

jmsgarton

ive got this far but i need to add more to count down if the input is set to high - after mastering this i can change the serial mode to write onto a display instead

const int  buttonPin = 23;    // the pin that the pushbutton is attached to
int midledin = 22;  // the pin that is either high for counting down or low for counting up

// 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
int ledState = 0;            // current state of line in

void setup() {
  pinMode (midledin, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
 
}


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

  // compare the buttonState to its previous state
  if (ledState == LOW && buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend 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
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

}

Go Up