Vending machine - help needed

Is there anyone out there that does Arduino / electronics for people as a job? Or are there are 1:1 teachers in Devon?

I have (stupidly) promised my little girl that I would build her a vending machine for her school fair. I'm completely new to Arduino / electronics, but being good with technology I thought I would be able to teach myself...

So I bought a starter Arduino kit (with LCD screens, motors, LED's etc) and worked through the lessons. I managed them OK, but struggled to really learn what I was doing.

I then bought a coin acceptor ... and realised I was in way over my head, when I couldn't get it to link with the Arduino.

I have spent weeks googling, trying other code from other projects, and I have to admit defeat.

My hope was to link the coin acceptor up to the Arduino. The LCD screen says "please enter £1". When £1 had been entered it says "please press button", and when one of the (3) buttons is pressed, the corresponding motor turns and dispenses a capsule. < sounded easy when I had this plan in my head!

So, I've given up and think I need someone to set it up for me, or work with me. I've tried googling workshops, courses etc, but can't find anything. So thought someone on here maybe able to point me in the right direction?

... and if all else fails, I'm going to build a really big vending machine, hide inside it and dispense capsules myself.

Thanks for your time, Sarah

Ok..... dispenses a capsule. What size is the capsule? What weight and what shape? You have to think how you're going to load them all. And what mechanical method is able to reliably isolate one of them, and dispense it.

there is a gigs and colaboration section on this full site. not this forum. you post there and maybe you can find someone who will work with you.

It seems the problem lies with the coin acceptor, right?
That's a long story with no real info.
What do you have so far (code, actual parts?) What does work, what doesn't?

Tell us about the coin acceptor.

Thanks all.

Southpark:
Ok..... dispenses a capsule. What size is the capsule? What weight and what shape? You have to think how you're going to load them all. And what mechanical method is able to reliably isolate one of them, and dispense it.

... Basically I need the Arduino to turn 1 (of 3) motors - I've got the capsule dispensing bit sorted.

wvmarle:
It seems the problem lies with the coin acceptor, right?
That's a long story with no real info.
What do you have so far (code, actual parts?) What does work, what doesn't?

I have multi coin coin coin acceptor, which I have set up to recognise 5 different coins
10p = 1 pulse
20p = 2 pulses
50p = 5 pulses
£1 = 10 pulses
£2 = 20 pulses

This is working fine.

I tried the following code from this tutorial The Maker Show: Episode 10 - Adding a Coin Acceptor to Your Arduino Project | Microsoft Learn
But every time I enter a coin it doesn't read the correct amount - say I enter one 10p (1 pulse) it shows on the serial monitor as 30 cents, then 43 cents, then 83 cents...

// Arduino coin acceptor code by hxlnt
// Originally created for "Adding a Coin Acceptor to Your Arduino Project" on the Maker Show
// See the entire video tutorial at The Maker Show: Episode 10 - Adding a Coin Acceptor to Your Arduino Project | Microsoft Learn
//
// Read your coin acceptor's specs and instructions first for hookup specifics
// Modifications to this code may be needed
// Coin acceptor model used in this example is JY-923
//
// xoxox

// Constants
const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 100;

// 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) {
// Play music?
// Spin up a motor?
// Start a game?
// It's up to you!
}

}

// 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);

}

SO I then tried the following code piggybank code (thinking it would be a start of linking the coin acceptor to the LCD screen.
GitHub - adafruit/Adafruit-Programmable-Piggy-Bank: Save up money and track your savings with a programmable piggy bank by Becky Stern! Oink! (changing the LCD bit at the start to work with mine).

If I load this and go to the serial monitor, and enter a coin it doesn't show as coins / credits, but as pulses. So if I enter £1 it shows at 10 pulses. So this makes me think that the coin acceptor IS working OK, and sending the right information / pulses through to the arduino, but something in the 1st code isn't working right?!?!

The LCD screen shows "please insert one quarter" however nothing else happens when I enter coins. I realise this is set up for a single coin acceptor, so not sure if this is the problem?

/*********************

  • connect the COIN wire to digital 2
  • set the side switches to "FAST" "NC"
    **********************/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

// attach coin wire to digital 2
#define COIN 2
int coins;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int fadebrightness = 0; // how bright the LED is when it's flashing

void setup() {
// declare pin 11 to be an output for LED:
pinMode(11, OUTPUT);
// Debugging output
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);

pinMode(COIN, INPUT);
digitalWrite(COIN, HIGH); // pull up
coins = 0;
}

void loop() {
lcd.setCursor(0,0);
lcd.print(" PLEASE INSERT ");
lcd.setCursor(0,1);
lcd.print(" ONE QUARTER ");
// set the brightness of pin 11:
analogWrite(11, brightness);

// while the coin pin is low (no coin detected), do nothing
while (! digitalRead(COIN)) {
delay(1);
}

// while the pin is high, we'll track the length with a counter
uint8_t counter = 0;
while (digitalRead(COIN)) {
delay(1);
counter++;
}
Serial.print(counter);
Serial.println(" ms long pulse");

if ((counter > 60) || (counter < 20))
return;

coins++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" OINK OINK! ");
lcd.setCursor(0,1);
lcd.print("YOU HAVE $");
lcd.print(coins*.25);
lcd.print(" ");
// loop through to flash the LED
fadebrightness = brightness;
for (int i = brightness; i < 510; i+=5) {
// turn the pin on:
analogWrite(11, fadebrightness);
fadebrightness = fadebrightness + fadeAmount;
delay(20);
// reverse the direction of the fading at the ends of the fade:
if (fadebrightness == 0 || fadebrightness == 255) {
fadeAmount = -fadeAmount;
}
}
brightness = brightness + 5;
}

