Go Down

Topic: CoinSlot to Arduino... Need Help (Read 933 times) previous topic - next topic

Gedon

Coin Slot to Arduino... Need Help. What to do?
Hey guys, been doing one of the project in instructables w/c is this
http://www.instructables.com/id/Make-Money-with-Arduino/

Interfacing a Coin Slot to a Arduino. Read and done the instructions provided in the project. But there seems to be a problem bec. i can't seem to get the desired output.

I have a Coin Slot w/c was already set to 3 different types of coins (P1, P5.00, P10.00)(Philippine Coins)
Set it up according to the instructions of the project, "COIN" white wire connected to pin2 (interrupt pin 0) of the arduino, common ground connection of the arduino and the coin slot (12V), and for an easy way to see the output i put up 3 LEDs on pin8, pin9, and pin10.

This is the code i used, (modified)(original comments still there)
--
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
int OneLed = 8;
int FiveLed = 9;
int TenLed = 10;
//A Coin has been inserted flag

void setup()
{
  pinMode(OneLed, OUTPUT);
  pinMode(FiveLed, OUTPUT);
  pinMode(TenLed, OUTPUT);
  Serial.begin(9600);               
//Start Serial Communication
 
  attachInterrupt(coinInt, coinInserted, RISING); 
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()   
//The function that is called every time it recieves a pulse
{
  coinsValue = coinsValue + 1;
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
  coinsChange = 1;                         
//Flag that there has been a coin inserted
}

void loop()
{
  if(coinsChange == 1)         
//Check if a coin has been Inserted
  {
   if (coinsValue == 1)
    {
       digitalWrite(OneLed, HIGH);
       delay(1000);
       digitalWrite(OneLed, LOW);
    }
    else if (coinsValue == 5)
    {
       digitalWrite(FiveLed, HIGH);
       delay(1000);
       digitalWrite(FiveLed, LOW);
    }
    else if (coinsValue == 10)
    {
       digitalWrite(TenLed, HIGH);
       delay(1000);
       digitalWrite(TenLed, LOW);
    }
//Print the Value of coins inserted
  coinsChange = 0;
  }
}
--
Rather than using microsoft express, i wanted to use simple LEDs on pin8, pin9, and pin10 to indicate if the program read the input correctly.
after doing the wiring, and coding in the arduino, i tested to see if it would work.
NONE of the LEDs light up after inserting coins on the coinslot.
Need Help :X

Gedon

Coin Slot to Arduino... Need Help. What to do?
Hey guys, been doing one of the project in instructables w/c is this
http://www.instructables.com/id/Make-Money-with-Arduino/

Interfacing a Coin Slot to a Arduino. Read and done the instructions provided in the project. But there seems to be a problem bec. i can't seem to get the desired output.

I have a Coin Slot w/c was already set to 3 different types of coins (P1, P5.00, P10.00)(Philippine Coins)
Set it up according to the instructions of the project, "COIN" white wire connected to pin2 (interrupt pin 0) of the arduino, common ground connection of the arduino and the coin slot (12V), and for an easy way to see the output i put up 3 LEDs on pin8, pin9, and pin10.

This is the code i used, (modified)(original comments still there)
--
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
int OneLed = 8;
int FiveLed = 9;
int TenLed = 10;
//A Coin has been inserted flag

void setup()
{
  pinMode(OneLed, OUTPUT);
  pinMode(FiveLed, OUTPUT);
  pinMode(TenLed, OUTPUT);
  Serial.begin(9600);               
//Start Serial Communication
 
  attachInterrupt(coinInt, coinInserted, RISING); 
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()   
//The function that is called every time it recieves a pulse
{
  coinsValue = coinsValue + 1;
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
  coinsChange = 1;                         
//Flag that there has been a coin inserted
}

void loop()
{
  if(coinsChange == 1)         
//Check if a coin has been Inserted
  {
   if (coinsValue == 1)
    {
       digitalWrite(OneLed, HIGH);
       delay(1000);
       digitalWrite(OneLed, LOW);
    }
    else if (coinsValue == 5)
    {
       digitalWrite(FiveLed, HIGH);
       delay(1000);
       digitalWrite(FiveLed, LOW);
    }
    else if (coinsValue == 10)
    {
       digitalWrite(TenLed, HIGH);
       delay(1000);
       digitalWrite(TenLed, LOW);
    }
//Print the Value of coins inserted
  coinsChange = 0;
  }
}
--
Rather than using microsoft express, i wanted to use simple LEDs on pin8, pin9, and pin10 to indicate if the program read the input correctly.
after doing the wiring, and coding in the arduino, i tested to see if it would work.
NONE of the LEDs light up after inserting coins on the coinslot.
Need Help :X

Nick Gammon

Read this before posting a programming question


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

http://www.gammon.com.au/interrupts

Variables used in the ISR need to be volatile.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Gedon


Coin Slot to Arduino... Need Help. What to do?

This is the code i used, (modified)(original comments still there)
This code doesn't seem to do what i want as an output.

Code: [Select]
[/b]
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
int OneLed = 8;
int FiveLed = 9;
int TenLed = 10;
//A Coin has been inserted flag

void setup()
{
  pinMode(OneLed, OUTPUT);
  pinMode(FiveLed, OUTPUT);
  pinMode(TenLed, OUTPUT);
  Serial.begin(9600);               
//Start Serial Communication
 
  attachInterrupt(coinInt, coinInserted, RISING); 
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()   
//The function that is called every time it recieves a pulse
{
  coinsValue = coinsValue + 1;
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
  coinsChange = 1;                         
//Flag that there has been a coin inserted
}

void loop()
{
  if(coinsChange == 1)         
//Check if a coin has been Inserted
  {
   if (coinsValue == 1)
    {
       digitalWrite(OneLed, HIGH);
       delay(1000);
       digitalWrite(OneLed, LOW);
    }
    else if (coinsValue == 5)
    {
       digitalWrite(FiveLed, HIGH);
       delay(1000);
       digitalWrite(FiveLed, LOW);
    }
    else if (coinsValue == 10)
    {
       digitalWrite(TenLed, HIGH);
       delay(1000);
       digitalWrite(TenLed, LOW);
    }
//Print the Value of coins inserted
  coinsChange = 0;
  }
}


What i want to do is, after receiving pulses from the COIN SLOT the arduino will count the number of pulses (1,5, or 10 pulses) then lights up LED corresponding to the number of pulses (if there is 1 pulse, LED1 will be HIGH; if there are 5 pulses, LED2 will be HIGH; if there are 10 pulses, LED3 will be HIGH)



any comments/suggestions?

PaulS

Quote
any comments/suggestions?

Sure. Hook up another LED with resistor to another pin. In the ISR, turn the LED pin on.

At least you'll know whether the interrupt was triggered.

Far-seeker

For debugging purposes, I would have some message to the Arduino IDE's serial monitor that displays when the void coinInserted() is called.  Just placing one right at the end would give an indication if your ISR is even being called.

Graynomad

#7
Oct 24, 2012, 06:26 pm Last Edit: Oct 24, 2012, 06:28 pm by Graynomad Reason: 1
It seems to me that you need to wait for X amount of time with no pulses before you can use the count. As it is your using coinsValue straight away on the first pulse when there may be more pulses on the way, then you delay(1000) which presumably misses all subsequent pulses.

Also you never reset coinsValue so it's value will continue to rise well past 10.

As it stands I would expect the LEDs to light on the 1st, 5th and 10th coins then never again.

coinsChange should be volatile.

There's no reason to have coinsValue as a float, that's silly. Use an int.


______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Nick Gammon


any comments/suggestions?


Yes. Pay attention to when I said to make ISR variables volatile. Don't just ignore it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Code: [Select]

int coinsChange = 0;
...
void coinInserted()   
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;                         
}

...
void loop()
{
  if(coinsChange == 1)     
...

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
Don't just ignore it.

To be fair, OP didn't. He/she just didn't make all ISR and loop() accessed variables volatile.

Graynomad

Well the OP is ignoring other people, this has been cross posted, myself and Far-seeker responded to the exact same post yesterday

http://arduino.cc/forum/index.php/topic,128790.msg969482.html#msg969482

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Coding Badly


Go Up