Please help me with coin-counter

Hey there.

i´ve come to the conclusion that i must be getting older, and slower at learning.
i am new to arduino, and I want to learn so bad.

my current problem is coding my coin-sorter/counter.

My setup:

6 x ir-sensor, 3 pins - HW201                                                                                             
( for detecting Danish coins; (order by size); 5, 20, 2, 10, 1, 0,5 dk.kr.)

Arduino uno board

4 Dig. sev.seg display, 12 pins

a few half attempts here:


#include <Arduino_BuiltIn.h>

#include <Wire.h>


#include <Arduino.h>

#define PINa A0
//#define PINb A1
//#define PINc A2
//#define PINd A3
//#define PINe A4
//#define PINf A5

//bool a;

float a = 5;  
//float b = 20;
//float c = 2;
//float d = 10;
//float e = 0.5;
//float f = 1;

// variable use to measuer the intervals inbetween impulses
int i=0;
// Number of impulses detected
int impulsCount=0;
// Sum of all the coins inseted
float total_amount=0;

void setup() {
 Serial.begin(9600);
//total= a;
pinMode(PINa, INPUT);
 //Serial.print("a = ");
 //Serial.println(a);
 //Serial.print("b = ");
 //Serial.println(b);
 //Serial.print("a + b = ");
 //Serial.println(a+b);
 //Serial.println(a+b+c);
 //Serial.println(+a);
}

void loop() {

 i=i+1;

  Serial.print("i=");
  Serial.print(i);
  Serial.print(" Impulses:");
  Serial.print(impulsCount);
  Serial.print(" Total:");
  Serial.println(total_amount);}
 
if digitalRead  (PINa = LOW) (i>=30 and impulsCount==1){
    total_amount=total_amount+2;
    impulsCount=0;
    Serial.println (0, total_amount);
  }
  
// if digitalRead(PINa) == LOW
//delay (1000);

//Serial.println (total);
}


------------------------------------

/* SevSeg Counter Example
 
 Copyright 2020 Dean Reading
 
 This example demonstrates a very simple use of the SevSeg library with a 4
 digit display. It displays a counter that counts up, showing deci-seconds.
 */

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
//sensorVal = (A1);
void setup() {
  byte numDigits = 4;
  byte digitPins[] = {12, 9, 8, 6};
  byte segmentPins[] = {11, 7, 4, 2, 1, 10, 5};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_ANODE; // See README.md for options
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = true; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(255);
 Serial.begin(9600);
  pinMode(3, INPUT_PULLUP);
  pinMode(13, OUTPUT);

void loop()



//   {
;   (digitalRead(3)== LOW)
 
;  {
    digitalWrite(13,HIGH);

    Serial.println(A1);    Serial.print(+50)  ;
    sevseg.setNumber(+50);
    delay(1000);
  }
  
  {
    
    digitalWrite(13,LOW);
    delay(1000);
  }
 }



/// END ///

-------------------

and many more examples of me trying to pick some and put together, but needles to show.

thank you

What exactly is the problem? You didn't say.

1 Like

That's part of your problem, I'm guessing.

1 Like

Arduino: 1.8.16 (Windows 8.1), Board: "Arduino Uno"

another.coin.counter.fail:56:1: error: expected unqualified-id before 'if'

if digitalRead (PINa = LOW) (i>=30 and impulsCount==1){

^~

another.coin.counter.fail:66:1: error: expected declaration before '}' token

}

^

exit status 1

expected unqualified-id before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

wow, paste a lot there,
yes there is allways errors

A suggestion; please give your variables more meaningful names. It will help with troubleshooting now and later.
Another: use int for the coins, since only one type needs a float.

I'm happy to see that you are doing only 1 pin at a time.
This statement assigns the value of LOW to PINa.

What you would like is to measure the value at PINa and then test it by using the == operator:

if ((digitalRead PINa == LOW) and (some_other condition_is_true)) {
//do this
}

Here is something to get you started (E&OE :slightly_smiling_face:):


#include <Arduino.h>

#define Pin_fem_DKK A0
//#define Pin_tyve_DKK A1
//#define Pin_to_DKK A2

float fem_DKK = 5;  
//float tyve_DKK = 20;
//float to_DKK = 2;

// variable use to measuer the intervals inbetween impulses
int interval = 0;

// Number of impulses detected
int impulsCount = 0;

// Sum of all the coins inseted
float total_amount = 0;

void setup() {
Serial.begin(9600);
pinMode(Pin_fem_DKK, INPUT);
}

void loop() {
interval ++; //WHY?

Serial.print(" interval = ");
Serial.println(i);

Serial.print(" Impulses: ");
Serial.println(impulsCount);

Serial.print(" Total: ");
Serial.println(total_amount);}
 
if (digitalRead  (PINa) == LOW) and (interval>=30 and impulsCount==1)) {
    total_amount = total_amount+2;
    impulsCount = 0;
    Serial.print( "Total Amount: ");
    Serial.println ( total_amount);
  }
}

Speak for yourself. I'm getting younger every day :grin:

1 Like

i´m happy as can be. that´s a lot of help. THANK YOU so much. this gave me new energy.

1 Like

Standard Accounting Practice, also.

