#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int sensorPins[4] = {3,4,6,7};
const int nominal[4] = {100, 200, 500, 1000};
int lastState[4] = {HIGH, HIGH, HIGH, HIGH};
long total = 0;
// Stepper motor pins
#define dirPin 2
#define stepPin 5
#define enablePin 8
unsigned long lastStepTime = 0;
const unsigned long stepInterval = 1600; // mikrodetik
bool stepState = false;
// Debounce timing per sensor
unsigned long lastTriggerTime[4] = {0, 0, 0, 0};
const unsigned long debounceDelay = 300; // ms
void setup() {
lcd.begin(16, 2, LCD_5x8DOTS);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Total : 0");
for (int i = 0; i < 4; i++) {
pinMode(sensorPins[i], INPUT);
}
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW); // aktifkan driver
digitalWrite(dirPin, HIGH); // arah putar
}
void loop() {
// Stepper motor non-blocking
unsigned long currentMicros = micros();
if (currentMicros - lastStepTime >= stepInterval) {
lastStepTime = currentMicros;
stepState = !stepState;
digitalWrite(stepPin, stepState);
}
unsigned long currentMillis = millis();
// Baca sensor IR dengan debounce waktu
for (int i = 0; i < 4; i++) {
int currentState = digitalRead(sensorPins[i]);
if (currentState == LOW && lastState[i] == HIGH) {
// Cek debounce waktu
if (currentMillis - lastTriggerTime[i] > debounceDelay) {
total += nominal[i];
lastTriggerTime[i] = currentMillis;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Total : ");
lcd.print(total);
}
}
lastState[i] = currentState;
}
}
Loading
cd74hc4067
cd74hc4067