Hey All,
I am currently working on a project involving a coin acceptor. I have a code that waits until there are 4 pulses send to the Arduino before it will activate two motors.
This is all working fine, but I am trying to combat a mysterious problem I have with the coin acceptor. Every now and then when I go to insert a coin it will register that a coin has been inserted when all I have done is touched the front panel or inserted the coin a tiny way into the slot.
I think I am experiencing the same issue as this: http://www.instructables.com/id/The-Soda-Locker-Vending-Machine/step6/Money-Cheat/
I have tried to use parts of his code to combat the problem but all I can seem to do is stop my code from counting the pulses and do all sorts of funky stuff.
I have also tried using my basic code and earthing the coin acceptor’s metal part to the -v on the power supply.
Here is my basic code without the changes:
#include <AFMotor.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
AF_DCMotor motor(1); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2); // create motor #2, 64KHz pwm
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address (addr, en, rw, rs, d4, d5, d6, d7, backlight, polarity)
// Constants
const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 4;
// Variables
volatile int cents = 0.00;
int credits = 0;
int coins = 0;
// Setup
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
pinMode(ledpin, OUTPUT);
motor.setSpeed(200); // set the speed to 200/255
motor2.setSpeed(200); // set the speed to 200/255
lcd.begin(20,4); // Initialize the lcd for 20 chars 4 lines, turn on backlight
//lcd.noBacklight(); // Turns backlight off
lcd.clear();
// NOTE: Cursor Position: (CHAR, LINE) starts at 0
lcd.setCursor(3,0);
lcd.print("Hey");
lcd.setCursor(3,1);
lcd.print("Official Show");
lcd.setCursor(5,2);
lcd.print("Programme");
lcd.setCursor(6,3);
lcd.print("");
}
// 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 {
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Hey");
lcd.setCursor(3,1);
lcd.print("Official Show");
lcd.setCursor(5,2);
lcd.print("Programme");
lcd.setCursor(6,3);
lcd.print(cents);
delay(1000);
}
// Debugging zone
Serial.print(cents);
Serial.print(" Money toward current programme and ");
Serial.print(credits);
Serial.println(" programme(s) earned so far.");
Serial.print(coins);
Serial.println(" programme(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) {
lcd.clear();
//lcd.setCursor(2,0);
// lcd.print("");
lcd.setCursor(5,1);
lcd.print("Processing");
lcd.setCursor(4,2);
lcd.print("Please Wait");
lcd.setCursor(5,3);
lcd.print("..........");
digitalWrite(ledpin, HIGH);
delay(1000);
digitalWrite(ledpin, LOW);
delay(1000);
digitalWrite(ledpin, HIGH);
delay(1000);
digitalWrite(ledpin, LOW);
// Turn Motors In hopper One
motor.run(FORWARD); // turn it on going forward
motor2.run(FORWARD); // turn it on going forward
//Delay
delay(1000);
//asm volatile (" jmp 0");
return;
// 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;
coins = coins + 1;
digitalWrite(ledpin, HIGH);
}
If anyone could assist me it would be much appreciated as I am now pulling my hair out as to what I can do to stop this error.