Coin slot, push button and a water pump for a soda vending machine.. Need Help

I have a coding problem. My project is a soda vending machine that has 3 buttons for the corresponding cup sizes: small, medium, and large. The process would be when a certain amount of coins are inserted and pressed the desired size, the 9v water pump will proceed to dispense for a specific amount of time.

I used the code here http://www.instructables.com/id/Make-Money-with-Arduino/ , modified to the equivalent of my country's coin value to make my coin slot work but it doesn't seem to recognize other type of coins other than one. Tried them on serial monitor and sometimes it gives random output like when I insert P1, it would give 2 P1 and when I insert P5, it would give like 8 of P1.

I also have a problem in making push buttons work after a coin slot is inserted. I also assume that the 9v water pump works like an LED as it only has positive and negative wires.

I'm not really good with codes and my project's due in a week. Kinda noob but willing to learn so sorry and thank you.

Here's my code:

const int coinInt = 0; 
const int button1 = 4;
int buttonstate1 = 0;
volatile float coinsValue = 0.00;
int coinsChange = 0;                  

void setup()
{
  Serial.begin(9600);                 
  attachInterrupt(coinInt, coinInserted, RISING);   
pinMode(button1,INPUT_PULLUP);
buttonstate1 = digitalRead(button1);

}

void coinInserted()    
{
  coinsValue = coinsValue + 1;  
  coinsChange = 1;                           
}

void loop()
{
  if(coinsChange == 1)          
  {
    coinsChange = 0;              
 
 
    if (coinsValue == 1 && buttonstate1 == HIGH)
  {
  digitalWrite(13, HIGH);
  delay(10000);       
  digitalWrite(13, LOW);   
  coinsValue = 0;
  }
   if (coinsValue == 5 && buttonstate1 == HIGH)
  {
  digitalWrite(7, HIGH);  
  delay(10000);              
  digitalWrite(7, LOW);
  coinsValue = 0;
  }
  }
}

Why is coinsValue a float type?

aarg:
Why is coinsValue a float type?

for decimal purposes I think, does it affect the code?

I also have a problem in making push buttons work after a coin slot is inserted

No surprise there as the button input is only read in setup()

UKHeliBob:
No surprise there as the button input is only read in setup()

I am kinda new to coding arduino so I really appreciate noticing the errors. I don't know what to do, I need help. Thank you for noticing it btw. :smiley:

You need to keep count of the number of coins inserted and their value. Then, when it is time to dispense the drink read the button input and if it is pressed dispense the appropriate drink.

If the coin reader will not reliably read the number and/or type of coins inserted then you are probably not going to be able to make this work easily.

New to coding, project due in a week... just curious: when did you get that assignment?

UKHeliBob:
You need to keep count of the number of coins inserted and their value. Then, when it is time to dispense the drink read the button input and if it is pressed dispense the appropriate drink.

This is actually my problem in the code. My coin slot can keep count of the coins and their value. The coin slot has a 7 segment display which shows what coin I insert and its amount. My problem is the output the arduino shows when I insert a coin. If I insert a P5 coin, it gives random response. With that, I only assume my problem is in the code.

J-M-L:
New to coding, project due in a week... just curious: when did you get that assignment?

The project has given a go signal 2 days ago after I proposed it to my professor. I will give full time to it as possible. I think I still have 10 days to finish this. Sorry I exaggerated that "week"

Hi,
How many different value coins can you use.
Just P5 or other values.

Tom.... :slight_smile:

TomGeorge:
Hi,
How many different value coins can you use.
Just P5 or other values.

Tom.... :slight_smile:

I can use up to 3, My coin slot can read P1 (1 pesos), P5 and P10. They are all accurately read by the coin slot when I inserted them.

Hi,
Okay so you have 3 types of coins, how will the Arduino know if the amount payed is correct if it is just counting coins, not their value?

Tom... :slight_smile:

I see coinsValue is declared volatile. Shouldn't coinsChange be too?

