Jumat, 13 Mei 2016

sketch arduino counter & kombinasi tombol

Di sini saya akan berbagi sketch arduino untuk menghitung naik dan turun menggunakan tombol dan dapat digeser kanan kiri pada kursornya yang ditampilkan pada LCD 16x2

// Nama: dedik suryani
// Nim : 04114045
// Nb  : kaki yang digunakan untuk LCD adalah 12,11,9,8,7,6






int sw1 = A0;
int sw2 = A1;
int sw3 = A4;
int sw4 = A5;
int count = 0;
int hitung = 0;
int geser = 0;
String tampil;
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,9,8,7,6);

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  tampil="";
  // Declare Switches as INPUT to Arduino
  pinMode(sw1,INPUT_PULLUP);
  pinMode(sw2,INPUT_PULLUP);
  pinMode(sw3,INPUT_PULLUP);
  pinMode(sw4,INPUT_PULLUP);
  digitalWrite(sw1,HIGH);
  digitalWrite(sw2,HIGH);
  digitalWrite(sw3,HIGH);
  digitalWrite(sw4,HIGH);
}

void loop()
{
  if(digitalRead(sw1) == LOW)   // jika sw1 ditekan
  {
    count++;                    // tambah 1 nilai
    lcd.setCursor(0, 1);
    lcd.print(count);
    delay(400);
  }
  if(digitalRead(sw2) == LOW)    // jika sw2 ditekan
  {
    count--;                     // kurangi 1 nilai
    if(count < 0)
      count = 0;
    lcd.setCursor(0, 1);
    lcd.print(count);
    delay(400);
  }
if(digitalRead(sw3) == LOW)   // jika sw3 ditekan
  {
    geser++;                    // geser kanan 1digit
    lcd.setCursor (geser,1);
    lcd.print(tampil);
    delay(400);
  }
   if(digitalRead(sw4) == LOW)    // jika sw4 ditekan
  {
    geser--;                     // geser kiri 1digit
    if(hitung < 0)
      hitung = 0;
    lcd.setCursor (geser,1);
    lcd.print(tampil);
    delay(400);
  }



copy code diatas lalu compile!!!

Tidak ada komentar:

Posting Komentar