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?