Hi, I'm trying to put together a coin acceptor that turns on a solenoid once the correct number of coins have been put in. I had it working correctly the other day while plugged into the computer, but now trying to put it where it's meant to be, it's not working. Even plugging it back into the computer it won't work.
As soon as electricity goes to the arduino, the coin count starts going up (in random increments) and the puzzle is solved. I don't really know much about coding, so this could be an easy fix. Thanks in advance!
// Constants
const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 100;
const byte lockPin = A0;
// Variables
volatile int cents = 0;
int credits = 0;
// Setup
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
// Configure the lock pin as an output
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, LOW);
}
// 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 >= 1) {
Serial.println("The puzzle has been solved");
digitalWrite(lockPin, HIGH);
}
else{
digitalWrite(lockPin, LOW);
}
}
// 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 + 20;
digitalWrite(ledpin, HIGH);
}