#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
const int BUTTON_PIN = 7; // the number of the pushbutton pin
const int SHORT_PRESS_TIME = 5000; // 500 milliseconds
// Variables will change:
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
pinMode(dirPin, OUTPUT); // Atur dirPin sebagai Output
pinMode(stepPin, OUTPUT); // Atur stepPin sebagai Output
}
void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
if(lastState == HIGH && currentState == LOW) // button is pressed
pressedTime = millis();
else if(lastState == LOW && currentState == HIGH) { // button is released
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration > SHORT_PRESS_TIME )
Serial.println("A long press is detected");
for (int i = 0; i < 1.5 * stepsPerRevolution; i++) {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(3000);
digitalWrite(stepPin, LOW);
delayMicroseconds(3000);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
}
}
// save the the last state
lastState = currentState;
}
// // int ledMerah = 4;
// static const int buttonPin = 7; // Pin untuk tombol
// int buttonStatePrevious = LOW; // previousstate tombol
// unsigned long minButtonLongPressDuration = 3000; // Waktu jeda min untuk long press button
// unsigned long buttonLongPressMillis; // Waktu dalam ms tombol kita telah ditekan
// bool buttonStateLongPress = false; // True ketika keadaan tombol long press
// const int intervalButton = 50; // Time between two readings of the button state
// unsigned long previousButtonMillis; // Timestamp pada pembacaan terakhir (dalam tipe data Unsigned Long tidak menyimpan angka minus)
// unsigned long buttonPressDuration; // Waktu ketika tombol ditekan dalam milisekon
// //// GENERAL ////
// unsigned long currentMillis; // Variabel untuk menyimpan/mengumpulkan jumlah milidetik sejak Arduino dimulai
// void setup() {
// Serial.begin(9600); // Inisialisasi serial monitor
// pinMode(buttonPin, INPUT); // Atur buttonPin sebagai Input
// // pinMode(ledMerah, OUTPUT); // Atur ledMerah sebagai Output
// pinMode(dirPin, OUTPUT); // Atur dirPin sebagai Output
// pinMode(stepPin, OUTPUT); // Atur stepPin sebagai Output
// Serial.println("Press button");
// }
// // Function for reading the button state
// void readButtonState() { // Membuat program fungsi pembacaan status tombol
// // Jika perbedaan waktu antara pembacaan sebelumnya lebih besar dari intervalButton, MAKA
// if(currentMillis - previousButtonMillis > intervalButton) {
// // Membaca nilai digital buttonPin 0/1 (LOW/HIGH)
// int buttonState = digitalRead(buttonPin);
// // Jika tombol telah ditekan (AND)
// // Jika tombol tidak ditekan sebelumnya (AND)
// // JIKA belum ada pengukuran yang berjalan untuk menentukan berapa lama tombol (buttonPin)telah ditekan
// if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
// buttonLongPressMillis = currentMillis;
// buttonStatePrevious = HIGH;
// Serial.println("Button pressed"); // Maka akan tampil pada serial monitor menandakan tombol ditekan (walaupun hanya sesaat menekan)
// }
// // Menghitung lama penekanan tombol
// buttonPressDuration = currentMillis - buttonLongPressMillis; // Lama penekanan tombol = waktu sekarang -
// // If the button is pressed AND
// // If there is no measurement running to determine how long the button is pressed AND
// // If the time the button has been pressed is larger or equal to the time needed for a long press
// if (buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
// buttonStateLongPress = true;
// Serial.println("Button long pressed");
// for (int i = 0; i < 1.5 * stepsPerRevolution; i++) {
// digitalWrite(dirPin, HIGH);
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(3000);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(3000);
// digitalWrite(dirPin, LOW);
// digitalWrite(stepPin, LOW);
// }
// }
// // Jika Tombol telah dilepas [0] (AND)
// // Jika Tombol telah ditekan sebelumnya [1]
// if (buttonState == LOW && buttonStatePrevious == HIGH) {
// buttonStatePrevious = LOW;
// buttonStateLongPress = false;
// Serial.println("Button released");
// // Jika tidak ada pengukuran yang berjalan untuk menentukan berapa lama tombol ditekan (AND)
// // Jika lama waktu penekanan tombol kurang dari (<) durasi tekan lama tombol
// // Catatan: The video shows:
// // Jika (!buttonStateLongPress && buttonPressDuration < minButtonLongPressDuration) {
// // Sejak buttonStateLongPress diatur FALSE pada line 75, !buttonStateLongPress akan selalu TRUE
// // dan dapat dihilangkan.
// if (buttonPressDuration < minButtonLongPressDuration) {
// Serial.println("Button pressed shortly");
// for (int i = 0; i < 1 * stepsPerRevolution; i++) {
// digitalWrite(dirPin, HIGH);
// digitalWrite(stepPin, HIGH);
// delayMicroseconds(400);
// digitalWrite(stepPin, LOW);
// delayMicroseconds(400);
// digitalWrite(dirPin, LOW);
// digitalWrite(stepPin, LOW);
// }
// }
// }
// // Menyimpan current timestamp di previousButtonMillis
// previousButtonMillis = currentMillis;
// }
// }
// void loop() {
// currentMillis = millis(); // Menyimpan/menampung waktu sekarang (current time)
// readButtonState(); // Fungsi pembacaan status tombol
// }