membuat conter lcd

 arduino code for lcd gambar
wiring rangkaian ldc with arduino

code program  arduino Uno



//YWROBOT

//Compatible with the Arduino IDE 1.0

//Library version:1.1

#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





// this constant won't change:

const int  Up_buttonPin   = 2;    // the pin that the pushbutton is attached to

const int  Down_buttonPin = 3;



// Variables will change:

int buttonPushCounter = 0;   // counter for the number of button presses

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

int up_lastButtonState = 0;     // previous state of the up 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("Please Select:");

  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;

}

counter hitung 2


code program

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int  buttonMasuk = 6;
const int  buttonKeluar = 7;
const int  buttonReset = 8;
int Penghitung = 0;
int statusMasuk = 0;
int statusKeluar = 0;
int statusReset = 0;
int statusTerakhir = 0;
int led = 13;
int maksimal = 20;

void setup() { 
   pinMode(buttonMasuk, INPUT);  
   pinMode(buttonKeluar, INPUT);
   pinMode(buttonReset, INPUT);
   pinMode(led, OUTPUT);    
   lcd.begin(16,2); 
   lcd.setCursor(1,0);
   lcd.print("Selamat Datang");
   lcd.setCursor(2,1);
   lcd.print("Di Boarduino");
   delay(5000);
   lcd.clear();
   lcd.setCursor(1,0);  
   lcd.print("Total :");
   lcd.setCursor(9,0);
   lcd.print("KOSONG");
   lcd.setCursor(1,1);
   lcd.print("Sisa  : ");
   lcd.setCursor(9,1);
   lcd.print(maksimal);
}

void loop() {
    statusReset = digitalRead(buttonReset);
    if (statusReset == HIGH) {
      Penghitung = 0;
      maksimal = 20;
      lcd.clear();
      delay(50);
      lcd.setCursor(1,0);  
      lcd.print("Total :");  
      lcd.setCursor(1,1);
      lcd.print("Sisa  : ");
      lcd.setCursor(9,0);
      lcd.print("KOSONG");
      lcd.setCursor(9,1);
      lcd.print(maksimal);
      digitalWrite(led, HIGH);
      delay(100);
      digitalWrite(led, LOW);
    }
   statusMasuk = digitalRead(buttonMasuk);
   if (statusMasuk != statusTerakhir) {
     if (statusMasuk == HIGH)
     {
      Penghitung++;
      maksimal--;
      digitalWrite(led, HIGH);
      delay(100);
      digitalWrite(led, LOW);
      lcd.setCursor(9,0);  
      lcd.print(Penghitung);
      lcd.setCursor(9,1);
      lcd.print(maksimal);
     }
      delay(50);

   }
   statusTerakhir = statusMasuk;
   statusKeluar = digitalRead(buttonKeluar);
   if (statusKeluar != statusTerakhir) {
     if (statusKeluar == HIGH)
     {
      Penghitung-=1;
      maksimal++;
      digitalWrite(led, HIGH);
      delay(100);
      digitalWrite(led, LOW);
      lcd.setCursor(9,0);
      lcd.print(Penghitung);
     }
         delay(50); 
         if (Penghitung > 0)
         {
            lcd.clear();
            delay(3);
            lcd.setCursor(1,0);  
            lcd.print("Total :");  
            lcd.setCursor(1,1);
            lcd.print("Sisa  : ");
            lcd.setCursor(9,0);
            lcd.print(Penghitung);
            lcd.setCursor(9,1);
            lcd.print(maksimal);
         }

         if (Penghitung <= 0)
         {         
            Penghitung = 0;
            maksimal = 20;
            lcd.setCursor(1,1);
            lcd.print("Sisa  : ");
            lcd.setCursor(9,1);
            lcd.print(maksimal);
            lcd.setCursor(9,0);
            lcd.print("KOSONG");
         }
         if (Penghitung >= maksimal)
         {
           Penghitung = 20;
           maksimal = 0;
           lcd.setCursor(9,0);
           lcd.print(Penghitung);
           lcd.setCursor(9,1);
           lcd.print("KOSONG");
           digitalWrite(led, HIGH);
           delay(5000);
           digitalWrite(led, LOW);
           delay(1000);
         } 
   }
   statusTerakhir = statusKeluar;
}

download library

https://drive.google.com/file/d/1XPGVEubM_ZQrO2OB4FDo51sIVWnDGeCI/view

 https://drive.google.com/file/d/1XPGVEubM_ZQrO2OB4FDo51sIVWnDGeCI/view

jika terjadi kesalahan eror code bisa lihat tutorial di you tube  bawah

https://youtu.be/9gI_lCXdA78

Komentar

Postingan populer dari blog ini

jam digital

teknik gulung dinamo