hi! i made a coin operated water dispenser ang got everything running:
step 1: arduino asks person to insert coin step 2: once specific no. of coins is inserted relay (pump & solenoid) turns on step 3: once specific time passed, relay turns off.
but sometimes when i get back to the step 1, even without inserting a coin, the arduino detects a coin inserted (sometimes it 5 coins worth sometimes 1 coin worth).
note:
-
the way i detect coins is through interrupt in pin d2 (read impulses) and give value to the impulses in the loop function.
-
i checked on an oscilloscope d2. after a cycle, when "please insert coin" amount is reduced (which i assume the interrupt was triggered since "coin_imp = coin_imp + 1" is only found in the interrupt) and there were no pulses detected from d2. also did digitalRead(2) and did not notice a 1 whenever i get back to step 1 and "please insert coin" amount is reduced.
-
im using a custom pcb with a relay without optocoupler (if thats important) but already put a diode on the dc jack where i insert my pump
-
i dont notice this problem when i dont insert a pump.
and heres the code:
//Variables
//Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Coin variables
float inserted_coin;
int coin_imp;
//Timers
unsigned long i; // coin impulse
unsigned long e; // dispensing time
unsigned long d; //button debounce
//Pins
int relay = 3;
//LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
//Money Counter
int pr_butt = 6; // button npin
float total;
float total_old;
//Pre-defined Values
//Price
float price = 10;
//Delays
unsigned long text_delay = 2000;
unsigned long text_blink_delay = 500;
unsigned long impread_delay = 5; //duration before reading total impulses then reseting impulse number
unsigned long dispense_delay = 1000; //water flow duration
void interrupt() {
coin_imp = coin_imp + 1;
i = 0;
}
void setup() {
//LCD
lcd.init();
lcd.backlight();
//Relay
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
//Interrupt
attachInterrupt(digitalPinToInterrupt(2), interrupt, RISING);
//Serial Monitor
Serial.begin(9600);
}
void loop() {
//Debug
Serial.println(coin_imp);
//Serial.println(digitalRead(2));
while (digitalRead(pr_butt) == HIGH) {
d++;
if ( d == 2) {
Serial.println(total);
delay(1000);
}
}
//Variable
float remaining_insert = price - inserted_coin;
//LCD
lcd.setCursor(5, 0);
lcd.print("Welcome to");
lcd.setCursor(7, 1);
lcd.print("AUQUA");
lcd.setCursor(4, 2);
lcd.print("Please Insert");
lcd.setCursor(7, 3);
lcd.print("P");
lcd.setCursor(8, 3);
lcd.print(remaining_insert);
i = i + 1;
// P1
if (i >= impread_delay && coin_imp == 1) {
inserted_coin = inserted_coin + 1;
i = 0;
coin_imp = 0;
}
// P5
else if (i >= impread_delay && coin_imp == 2) {
inserted_coin = inserted_coin + 5;
i = 0;
coin_imp = 0;
}
// P10
else if (i >= impread_delay && coin_imp == 3) {
inserted_coin = inserted_coin + 10;
i = 0;
coin_imp = 0;
}
if (remaining_insert <= 10 && i >= 5000) {
inserted_coin = 0;
}
if (remaining_insert <= 0) {
digitalWrite(relay, HIGH);
lcd.clear();
}
while (digitalRead(relay) == HIGH) {
e = e + 2000;
lcd.setCursor(4 , 2);
lcd.print("PLEASE WAIT ");
lcd.setCursor(5, 1);
lcd.print("DISPENSING");
delay(text_blink_delay);
lcd.setCursor(4 , 2);
lcd.print("PLEASE WAIT.");
lcd.setCursor(5, 1);
lcd.print(" ");
delay(text_blink_delay);
lcd.setCursor(4 , 2);
lcd.print("PLEASE WAIT..");
lcd.setCursor(5, 1);
lcd.print("DISPENSING");
delay(text_blink_delay);
lcd.setCursor(4 , 2);
lcd.print("PLEASE WAIT...");
lcd.setCursor(5, 1);
lcd.print(" ");
delay(text_blink_delay);
if (e >= dispense_delay) {
e = 0;
total_old = total;
total = total_old + inserted_coin;
inserted_coin = 0;
digitalWrite(relay, LOW);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("THANK YOU FOR USING");
lcd.setCursor(7, 2);
lcd.print("AUQUA");
delay(text_delay);
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("COME AGAIN");
lcd.setCursor(8, 2);
lcd.print("SOON");
delay(text_delay);
lcd.clear();
}
}
}