2 Likes

ya thanks. just had trouble with the half, and thought I had to change them all. :wink:

Fiduciary fractions have always traditionally been handled with integers, too. It's just a change of units, for example counting integer tenths of a penny. Or half pennies, or whatever is necessary.

so far so good.
my only problem now is: that when I put a coin by the censor, it blinks on the sensor LED but doesn't count. if I put a finger in front of it, it does. it's like the Arduino isn't fast enough. I know that it is, so it must be missing some code. Is it to do with "interval", "impulse", or...? any ideers?


#include "SevSeg.h"
SevSeg sevseg;

 #define Pin_en_DKK A0
 #define Pin_halv_DKK A1
 #define Pin_ti_DKK A2
 #define Pin_to_DKK A3
 #define Pin_tyve_DKK A4
 #define Pin_fem_DKK A5
 
int en_DKK = 10;
int halv_DKK = 5;
int ti_DKK = 100;
int to_DKK = 20;
int tyve_DKK = 200;
int fem_DKK = 50;

int interval = 0;

// Number of impulses detected
int impulsCount = 0;

// Sum of all the coins inseted
int total_amount = 0;


void setup() {

pinMode(Pin_en_DKK, INPUT);
pinMode(Pin_halv_DKK, INPUT);
pinMode(Pin_ti_DKK, INPUT);
pinMode(Pin_to_DKK, INPUT);
pinMode(Pin_tyve_DKK, INPUT);
pinMode(Pin_fem_DKK, INPUT);

Serial.begin(9600);
  byte numDigits = 4;
  byte digitPins[] = {12, 9, 8, 6};
  byte segmentPins[] = {11, 7, 4, 2, 3, 10, 5};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_ANODE; // See README.md for options
  bool updateWithDelays = true; // Default 'false' is Recommended
  bool leadingZeros = true; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected
  
  sevseg.begin (hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(200);
}
void loop() {

if ((digitalRead  (Pin_en_DKK) == LOW))
{
   total_amount = total_amount+10;
  
//delay (5);
}

  else  Serial.println(total_amount);
  sevseg.setNumber(total_amount ) ;
  sevseg.refreshDisplay(); 

  
if ((digitalRead  (Pin_halv_DKK) == LOW))
{   total_amount = total_amount+5;

//delay (5);
}

  else  Serial.println(total_amount);
  sevseg.setNumber(total_amount ) ;
  sevseg.refreshDisplay(); 

  
if ((digitalRead  (Pin_ti_DKK) == LOW))
{
   total_amount = total_amount+100;
//delay (5);
}

  else  Serial.println(total_amount);
  sevseg.setNumber(total_amount ) ;
  sevseg.refreshDisplay(); 


if ((digitalRead  (Pin_to_DKK) == LOW))
{
   total_amount = total_amount+20;
   
//delay (5);
}

  else  Serial.println(total_amount);
  sevseg.setNumber(total_amount ) ;
  sevseg.refreshDisplay(); 


if ((digitalRead  (Pin_tyve_DKK) == LOW))
{
   total_amount = total_amount+200;
//delay (5);
}

  else  Serial.println(total_amount);
  sevseg.setNumber(total_amount ) ;
  sevseg.refreshDisplay(); 


if ((digitalRead  (Pin_fem_DKK) == LOW))
{
   total_amount = total_amount+50;
   
    impulsCount = impulsCount +1;
 
//delay (5);
}

  else  Serial.println(total_amount);
  sevseg.setNumber(total_amount ) ;
  sevseg.refreshDisplay(); 
  {
  }  }  

Your indentation suggests that you are mistaken about the boundaries of your code blocks. I'm quite sure it's not doing what you think it is doing, actually it is:

if ((digitalRead  (Pin_tyve_DKK) == LOW))
{
  total_amount = total_amount + 200;
  //delay (5);
}
else
{
  Serial.println(total_amount);
}

sevseg.setNumber(total_amount ) ;
sevseg.refreshDisplay();

Also you are completely lacking state change detection on the inputs, you are testing only the state. I see commented out delay() statements, no explanation so I will guess you also have problems with switch debounce.

Could you please post a picture of your exact setup?

And:

  • how long do you have to hold your finger in front of the sensor for it to register?
  • how long does the coin stay in front of the register?

(post deleted by author)

I´m just trying all kinds of different things, more or less blindfolded I guess. this is my first project and with very few tutorials. there is a video coming, it is currently in the spam filter waiting for approval.
I really appreciate the help. thanks a lot

Thanks, but you don't provide much feedback. For example, several issues are addressed in post #12 and #13, but you made absolutely no reply. Except for the photo...

sorry. first of all i tried to post a video, but only pic. came up no matter what type i converted to.
but my challenge is mostly that English is my third language, so all the technical terms are pretty hard to understand and translation programs are useless in this case.

I understand the words but i really don´t know what to do.

now it's on youtube :wink:

I notice the second washer did not trip the sensor. 

Played back at quarter speed (no pun intended) it looks like the coins might be still bouncing and not be flat against the track when they pass the sensor.  In fact the second one appears to present only edge-on as it passes the sensor.  Is there any way to settle the coin down before it passes the sensor?