Go Down

Topic: Arduino/CoinSlot/LCD - Variable Display. Need Help. (Read 701 times) previous topic - next topic

Gedon

Hi, need help on some programming problem.
I have already post a question about the interface of a coin acceptor to Arduino Uno,
http://arduino.cc/forum/index.php/topic,138209.msg1038645.html#msg1038645
and just wanted to repost and add another concern about the programming.

In the display, i want to display the Total of the Coins and the number of the coins inserted.
With this code i can't seem to get what i want to display.
x = number of Type 1 Coin
y = number of Type 2 Coin
z = number of Type 3 Coin
a = monetary total of the coins inserted

So here is my code:
Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
volatile int coins = 0;
volatile int x = 0;
volatile int y = 0;
volatile int z = 0;
int a = 0;
void setup()
{
  lcd.begin(16, 2);
//CoinSlot
  EIFR = _BV (INTF0);
  pinMode(2,INPUT_PULLUP); 
  attachInterrupt(0, coinInserted, FALLING);
//CoinSlot
}
//CoinSlot
void coinInserted()
{
  coins = coins + 1;
  CoinChange = 1;
}
void loop()
{
  delay(1000);
  if(coins == 1){
    x = x + 1;
    a = a + 1;
  }
  else if (coins == 5){
    y = y + 1;
    a = a + 5;
  }
  else if (coins == 10){
    z = z + 1;
    a = a + 10;
  }
  lcd.setCursor(1,0);
  lcd.print(x);
  lcd.setCursor(4,0);
  lcd.print(y);
  lcd.setCursor(7,0);
  lcd.print(z);
  lcd.setCursor(1,1);
  lcd.print(a);
  coins = 0;
}


What happens is that i have setup 4 variables that will be displayed in the LCD. I was hoping that with the 1 second delay it would have given the interrupt function the time it needs to count up the "coins" variable before entering the Loop function. But as a result it either does count right or only counts up the "x" variable even when the i have use Type 2 or 3 coins.

Need HELP.

PaulS

Quote
With this code i can't seem to get what i want to display.
x = number of Type 1 Coin
y = number of Type 2 Coin
z = number of Type 3 Coin
a = monetary total of the coins inserted

So, why not use names that make sense?

Code: [Select]
volatile int x = 0;
volatile int y = 0;
volatile int z = 0;

Why are these volatile?

Code: [Select]
  CoinChange = 1;
Why is this not declared?

Quote
What happens is that i have setup 4 variables that will be displayed in the LCD. I was hoping that with the 1 second delay it would have given the interrupt function the time it needs to count up the "coins" variable before entering the Loop function. But as a result it either does count right or only counts up the "x" variable even when the i have use Type 2 or 3 coins.

Where are your debug statements? What value does coins have, on each pass through loop?

Gedon

I am testing the code first that is why i have named them simply as "x", "y" and "z" variables.
Declaring these variables as volatile is just a precaution, but if it is unneeded then i'll simply remove it.

"CoinChange = 1" ; i forgot to delete that statement. i declared a "CoinChange" variable for a condition purpose but it is useless unless i can correct the problem of my code.

There is no problem with the connections of the devices. All is working properly.
What i want to display in the LCD is the 4 variables that counts the number of the coins and the total monetary of the coins inserted.
Type 1 coin (P1.00) = 1 pulse
Type 2 coin (P5.00) = 5 pulses
Type 3 coin (P10.00) = 10 pulses

Adding the total monetary is not a problem. The main concern is that i want to count the number of each type of coin that is inserted in the system.

With the code i shown, when i tested it, logically i was thinking with the delay at that start of the loop function the interrupt function would have had the time to count up the "coins" value before entering the main codes in the loop function.
The result is that it either counts right or:

~ Even when the total monetary counts up correctly to the corresponding type of coin, the Variable that corresponds to that coin doesn't count up.\
~ It counts up the wrong variable, most of the time it is the "x" variable, even when type 2 or type 3 is inserted in the coin slot.
~ It doens't count at all; the total monetary and any of the variables.

Ideally what i want is:
~ When i insert a Type 1 Coin, "x" variable counts up (x = x + 1), and total monetary counts up as well (a = a + 1)
~ When i insert a Type 2 Coin, "y" variable counts up (y = y + 1), and total monetary counts up as well (a = a + 5)
~ When i insert a Type 3 Coin, "z" variable counts up (z = z + 1), and total monetary counts up as well (a = a + 10)

PaulS

Wishing that things would happen is fine. Actually adding some Serial.print() statements to see what IS happening is far better, in my opinion.

Go Up