I also realise both codes have LED code written into them which I don't need, but don't know how to remove it, and not sure if it's affecting anything?

Removing code is easy, just delete those lines. Indeed you may have to update other bits but that's usually trivial (my preferred method is to just work through the compiler errors whenever they pop up).

Then that first piece of code:

You stated before that every tick is 10 cents. Then why adding just 1 cent per pulse?

Also, do you really need to use interrupts? Get rid of that delay(1000) in your code, have a look at "blink without delay" on how to do regular actions without blocking your script, and you can just start to poll the coin acceptor to see whether a pulse is coming in (you will have to add some code to make sure you count each pulse once and only once, only if pulses are extremely short or you have a really heavy workload elsewhere you need interrupts for this).

This is literally the first time I have ever used an Arduino or tried to do coding, so I don't really understand what I am doing...

wvmarle:
You stated before that every tick is 10 cents. Then why adding just 1 cent per pulse?

Thank you, I've changed it to 10 cent per pulse.

wvmarle:
Also, do you really need to use interrupts? Get rid of that delay(1000) in your code, have a look at "blink without delay" on how to do regular actions without blocking your script, and you can just start to poll the coin acceptor to see whether a pulse is coming in (you will have to add some code to make sure you count each pulse once and only once, only if pulses are extremely short or you have a really heavy workload elsewhere you need interrupts for this).

As I don't know what I am doing, I have no idea why the person that wrote the code used interrupts, or what they even are :frowning: I've just looked at "blink without delay" and have no idea how to change the code to remove the interrupts.

As long as you have no delays in the loop, so it's running thousands of times a second, you can simply poll the pin, when you find the pulse you add to your cents, record you see the pulse, and when the pulse is finished you clear that flag again (otherwise a short 1 ms pulse will be counted again and again and again, and your coin pulses may very well be much longer than that).

#define coinPin 2

bool havePulse = false;
int cents = 0;
int oldCents = 0;
void setup() {
  pinMode(coinPin, INPUT);
}

void loop() {
  if (digitalRead(coinPin) == LOW && havePulse == false) { // Pulse coming in.
    havePulse = true; // Record that we're reading this pulse - don't want to read it multiple times.
    cents += 10;
  }
  if (digitalRead(coinPin) == HIGH && havePulse == true) { // End of pulse.
    havePulse = false; // Clear the flag, we're ready for the next.
  }
  if (oldCents != cents) { // The value of cents has changed.
    Serial.print("Coin inserted! Current cents value: ");
    Serial.println(cents);
    oldCents = cents; // Record the new value.
  }
  // Do whatever other stuff you have to do.
}

Thank you.

I've just loaded your code but nothing shows up on the serial monitor now...

Didn't you notice any bits I may have forgotten to add? Like a Serial.begin()...
Remember that this untested, not even compile-tested, typed out in a few minutes directly in the comment, primarily meant to illustrate how it can be done. That Serial bit was something I added as a bonus, thought it may be helpful to give you yet another state-change-detection example.

Don't just load pieces of code from here and there unless you understand what the code is doing. There are great help pages on this site for all Arduino specific commands, plus the huge resources on C++ programming all over the Internet.

Thank you.

As I said I have no experience, so won't know if anything is missing.

Thanks for your help. But I think I'll give up.

Thanks anyway. Sarah

You will have to put some effort in it - start by learning C++ basics, and you'll be able to understand the code soon enough. It's not that hard, but it's also not something you can do without learning anything!

I've spent a good month trying to teach myself, starting from the beginning, but just don't get it

wvmarle:
You will have to put some effort in it - start by learning C++ basics, and you'll be able to understand the code soon enough. It's not that hard, but it's also not something you can do without learning anything!

I'm not shy for hard work, but I've spent a good month trying to teach myself the very basics, but just don't get it. Not everyone is good at everything - which is my in my original post I asked for help finding someone that does 1:1 lessons / workshops.

Sadly I don't have the time to invest anymore in learning this.

Thanks anyway for your help.

No idea where to find teachers... maybe your local secondary school? Or if you have a university/college in town, post a note there. They must have notice boards for general requests.

This is literally the first time I have ever used an Arduino or tried to do coding, so I don't really understand what I am doing...

:frowning:

Sadly I don't have the time to invest anymore in learning this.

:cry:

Not good prospects at all. The usual path is to study and use code that is easy enough to understand or to figure out while learning something new. To not understand what you are doing is taking you nowhere.

Johan_Ha:
:frowning:

:cry:

Not good prospects at all. The usual path is to study and use code that is easy enough to understand or to figure out while learning something new. To not understand what you are doing is taking you nowhere.

I know, it's sad... wish I had time to learn. Which is why I thought paying someone to do it would be the best option for now.

Nevermind, I'll get there, just a less high tech route :slight_smile:

beelywoker:
I know, it's sad... wish I had time to learn. Which is why I thought paying someone to do it would be the best option for now.

Which in itself is a good idea - this the wrong platform, as it's global and you're looking for someone local.

Anyway if programming as such has your interest, maybe you should try learning Python first. It's very often used as first teaching language, and much easier to learn than a low-level language as C++ as there's a lot that the interpreter takes care of (memory management, variable types, etc).

The moment you understand the basic concepts of programming, it's pretty easy to learn other languages.