#include<LiquidCrystal_I2C.h>
#define LED1 3 //pin 13 bernama LED
#define tombol 2 //pin 2 (pin interrupt) bernama tombol
#define LED2 4
#define front 12
#define back 13
static int count = 0;
unsigned long previousMillis = 0; // variabel untuk menyimpan waktu terakhir motor berjalan atau berhenti
const unsigned long motorInterval = 20000; // interval total waktu untuk motor (5 detik + 3 detik + 5 detik + 7 detik = 20 detik)
LiquidCrystal_I2C lcd(0x27, 20, 4);
void tombol_ditekan()
{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
//delay(1000);
digitalWrite(front, LOW);
digitalWrite(back, LOW);
delay(3000);
count = 0;
}
void setup() {
lcd.init();
lcd.backlight();
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(front,OUTPUT);
pinMode(back,OUTPUT);
pinMode(tombol,INPUT);
attachInterrupt(digitalPinToInterrupt(2),tombol_ditekan, FALLING); //pin 2 (interrupt 0) digunakan sebagai interrupt eksternal
}
void loop() { //Main Program
unsigned long currentMillis = millis();
unsigned long elapsedMillis = currentMillis - previousMillis;
if (elapsedMillis < 5000) { // Motor berputar ke kanan selama 5 detik
digitalWrite(front, HIGH);
digitalWrite(back, LOW);
} else if (elapsedMillis < 8000) { // Motor mati selama 3 detik
digitalWrite(front, LOW);
digitalWrite(back, LOW);
} else if (elapsedMillis < 13000) { // Motor berputar ke kiri selama 5 detik
digitalWrite(front, LOW);
digitalWrite(back, HIGH);
} else if (elapsedMillis < 20000) { // Motor mati selama 7 detik
digitalWrite(front, LOW);
digitalWrite(back, LOW);
}else {
digitalWrite(front, HIGH);
digitalWrite(back, LOW);
}
int nilai;
nilai = 50+count;
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
lcd.setCursor(0,0);
lcd.print("Count:");
lcd.setCursor(0,1);
lcd.print(nilai);
count -- ;
// menambahkan integer count dengan angka 1
if (nilai <= 00){
count =0;
}
delay(1000);
}
Tidak ada komentar:
Posting Komentar