Hello everyone, I'm facing some issues with a project I'm working on.
Problem: When a coin is inserted into the Coin Acceptor, the program should detect it once and the Solenoid Door Lock should open for 5 seconds. However, instead, the Solenoid Door Lock is looping on its own and also randomly activating. I suspect this is caused by the 5V Relay even though I haven't touched it.
Question: What could be the solution to this issue?
Suggested Solution from My Teacher: Connect the VCC on the Relay directly to the 5V Adapter and connect the GND on the Relay to the GND of the Relay, Adapter, and Arduino Uno Board. It's will work ?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int coinPin = 2; // Pin to detect coins
const int relayPin = 11; // Pin for relay
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address and size 16x2
volatile boolean coinInserted = false; // Status if a coin is inserted
unsigned long balance = 0; // Initial balance
unsigned long lastInterruptTime = 0; // For debouncing
unsigned long unlockTime = 0; // Time when the lock is opened
const unsigned long unlockDuration = 5000; // Duration the lock stays open (5 seconds)
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Initialize LCD
lcd.backlight(); // Turn on the LCD backlight
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is off (lock is locked)
pinMode(coinPin, INPUT_PULLUP); // Coin pin uses internal pull-up
attachInterrupt(digitalPinToInterrupt(coinPin), coinInterrupt, RISING);
lcd.setCursor(0, 0);
lcd.print("Waiting for coin...");
}
void loop() {
if (coinInserted) {
noInterrupts(); // Avoid data conflicts with interrupt
coinInserted = false; // Reset flag
interrupts();
balance += 1; // Add to balance
Serial.print("Coin accepted! Balance: ");
Serial.println(balance);
// Display coin accepted message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Coin Accepted!");
lcd.setCursor(0, 1);
lcd.print("Total Coins: ");
lcd.print(balance);
// Unlock (activate relay)
digitalWrite(relayPin, HIGH);
unlockTime = millis(); // Record the time when the lock is opened
delay(2000); // Wait a moment to ensure the display is visible
}
if (millis() - unlockTime < unlockDuration && unlockTime != 0) {
// If the lock is open
lcd.setCursor(0, 1);
lcd.print("Door Open");
} else if (millis() - unlockTime >= unlockDuration && unlockTime != 0) {
// If the lock open time has elapsed
digitalWrite(relayPin, LOW); // Turn off relay (lock is locked)
unlockTime = 0; // Reset the open time
Serial.println("Door locked again.");
// Display lock message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Locked");
delay(1000); // Wait before returning to the initial state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting for coin...");
}
}
void coinInterrupt() {
unsigned long interruptTime = millis(); // Get current time
if (interruptTime - lastInterruptTime > 500) { // Debouncing (500 ms)
coinInserted = true; // Set flag when coin is detected
Serial.println("Interrupt detected, coin inserted!");
}
lastInterruptTime = interruptTime; // Save the last interrupt time
}