general questions to my Counting Machine

Hi there,
im quite new to Arduino. Im working as Pizzadriver, besides school, so I have a lot of small coins. I want to buy a machine that sorts and counts. The sorting is done mechanical.

My Idea for counting was this:
every coin conduct electricity, so if a coin passes 2 wires it closes the circle, the Arduino detects the voltage and count one coin plus.
I used the program for a button. And it kind of works but sometimes it counts but the wires arent touching.

Maybe you have a smarter Idea? Mine is probably not the best...
P.s. sorry for bad englisch :smiley:

//define variables
int count= 0;
int buttonPin1= 2;
int buttonState1 = 0;
int gesamt=0;

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
}
 
void loop() 
{
  buttonState1 = digitalRead(buttonPin1);
  if(buttonState1==0 )  //if there is a voltage he goes in here
  {
    count++;            //counts my variable
    delay(500);         //buffer because the coin takes some time to fully past the wire, program should not count more then 1
    Serial.print(count);//test
    Serial.print(" ");
    delay(300);
  }

  if(count==100)        //abort condition
  {
    gesamt=count*2;
    Serial.print("Der Gesamtbetrag beträgt:");
    Serial.print(gesamt);
    while(1);           //endless loop to end program
  }
}

I don´t know where to put my code so i just copy it:

There was a thread just above where you posted this called "How to use this forum - please read". Did you even consider looking for instructions or reading them?

You have your pin configured as "INPUT" instead of "INPUT_PULLUP". How do you have it wired? Is your pin floating when the circuit isn't closed?

    delay(500);         //buffer because the coin takes some time to fully past the wire, program should not count more then 1

Stuffing your head in the sand for a fixed length of time is NOT the correct way to deal with this situation.

Look at the state change detection example. Increment the count when the switch BECOMES "pressed".

@Delta_G
Sorry I didnt saw that post, gonna read it now.
Thanks for your answer. And its a simple circuit, and 2 cables arent pluged in, i hold them together when they should count and else they are floating.

Thanks PaulS, will check it!

Thanks a lot!
First I only Used StateChangeDetection. Worked but not like i wanted it.
Then I also changed it to INPUT_PULLUP and now it works fine.
But I still have one question. I thougt that "HIGH" means there is a Voltage but it somehow its "LOW"... why so?

High means 5V and Low means connected to ground. Not connected is floating and that can read anything at random.

Most of the time we wire our buttons and switches so that they connect to ground when made so LOW usually means a pressed button. That is done so that the wires going to the buttons aren't carrying voltage. That way we avoid so many hot wires that could short to ground.

EDITED per #7

connectedness

connection? connectivity?

Coins are dirty and might not make good contact with wires. They do, however, block light quite well. I would recommend a photo interrupter to sense the passage of coins.
http://www.martyncurrey.com/connecting-an-photo-interrupter-to-an-arduino/

every coin conduct electricity, so if a coin passes 2 wires it closes the circle, the Arduino detects the voltage and count one coin plus.

This solution just counts the number of coins, regardless of the coin value. Is this what you want?
A coin acceptor uses Hall sensors to "calculate" the signature of a coin, thus distinguishing between coins.
You put the coins through a coin acceptor to calculate a specific amount (of money) rather than count the coins.

florinc:
connection? connectivity?

Sorry. Just connected. Typing on a little phone keyboard and dealing with autocorrect.

No, the coins are getting sorted. In every storage is one "detector" and stores the counting in different variables.