So I'm trying some basic before bigger project(coin acceptor to turn on 12v air hockey table).
Want to turn on LED after a pulse from Coin Acceptor.
When one coin inserted I see #2 on Coin Acceptor LED, assuming that's 2 pulse.
Anyway, I leached this code off internet but it is not working for me. The minute Pro Micro is powered, LED is 'ON' without trigger from Coin Acceptor, why? I hooked up LED from pin3 and through resistor to onboard ground.
const int coinpin = 2;
//const int ledpin = LED_BUILTIN;
const int ledpin = 3;
const int target = 4;
// Variables
volatile int cents = 0;
int credits = 0;
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
pinMode(ledpin, OUTPUT);
}
void loop() {
// If we've hit our target amount of coins, increment our credits and reset the cents counter
if (cents >= target) {
credits = credits + 1;
cents = cents - target;
}
// 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);
}
// 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);
}
Most likely your coininterrupt is floating and acting like an antenna for your mains frequency. Study the schematic of your coin device and see where the interrupt line goes.
Paul
ps. Is the Arduino ground connected to the coin accepter ground?
i would think the LED could only be on for a fraction of a second since it is constantly being turned off in loop.
i would comment out the attachInterrupt() to see if the LED is still lit. are you sure the LED is wires to ground?
if it isn't, then is seems the interrupt is constantly being invoked. how is the coinPin wired. should is be configured as INPUT_PULLUP?
what happens if a coin is inserted near the end of the 1 sec delay? the led won't be on very long. it would make more sense to recognize when a coin is inserted and start a timer or call delay.
i think using an interrupt is unnecessary in this case. either you're waiting for coins or you're doing something else.
I was trying to connect 3 electronics with one 12v power source so it could share common ground and it seems that this little board draw more voltage then the other. I unplug the one leaving just the coin and Arduino and I think I killed ARDY.
Alright, on with the 3rd pro micro. Time to solder some more pins.
From Arduino IDE examples, which one should I go off of based on my project vision?