// Include the AccelStepper Library
#include <AccelStepper.h>
// Define pin connections
const int dirPin = 18;
const int stepPin = 19;
const int buzzer = 5;
// const int reset_ = 25;
// const int switch_ = 4;
const int relay = 13;
const int gas = 34;
unsigned long previousMillis = 0;
unsigned long interval = 1000; // Interval dalam milidetik (ms)
int countdownTime = 10; // Waktu hitung mundur dalam detik
int tim = 10;
int gas_jalan = 0;
String command;
// Creates an instance
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
Serial.begin(115200);
// pinMode(switch_, INPUT_PULLUP);
// pinMode(reset_, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(relay, LOW);
digitalWrite(buzzer, LOW);
Serial.println("Type Command (bayar, habis)");
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(2000);
myStepper.setAcceleration(20000);
myStepper.setSpeed(2000);
}
void loop() {
if (Serial.available()) {
command = Serial.readStringUntil('\n');
command.trim();
unsigned long currentMillis = millis();
if (command.equals("bayar")) {
digitalWrite(relay, HIGH);
int gas_jalan = analogRead(gas);
// Serial.println(gas_jalan);
// delay(500);
// Change direction once the motor reaches target position
myStepper.moveTo(gas_jalan);
// Move the motor one step
myStepper.run();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
countdownTime--;
Serial.print("Countdown: ");
Serial.println(countdownTime);
if (countdownTime <= 0) {
Serial.println("Countdown Selesai");
// Lakukan aksi setelah hitung mundur selesai
// Contoh: mengirimkan sinyal, mengendalikan perangkat, dll.
tone(buzzer, 200);
// Set ulang hitung mundur ke nilai awal
// countdownTime = 10;
while (true) {
// Tetap dalam loop tanpa melakukan apa-apa
}
}
}
}
else if (command.equals("habis")) {
noTone(buzzer);
digitalWrite(relay, LOW);
}
else {
Serial.println("bad command");
}
Serial.print("Command: ");
Serial.println(command);
}
}