Summary (Arduino Nano + L298N feeder + LDR + MAX9814 mic)
I'm building a MTG Card scanner but I'm stuck on my engine that is not turning off and restarting when it should and the Scanner keeps feeding cards.
Here's a short video of the expected behaviour for the scanner/feeding mechanism
The original design (from the engineer who designed the machine) uses:
- L298N H-bridge for a DC motor feeder (ENA=D9, IN1=D8, IN2=D7)
- LDR sensor on A7 with an LED shining at it to detect card position
- TM1637 4-digit display + NeoPixel ring, buzzer, 2 buttons (start/stop toggle + counter reset)
- Microphone input originally expected a digital “sound detected” signal (D0 HIGH) to trigger feeding after a phone “scan beep”
What works
- Arduino Nano + IDE connectivity is OK (COM port, uploads fine).
- Motor driver wiring is correct: a minimal test sketch toggling motor ON/OFF every 2 seconds works perfectly (so L298N + motor + ENA/IN pins are wired correctly and stopping the motor via IN1/IN2 LOW works).
- LDR readings look reasonable when tested manually: LED aimed at LDR gives ~500–600, covering with a card drops to ~20–150.
Expected Behaviour - Step by Step
Based on the original design video, here's what SHOULD happen:
1. STARTUP
- LED ring turns RED
- LDR baseline calibrated (~500-600 with no card blocking)
- Display shows "0000"
- Motor OFF, waiting for button press
2. USER PRESSES START BUTTON (D6)
scannerActive = true- Motor STARTS (feeds first card toward scan position)
- LED stays RED
3. CARD REACHES LDR SENSOR
- LDR value DROPS (e.g., 500 → 150) as card blocks light
cardInPosition = true- Code detects:
ldrValue < baseLdrValue - 50
4. CARD PASSES LDR (exits scan position) THIS IS WHERE IT FAILS
- LDR value RISES back (e.g., 150 → 500)
- Code detects:
ldrValue > baseLdrValue - 30 - Motor STOPS
cardInPosition = false- Card is now in position for phone camera to scan
5. WAITING FOR PHONE BEEP
- Motor remains OFF
- Scanner waits for phone app to recognize card
- Phone app plays SUCCESS BEEP when card identified
6. MICROPHONE DETECTS BEEP
micValuespikes above threshold (~360)soundDetected = true(edge trigger, not continuous)- Card count increments, display updates
- Motor STARTS again (feeds next card)
7. REPEAT STEPS 3-6 FOR EACH CARD
8. TIMEOUT (no cards for 8 seconds)
- Motor STOPS
- LED BLINKS RED + Buzzer beeps
timeoutMode = true
What's Actually Happening
Step 4 fails: The motor doesn't stop because the LDR value never returns to baseline between cards during continuous feeding. It just keeps feeding too fast.
Result: Steps 3→4→5→6 collapse into one continuous feed:
Card1 enters → Card2 enters → Card3 enters → ... (motor never stops)
The condition ldrValue > baseLdrValue - 30 is never met because:
- Cards pass too quickly for LDR to recover
- Or the next card is already blocking before the previous one clears
- Or ambient light / LED state changes have shifted the baseline
Microphone changes / current status
- The original mic modules were unreliable (stuck HIGH / weird values), so I replaced them with a MAX9814 (wired VDD→5V, GND→GND, OUT→A2). Board has no indicator LEDs (expected).
- MAX9814 analog values at idle are ~250–300 and claps/phone beep jump to ~400–500 (not huge spikes like 700+). I implemented analog threshold + edge detection in code (instead of digitalRead), but the motor-stop issue persists even when the microphone is fully disabled (soundDetected forced false) → so the core issue seems to be LDR/card-position logic under real feeding conditions rather than mic triggering.
What I’m looking for help with
- Best practice for using an LED+LDR as a card position sensor in a feeder where cards pass quickly and ambient/stack effects prevent the signal returning to baseline between cards.
- How to robustly detect “one card has passed / reached position” (e.g., hysteresis, tracking min/max, relative thresholds, debouncing, time-based gating).
- Optional: controlling feeder speed via ENA PWM to make LDR detection reliable.
PS the movie link posted above contains the design of the device.
Annotated Diagram
Current Sketch
#include <TM1637Display.h>
#include <Adafruit_NeoPixel.h>
// Pins
const int ENA = 9;
const int IN1 = 8;
const int IN2 = 7;
const int buzzerPin = 11;
const int ldrPin = A7;
const int buttonResetPin = 3;
const int buttonManualTrigger = 6;
const int micPin = A2;
const int displayClkPin = 4;
const int displayDioPin = 5;
// Initialize display
TM1637Display display(displayClkPin, displayDioPin);
// Initialize Neopixel LED
Adafruit_NeoPixel ledRing(8, A0, NEO_GRB + NEO_KHZ800);
// Variables
int cardCount = 0;
bool motorRunning = false;
bool scannerActive = false;
bool cardInPosition = false;
bool timeoutMode = false;
unsigned long lastButtonPress = 0;
unsigned long lastResetPress = 0;
unsigned long lastCardDetectedTime = 0;
unsigned long lastCardMovedTime = 0;
const unsigned long timeoutDuration = 8000;
const unsigned long cardCountDelay = 1000;
unsigned long lastCardCountTime = 0;
int baseLdrValue;
// Lighting control
const int reasonBrightness = 255;
unsigned long dimmingStartTime = 0;
const unsigned long dimmingDuration = 5000;
bool isDimmed = false;
unsigned long colorFadeTime = 0;
int fadeStep = 0;
// --- MAX9814 mic detection (analog on A2) ---
int micThreshold = 360; // tune later (based on your readings)
bool lastSoundState = false;
unsigned long lastSoundTriggerTime = 0;
const unsigned long soundCooldown = 400; // ms between triggers
// --- Optional: slow debug prints (Serial Monitor readable) ---
unsigned long lastDebugPrint = 0;
const unsigned long debugInterval = 200; // print every 200ms
//Startup sequence
const int startup = 5; // Select the desired start sequence (1..5)
void setup() {
Serial.begin(9600);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonResetPin, INPUT_PULLUP);
pinMode(buttonManualTrigger, INPUT_PULLUP);
pinMode(ldrPin, INPUT);
pinMode(micPin, INPUT);
ledRing.begin();
setLightRed(reasonBrightness); // Calibrate LDR with red light
ledRing.show();
digitalWrite(ENA, HIGH);
display.setBrightness(0x0f);
display.showNumberDec(cardCount);
delay(500);
baseLdrValue = analogRead(ldrPin);
Serial.print("Basis LDR-Wert: ");
Serial.println(baseLdrValue);
delay(500);
// Selecting the start sequence
switch (startup) {
case 1: startupSequence1(); break;
case 2: startupSequence2(); break;
case 3: startupSequence3(); break;
case 4: startupSequence4(); break;
case 5: startupSequence5(); break;
default:
Serial.println("UngĂĽltige Startsequenz. Standard wird verwendet.");
startupSequence1();
break;
}
}
void loop() {
int ldrValue = analogRead(ldrPin);
// --- Microphone (MAX9814) analog detection ---
int micValue = analogRead(micPin);
bool soundState = (micValue > micThreshold);
bool soundDetected = false;
// Trigger only on LOW->HIGH edge + cooldown (prevents constant retriggering)
if (soundState && !lastSoundState &&
millis() - lastSoundTriggerTime > soundCooldown) {
soundDetected = true;
lastSoundTriggerTime = millis();
}
lastSoundState = soundState;
// --- Optional debug (comment out if not needed) ---
if (millis() - lastDebugPrint > debugInterval) {
lastDebugPrint = millis();
Serial.print("LDR: ");
Serial.print(ldrValue);
Serial.print(" | base: ");
Serial.print(baseLdrValue);
Serial.print(" | MIC: ");
Serial.print(micValue);
Serial.print(" | soundState: ");
Serial.print(soundState);
Serial.print(" | soundDetected: ");
Serial.println(soundDetected);
}
// Start button: Switching the scanner
if (digitalRead(buttonManualTrigger) == LOW) {
if (millis() - lastButtonPress > 300) {
scannerActive = !scannerActive;
lastButtonPress = millis();
if (scannerActive) {
setLightRed(255);
moveCard();
timeoutMode = false;
lastCardDetectedTime = millis();
lastCardMovedTime = millis();
} else {
stopMotor();
timeoutMode = false;
isDimmed = false;
}
ledRing.show();
}
}
// Motor control
if (!scannerActive && motorRunning) {
stopMotor();
}
// Reset button: Reset counter
if (digitalRead(buttonResetPin) == LOW && millis() - lastResetPress > 300) {
cardCount = 0;
display.showNumberDec(cardCount);
lastResetPress = millis();
delay(500);
}
// When the scanner is active
if (scannerActive) {
if (!cardInPosition && ldrValue < baseLdrValue - 50) {
cardInPosition = true;
lastCardDetectedTime = millis();
// Serial.println("Card is promoted");
}
if (cardInPosition && ldrValue > baseLdrValue - 30) {
stopMotor();
setLightRed(255);
delay(50);
baseLdrValue = analogRead(ldrPin);
cardInPosition = false;
// Serial.println("Card is scanned");
}
if (millis() - lastCardMovedTime >= 3000 && !cardInPosition && !timeoutMode) {
isDimmed = true;
}
if (soundDetected) {
isDimmed = false;
setLightRed(255);
moveCard();
if (!timeoutMode && millis() - lastCardCountTime > cardCountDelay) {
lastCardCountTime = millis();
cardCount++;
display.showNumberDec(cardCount);
}
lastCardMovedTime = millis();
}
if (millis() - lastCardMovedTime > timeoutDuration && !timeoutMode) {
timeoutMode = true;
isDimmed = false;
stopMotor();
tone(buzzerPin, 1000, 500);
// Serial.println("Timeout Mode");
}
}
// Timeout mode flashes
if (timeoutMode) {
blinkRedLEDAndSoundBuzzer();
}
// Pause mode - lights change smoothly
if (!scannerActive && !timeoutMode) {
fadeLights();
}
if (isDimmed) {
dimLights();
}
}
void moveCard() {
if (scannerActive && !timeoutMode) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
motorRunning = true;
}
}
void stopMotor() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
motorRunning = false;
}
void blinkRedLEDAndSoundBuzzer() {
static unsigned long lastBlinkTime = 0;
unsigned long currentMillis = millis();
if (currentMillis - lastBlinkTime >= 500) {
lastBlinkTime = currentMillis;
static bool ledState = false;
ledState = !ledState;
if (ledState) {
setLightRed(255);
tone(buzzerPin, 1000, 100);
} else {
ledRing.clear();
noTone(buzzerPin);
}
ledRing.show();
}
}
void dimLights() {
unsigned long elapsedTime = millis() - dimmingStartTime;
int brightness = map(elapsedTime, 0, dimmingDuration, 255, 0);
setLightRed(brightness);
ledRing.show();
}
void fadeLights() {
unsigned long currentMillis = millis();
if (currentMillis - colorFadeTime >= 2000) {
colorFadeTime = currentMillis;
switch (fadeStep % 6) {
case 0: ledRing.fill(ledRing.Color(255, 0, 0)); break;
case 1: ledRing.fill(ledRing.Color(255, 127, 0)); break;
case 2: ledRing.fill(ledRing.Color(255, 255, 0)); break;
case 3: ledRing.fill(ledRing.Color(0, 255, 0)); break;
case 4: ledRing.fill(ledRing.Color(0, 0, 255)); break;
case 5: ledRing.fill(ledRing.Color(75, 0, 130)); break;
}
ledRing.show();
fadeStep++;
}
}
void setLightRed(int brightness) {
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(brightness, 0, 0));
}
ledRing.show();
}
// --- Startup sequences (unchanged) ---
void startupSequence1() {
ledRing.clear();
ledRing.show();
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(255, 0, 0));
ledRing.show();
tone(buzzerPin, 1000, 100);
delay(100);
}
delay(200);
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(0, 255, 0));
ledRing.show();
tone(buzzerPin, 1200, 100);
delay(100);
}
delay(200);
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(0, 0, 255));
ledRing.show();
tone(buzzerPin, 1400, 100);
delay(100);
}
delay(200);
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(255, 255, 255));
ledRing.show();
tone(buzzerPin, 1600, 100);
delay(100);
}
delay(500);
ledRing.fill(ledRing.Color(255, 255, 255));
ledRing.show();
tone(buzzerPin, 2000, 300);
delay(300);
ledRing.clear();
ledRing.show();
noTone(buzzerPin);
}
void startupSequence2() {
ledRing.clear();
ledRing.show();
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(255, 165, 0));
ledRing.show();
tone(buzzerPin, 800 + (i * 50), 100);
delay(100);
}
delay(300);
for (int j = 0; j < 3; j++) {
for (int brightness = 0; brightness <= 255; brightness += 5) {
ledRing.fill(ledRing.Color(brightness, brightness / 2, 0));
ledRing.show();
delay(10);
}
for (int brightness = 255; brightness >= 0; brightness -= 5) {
ledRing.fill(ledRing.Color(brightness, brightness / 2, 0));
ledRing.show();
delay(10);
}
tone(buzzerPin, 1200, 150);
delay(150);
}
for (int i = 0; i < 3; i++) {
ledRing.fill(ledRing.Color(255, 255, 255));
ledRing.show();
tone(buzzerPin, 2000, 100);
delay(100);
ledRing.clear();
ledRing.show();
noTone(buzzerPin);
delay(100);
}
}
void startupSequence3() {
ledRing.clear();
ledRing.show();
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(255, 0, 255));
ledRing.show();
tone(buzzerPin, 800 + (i * 30), 50);
delay(50);
}
noTone(buzzerPin);
delay(300);
int half = ledRing.numPixels() / 2;
for (int i = 0; i <= half; i++) {
ledRing.setPixelColor(half - i, ledRing.Color(0, 255, 255));
ledRing.setPixelColor(half + i, ledRing.Color(0, 255, 255));
ledRing.show();
tone(buzzerPin, 1000 + (i * 40), 80);
delay(80);
}
noTone(buzzerPin);
delay(300);
for (int j = 0; j < 3; j++) {
ledRing.fill(ledRing.Color(0, 0, 255));
ledRing.show();
tone(buzzerPin, 1500, 100);
delay(200);
ledRing.fill(ledRing.Color(0, 0, 100));
ledRing.show();
delay(200);
}
noTone(buzzerPin);
delay(300);
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(255, 255, 255));
ledRing.setPixelColor((i + ledRing.numPixels() / 2) % ledRing.numPixels(), 0);
ledRing.show();
tone(buzzerPin, 2000 + (i * 20), 30);
delay(30);
}
noTone(buzzerPin);
delay(300);
ledRing.fill(ledRing.Color(255, 255, 255));
ledRing.show();
tone(buzzerPin, 2500, 150);
delay(150);
ledRing.clear();
ledRing.show();
noTone(buzzerPin);
}
void startupSequence4() {
Serial.println("Starte Startup-Sequenz 4");
ledRing.clear();
ledRing.show();
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(255, 0, 255));
ledRing.show();
tone(buzzerPin, 800 + (i * 30), 50);
delay(50);
}
noTone(buzzerPin);
delay(300);
int half = ledRing.numPixels() / 2;
for (int i = 0; i <= half; i++) {
ledRing.setPixelColor(half - i, ledRing.Color(0, 255, 255));
ledRing.setPixelColor(half + i, ledRing.Color(0, 255, 255));
ledRing.show();
tone(buzzerPin, 1000 + (i * 40), 80);
delay(80);
}
noTone(buzzerPin);
delay(300);
for (int j = 0; j < 3; j++) {
ledRing.fill(ledRing.Color(0, 0, 255));
ledRing.show();
tone(buzzerPin, 1500, 100);
delay(200);
ledRing.fill(ledRing.Color(0, 0, 100));
ledRing.show();
delay(200);
}
noTone(buzzerPin);
delay(300);
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(255, 255, 255));
ledRing.setPixelColor((i + ledRing.numPixels() / 2) % ledRing.numPixels(), 0);
ledRing.show();
tone(buzzerPin, 2000 + (i * 20), 30);
delay(30);
}
noTone(buzzerPin);
delay(300);
ledRing.fill(ledRing.Color(255, 255, 255));
ledRing.show();
tone(buzzerPin, 2500, 150);
delay(150);
ledRing.clear();
ledRing.show();
noTone(buzzerPin);
}
void startupSequence5() {
ledRing.clear();
ledRing.show();
for (int i = 0; i < ledRing.numPixels(); i++) {
ledRing.setPixelColor(i, ledRing.Color(0, 0, 255));
ledRing.show();
tone(buzzerPin, 800 + (i * 20), 50);
delay(50);
ledRing.setPixelColor(i, 0);
}
noTone(buzzerPin);
delay(300);
int half = ledRing.numPixels() / 2;
for (int i = half; i >= 0; i--) {
ledRing.setPixelColor(half - i, ledRing.Color(255, 255, 0));
ledRing.setPixelColor(half + i, ledRing.Color(255, 255, 0));
ledRing.show();
tone(buzzerPin, 1000 + (i * 50), 50);
delay(80);
}
noTone(buzzerPin);
delay(300);
for (int j = 0; j < 3; j++) {
ledRing.fill(ledRing.Color(255, 255, 255));
ledRing.show();
tone(buzzerPin, 1500, 100);
delay(200);
ledRing.clear();
ledRing.show();
delay(200);
}
noTone(buzzerPin);
delay(300);
for (int i = 0; i <= half; i++) {
ledRing.setPixelColor(half - i, 0);
ledRing.setPixelColor(half + i, 0);
ledRing.show();
tone(buzzerPin, 2000 - (i * 50), 50);
delay(80);
}
noTone(buzzerPin);
delay(300);
for (int i = 0; i < 2; i++) {
ledRing.fill(ledRing.Color(255, 0, 0));
ledRing.show();
tone(buzzerPin, 2500, 150);
delay(150);
ledRing.clear();
ledRing.show();
noTone(buzzerPin);
delay(150);
}
}```