TomGeorge:
Hi,
Okay so you have 3 types of coins, how will the Arduino know if the amount payed is correct if it is just counting coins, not their value?

Tom... :slight_smile:

The coin slot can determine the value of the coin. It sends pulses equivalent to the value of the coin.

UPDATE: I got the coin slot working, thanks to a video I watched on github. Now my only problem is the push button. If I get 1 credit then pressed a button, it is supposed to activate the water pump, dispensing the soda. Can I get some modification codes to achieve that? Here's my current code right now. ledpin is designated to give blink equivalent to the pulse recieved and ledpin2 is designated for the output whenever I get 1 credit.

// Constants
const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 15;
const int ledpin2 = 4;

// Variables
volatile int cents = 0;
int credits = 0;

// Setup
void setup() {
  
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
  pinMode(ledpin, OUTPUT);
  
}

// Main loop
void loop() {
  
  // If we've hit our target amount of coins, increment our credits and reset the cents counter
  if (cents >= targetcents) {
    credits = credits + 1;
    cents = cents - targetcents;
  }

  // If we haven't reached our target, keep waiting...
  else {
  }

  // Debugging zone
  Serial.print(cents);
  Serial.print(" cents toward current credit and ");
  Serial.print(credits);
  Serial.println(" credit(s) earned so far.");
  delay(1000);

  // Turn off LED
  digitalWrite(ledpin, LOW);

  // Now, write your own cool code here that triggers an event when the player has credits!
  if (credits > 0) {
    digitalWrite(ledpin2, HIGH);
    delay(10000);
    digitalWrite(ledpin2, LOW);
    credits = credits - 1;
  }
  
}

// Interrupt
void coinInterrupt(){
  
  // Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
  cents = cents + 1;
  digitalWrite(ledpin, HIGH);
  
}

As I browse deeper, I was able to fill in more of my problems. I finished coding the coin slot and the push button. My only problem left is the output. My output is a 9v water pump. What code can I use to create a 9v output? Thanks

Here's my current code.

// Constants
const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 7;
const int ledpin2 = 4;
const int buttonPin = 7;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status
const int buttonPin2 = 8;     // the number of the pushbutton pin
int buttonState2 = 0;         // variable for reading the pushbutton status

// Variables
volatile int cents = 0;
int credits = 0;

// Setup
void setup() {
  
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
  pinMode(ledpin, OUTPUT);
  pinMode(ledpin2, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  
}

// Main loop
void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
 // If we've hit our target amount of coins, increment our credits and reset the cents counter
  if (cents >= targetcents) {
    credits = credits + 1;
    cents = cents - targetcents;
  }

  // If we haven't reached our target, keep waiting...
  else {
  }

  // Debugging zone
  Serial.print(cents);
  Serial.print(" cents toward current credit and ");
  Serial.print(credits);
  Serial.println(" credit(s) earned so far.");
  delay(1000);

  // Turn off LED
  digitalWrite(ledpin, LOW);

  // Now, write your own cool code here that triggers an event when the player has credits!
  if (credits > 0 && buttonState == HIGH) {
    digitalWrite(ledpin2, HIGH);
    delay(1000);
    digitalWrite(ledpin2, LOW);
    credits = credits - 1;
  }
  if (credits >= 2 && buttonState2 == HIGH) {
    digitalWrite(ledpin2, HIGH);
    delay(5000);
    digitalWrite(ledpin2, LOW);
    credits = credits - 2;
  }

  
}

// Interrupt
void coinInterrupt(){
  
  // Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
  cents = cents + 1;
  digitalWrite(ledpin, HIGH);
  
}

kimy63:
My output is a 9v water pump. What code can I use to create a 9v output?

You can't. Not directly anyway - the Arduino can't provide 9V and even if it could it's unlikely it could provide enough current to drive your motor.

You will need an external power source and something to allow the arduino to control it - a transistor or a relay for example. Check the motor control section in the playground for solutions.