Membuat counter score arduino push button dan lcd i2c


-Arduino / nodemcu

- Lcd 16x2 i2c atau yang lainnyq

- push button

- Jumper


Skematik :



Program:



#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display


// Variabel pin push button

const int Up_buttonPin = 2;

const int Down_buttonPin = 3;


// Variables will change

int buttonPushCounter = 0; // counter jika tidak ditekan

int up_buttonState = 0; // current jika tidak ditekan

int up_lastButtonState = 0; // previous state dari push button

int down_buttonState = 0; // current state of the up button

int down_lastButtonState = 0; // previous state of the up button


bool bPress = false;


void setup()

{

  Serial.begin(9600);

  pinMode( Up_buttonPin , INPUT_PULLUP);

  pinMode( Down_buttonPin , INPUT_PULLUP);

  lcd.init(); // initialize the lcd

  // Print a message to the LCD.

  lcd.backlight();

  lcd.setCursor(0,0);

  lcd.print("silahkan pilih:");

  lcd.setCursor(2,1);

  lcd.print(buttonPushCounter);

}


void loop()

{

   checkUp();

   checkDown();

   if( bPress){


       bPress = false;

      lcd.setCursor(2,1);

      lcd.print(" ");

      lcd.setCursor(2,1);

      lcd.print(buttonPushCounter);

   }

}


void checkUp()

{

  up_buttonState = digitalRead(Up_buttonPin);

  // compare the buttonState to its previous state

  if (up_buttonState != up_lastButtonState) {

    // if the state has changed, increment the counter

    if (up_buttonState == LOW) {

        bPress = true;

      // 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


  up_lastButtonState = up_buttonState;

}


void checkDown()


{

  down_buttonState = digitalRead(Down_buttonPin);

  // compare the buttonState to its previous state

  if (down_buttonState != down_lastButtonState) {

    // if the state has changed, increment the counter

    if (down_buttonState == LOW) {

        bPress = true;

      // 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


  down_lastButtonState = down_buttonState;

}



Posting Komentar

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.