i need help with reading interrupts from coin acceptor and activating respective solenoids based on the interrupt received. for example if i set the coin acceptor to give 5 pulses for p1 coin, arduino should be able to read the 5 pulses and activate the respective solenoid/led and for p2 coin if pulses is 2 arduino should activate another solenoid/led. Please help me program this.
i've connected pulse of coin acceptor to interrupt 0 which is pin 2 on arduino
and for solenoid i've connected transistor and a digital pin is connected to the base so when p1 is detected arduino shld read the interrupt and push solenoid and for p2 another solenoid
solenoid i"ve used is of 5V, and coin acceptor is 12V and i've shorted the ground of arduino and 12V supply i need help with program only Please help.
Show us a good schematic of your circuit.
Show us a good image of your wiring.
Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu. [code] Paste sketch here. [/code]
connection is as in the picture and any 2 digital pins are the output to the solenoids.
the coin acceptor can be set to give pulses upto 50, but I've set it to give
2 pulses for p1 coin and
5 pulses for p2 coin
pulses to arduino is connected with signal wire.
code </
volatile int coins = 0; //counter pin from coin acceptor
int solenoidPin = 12; // output to solenoid1 pin for 1st solenoid
int solenoid2Pin = 13;
void setup()
{
Serial.begin(9600);
pinMode (2,INPUT_PULLUP); //2 is counter pin from coin acceptor
attachInterrupt(0, coinInserted, CHANGE); //coin counterr
pinMode(solenoidPin, OUTPUT); //sets 12th pin as output
pinMode(solenoidPin, OUTPUT);//11th output for sol2
}
void coinInserted()
{
coins=coins+1;
}
void loop()
{
Serial.print(coins);
If you insert a 5P coin, won't your program trigger the sol on pin 12 after the first 2 pulses? Shouldn't you use either RISING or FALLING in the interrupt setup, looks like CHANGE would cause doubling of interrupts?
That acceptor may already have what you need built in, can you post a link and/or datasheet?
On your Fritzing picture, I'm not sure about the color of the 3rd band on that pulldown resistor, if it's silver then it's 0.1 Ohm, that's bad.
Yes, I’ve tried the falling and also rising, but I I’m not getting any output on either of the output pins. I don’t think there is a problem with the coin acceptor as I’ve checked the pulse by directly connecting a led so the led blinked twice for p2 coin and blinked 5 times for p1 coin jus like I’ve set them to give pulses. I don’t think pull down resistor is even necessary i’ve Simply used it, connecting it to interrupt directly also works.
@cyborg8274, your program has a major logical problem - how does it know that all of the pulses for a particular coin have been detected?
If you don't deal with that the first 2 pulses for a 5 pulse coin could be interpreted as a 2 pulse coin.
My guess is that when you insert a coin the pulses occur fairly quickly so that you would know that the train of pulses is complete when another pulse does not arrive within a certain time. If you build some code like that into your ISR then it could set a variable (let's call it newCoin) = true when all the pulses have stopped coming.
When the code in loop() sees that newCoin == true it will know that a valid count exists.
You also have a small technical problem. You are using an int as the datatype for your coins variable. An int uses 2 bytes and if you try to read a variable with 2 (or more) bytes that is updated by an ISR the value could change while you are reading the variable - because the read takes more than one instruction. The simplest way to deal with this for your project is to define coins as a byte variable. Otherwise you need to briefly disable interrupts while you read a multi-byte variable.
Can you please, reformat the program codes of your Post#3.
Open your post in the Edit/Modify Mode.
Bring the cursor at the very beginning of your code. Now, type this word: [ c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.
Bring the cursor at the very end of your code. Now, type this word: [ / c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.
IMHO the code in your Original Post was better - going in the right direction.
What gave you the idea to use pulseIn()
Let's think about this logically ...
When you put in a coin that produces the maximum number of pulses what is the time interval (in microsecs) between the first pulse and the last pulse?
When you ISR first detects a pulse it should save the value of micros() and your maain code should allow it to keep counting pulses until enough time has elapsed that you can be certain that no more pulses will come from that coin. I have no idea what that time interval might be but let's pretend it is 20 millisecs or 20,000 microsecs. Then the code in your ISR (extended from your Original Post) might be like this
void coinInserted()
{
if (waitingForCoin == true) { // make sure it only saves the time for the first pulse
isrSavedMicros = micros();
waitingForCoin = false;
}
coins = coins + 1;
}
and in your code in loop() you could have something like this
void loop() {
noInterrupts();
if (waitingForCoin == false) {
coinStartMicros = isrSavedMicros;
tempCoinPulses = coins;
}
interrupts();
if (micros - coinStartMicros >= maxPulseTimeMicros) { // we know there will be no more pulses
numCoinPulses = tempCoinPulses;
waitingForCoin = true;
}
// other code
}