Coin operated water dispenser help!

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:

  1. the way i detect coins is through interrupt in pin d2 (read impulses) and give value to the impulses in the loop function.

  2. 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.

  3. 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

  4. i dont notice this problem when i dont insert a pump.

here is the pcb schematic

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();
    }

  }


}

Your topic has been moved here as this forum section is more appropriate than where it was originally posted.

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

life is giving you a hint on where to dig...
without code and clear doc on how things are wired and powered, we can't help

1 Like

A couple of concepts…

EMI from the motor/relay… power supply & noise injection

Interrupts are fine if you need them- we’ll see.

Is your input pin pulled up or down to avoid floating states?

1 Like

seems like i cant attach anything to this reply since im new but heres a photo of my pcb cowd - Album on Imgur

side note: the coin pin in the schematic is pulled down but it should be pulled up. i fixed that by removing the resistor and hooking jumper wires from d2 to 5v with a 10k resistor in between

what other information can i give?

  1. EMI from the motor/relay… power supply & noise injection
  • is that another term for reverse emf? i did put in a diode on the jack where i put my pump but it still has that problem (please insert coin amount going down without isnerting a coin) though it was worse before i think because when i print on the serial monitor the variable where i store my impulses (coin_imp) it would go more than 10 before i put a diode(4001).
  1. Interrupts are fine if you need them- we’ll see.

so my interrupt pin is pin d2 and my relay is on pin d3. is it possible that the noise from d3 trigger the interrupt?

  1. Is your input pin pulled up or down to avoid floating states?

its pulled up.

  1. Noise or voltage sag can affect your signal and power lines in a number of ways. Your own board - decoupling capacitors… we still need to see schematic and assembly photos to guess at the problems.

  2. Interrupts can be tricky, often not needed if your code is written properly.
    Interrupts are for very specific requirements.

  3. Good.

1 Like

Do you have any data on the pump you are using? Power consumption, what input voltage you are using etc?

1 Like

  1. ok. here a picture of my pcb schematic

and heres the actual one the red line is the diode

note: the only difference i made in the actual pcb is that the resistor on the "coin" pin is pulled up instead of down and that i put a diode (4001) between the + and - (cathode on +) pin of the dc jack for the pump.

  1. should i just then use this instead? (i used i var instead of millis because my knock off arduino's millis is crazy i think. it doesnt have a uniform increment.)
i ++

if (digitalRead(2) == HIGH) {
coin_imp + 1
i = 0
}

if (i >= 30 && coin_imp == 1) {
inserted_coin = inserted_coin + 1
i = 0
}

the one im using right now to simulate is only 12v 4.2w.

im using a 12v 4.5A universal laptop charger to power it.

If those thin red wires are for your board to the relays then you may be giving off unwanted/spurious signals.
Thicker and shielded would be a good start.

1 Like

Hi,
@spycbeef schematic.

Unfortunately its a search a word schematic, ok for PCB production, but not for troubleshooting.

Tom.... :grinning: :+1: :coffee: :australia:

Hi,
Your circuit shows no bypass capacitors, especially at your Nano power pins or coin counter or hopper inputs.

Can you please post an exported jpg image(s) of your PCB pattern please?
We need to see where your signal tracks and power tracks go.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

1 Like

Here's what it actually looks like.

is this not going to keep current from flowing backwads?

sorry im not really well bersed in electronics and i just started learning.

and is that whats triggering my interrupt?

Is this it?

What’s feeding the interrupt input pin..

Is it a clean ‘denounced’ signal, or just spring contacts from whatever device is attached ?

Contact bounce is a thing.

This is one issue with using interrupts - they’re very fast, and you need to be ready for bursts of unwanted pin changes.
How you handle them is up to you, or your source device might be able to help.

Possibly adding a small capacitor on the input pin may help quench some of those ‘extra’ pulses.

1 Like

Which is extraordinarily improbable.

No visible workable 5 V supply for the relays.

Obvious cause of problems.

Unreliable.

Just sayin'! :roll_eyes:

1 Like