Coin Counter V1

So I started with the coding made by Michael Illingby “Micheali”, I changed it to fit the criteria my teacher gave me for our “Final Project”. I 3D designed a machine that would automatically sort and count the coins. Now I need to finallize the code and put the finished arduino into the machine. The original code has the coin detection system set up for switches, I was wanting to put IR sensor modules to detect the coins. If that would be too dificult let me know and I’m open to other suggestions. I was also hoping the display would be able to show the total amount of money in the machine as well as being able to click a button and it will display the number of each coin in the machine (EX ~ Total $2.27 Click Botton P-2 N-5 D-10 Q-5). I’m very new to arduino and I tried my best to make the code work but I was unable to make it work, I was really hoping someone would be able to help me fix it because not even my teacher knows how…

/*
Coin Counter by Mitchell Moriarty 2018

Pins defined below.

Scope of project: After sorting coins, and having them fall past a trigger that closes to ground, count the 
coin, and then add it's value to a running total.
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

// this constant won't change:
const int  coin1Pin = 2;   // the pin that the 1 pushcoin is attached to
const int  coin5Pin = 3;    // the pin that the 5 pushcoin is attached to
const int  coin10Pin = 4;     // the pin that the 10 pushcoin is attached to
const int  coin25Pin = 5;     // the pin that the 25 pushcoin is attached to
const int  led1 = 6;       // the pin that the Penny LED is attached to
const int  led5 = 7;        // the pin that the Nickle LED is attached to
const int  led10 = 8;       // the pin that the Dime LED is attached to
const int  led25 = 9;        // the pin that the Quarter LED is attached to
 
// Variables will change:
unsigned long  coin1counter = 0;  //  counter for the number of $0.01 coins
unsigned long  coin5counter = 0;   //   counter for the number of $0.05 coins
unsigned long  coin10counter = 0;   //    counter for the number of $0.10 coins
unsigned long  coin25counter = 0;    //     counter for the number of $0.25 coins

// Here we will store the totals

float coin1total = coin1counter*0.01;  // Math for Calculating value of a Penny in relation to a dollar
float coin5total = coin5counter*0.05;   // Math for Calculating value of a Nickle in relation to a dollar
float coin10total = coin10counter*0.10;  // Math for Calculating value of a Dime in relation to a dollar
float coin25total = coin25counter*0.25;   // Math for Calculating value of a Quarter in relation to a dollar
int totalcoins = coin1counter+coin5counter+coin10counter+coin25counter;
float totalcash = coin1total+coin5total+coin10total+coin25total;

void setup() {
  // initialize the coin pin as a input:
  pinMode(coin1Pin, INPUT_PULLUP);
  pinMode(coin5Pin, INPUT_PULLUP);
  pinMode(coin10Pin, INPUT_PULLUP);
  pinMode(coin25Pin, INPUT_PULLUP);
  pinMode(led1, OUTPUT);  // initialize the LED as an output:
  pinMode(led5, OUTPUT);  // initialize the LED as an output:
  pinMode(led10, OUTPUT);  // initialize the LED as an output:
  pinMode(led25, OUTPUT);  // initialize the LED as an output:
  
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("Coin Counter");
  lcd.setCursor(0,1);
  lcd.print("Starting.");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Starting..");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Starting...");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Starting....");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Starting.....");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Starting......");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Starting.......");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Starting........");
  delay(100);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Insert Coins Now");
  delay(1000);
  lcd.clear();
}
 
 
void loop() {
    //read button states and make sure we are really seeing a coin drop.

      int coin1_state = digitalRead(coin1Pin);
      int coin5_state = digitalRead(coin5Pin);
      int coin10_state = digitalRead(coin10Pin);
      int coin25_state = digitalRead(coin25Pin);


//debounce & process

delay (100); // this should give us the correct timing for 1 coin every 200ms which is 5coins a second.

// Here we will start looking for coins and adding them up.
  if (coin1_state == LOW && digitalRead(coin1Pin == LOW)) {  
      coin1counter ++; // if the coin is detected, increment the counter
      digitalWrite(led1, HIGH);
}
  if (coin5_state == LOW && digitalRead(coin5Pin == LOW)) {  
      coin5counter++; // if the coin is detected, increment the counter
      digitalWrite(led5, HIGH);
}
  if (coin10_state == LOW && digitalRead(coin10Pin == LOW)) {  
      coin10counter++; // if the coin is detected, increment the counter
      digitalWrite(led10, HIGH);
}
  if (coin25_state == LOW && digitalRead(coin25Pin == LOW)) {  
      coin25counter++; // if the coin is detected, increment the counter
      digitalWrite(led25, HIGH);
}
      UpdateTotals();
      lcd.print("                ");
      lcd.setCursor(0,0);
      lcd.print("Cash $:");
      lcd.print(totalcash);
      lcd.setCursor(0,1);
      lcd.print("# of Coins:");
      lcd.print(totalcoins);
      digitalWrite(led1, LOW);
      digitalWrite(led5, LOW);
      digitalWrite(led10, LOW);
      digitalWrite(led25, LOW);


 
  //delay(100);
}
void UpdateTotals(){

coin1total = coin1counter*0.01;
coin5total = coin5counter*0.05;
coin10total = coin10counter*0.10;
coin25total = coin25counter*0.25;
totalcoins = coin1counter+coin5counter+coin10counter+coin25counter;
totalcash = coin1total+coin5total+coin10total+coin25total;
}

Coin_Counter_V1.ino (4.92 KB)

2 requests.

  1. Post Your code using code tags.
  2. What is the outcome, what happends and what is wanted to happend.

I plan to make the machine with a hopper so that you can drop a hand full of coins in and a wheel will take the coins one by one and put them onto a rain the rail will have holes that are sized for each coin as the coins roll down they will roll into the holes their size and roll past the ones that are too small. Once they fall through the hole they will fall down a very short chute before landing in sorted trays. In each chute I plan to put an IR sensor module that will send a signal everytime a coin passes allowing arduino to coin the total as well as the total number of each coin. There was also LEDs that were added to the code so that a different LED would blink each time a coin passed the sensor. I'm am not sure if I will use this feature, as the deadline is coming up and Im not sure if I want to deal with the wiring of it but, we will see. I hope this answers your second question. Sadly I don't know how to solve the other problem/ your first question. I tried but due to this being my very first post on the Arduino Forum I am quite uneducated when it comes to the workings of this forum. If you could try to help me post the code in the form you need that would me much appriciated!

I am quite uneducated when it comes to the workings of this forum.

There's a post at the top of every board titled, "How to use this forum, please read". So there is absolutely NO excuse for you saying that you don't know how to use code tags.

The main difference between those who can and those who cannot in this hobby is the tendency to read the directions. If you're one of those people who doesn't like to read the directions first then pick something different to do because electronics and especially programming is going to frustrate you to no end. If you're going to make it in this game you gotta be smart enough to spot the how to in big bold letters at the top of the page.

I was really hoping someone would be able to help me fix it

We can't help you fix it until you tell us how it is broken...

Railroader:
2) What is the outcome, what happends and what is wanted to happend.

You only answered half of that question. Please answer the whole thing.

I'm sorry, Railroader is barely legible. What he was asking was:

What was the outcome. What happened and how does that compare with what you wanted to happen.

I am in desperate need of help and I would not skip directions if it meant getting to the help I need. I clicked on the post but I have no attention to detail so I completly missed the fact that you are able to click on the picture. I feel really dumb now, I appologize. It's not that it's broken I need to rephrase that. I have no knowledge of how to code the things that I haven't already done in my code. This includes the IR sensor modules and the botton that changes the LCD to display the coin amounts. I will fix my first reply to him right now as well.

I have no knowledge of how to code the things that I haven't already done in my code. This includes the IR sensor modules and the botton that changes the LCD to display the coin amounts.

OK, so let's break it into pieces. Put the code you have on the self for a minute. Work on a new code that ONLY deals with the IR sensor. Mount it on a breadboard or something so you can block the light or allow it. And write some simple code that does ONLY that one thing. Then once you know how IR sensors work then you can move to trying to add that to your larger code.

That's how good code gets done. If you looked at my computer, for every large complicated project you would also find a folder full of dozens of little test sketches to work out this one little thing or that.

I will order the IR sensors and then do that. I linked the IR sensors down below I think these were the correct sensors

Those sensors don't appear suitable to detect coins passing by.

Get a break beam sensor instead: the two sides pointing at one another. Can also easily be built yourself by just using an IR LED and IR photodiode. Let the coins roll between them for excellent signals.

Those sensors looks like needing a reflector, a mirror, to function. But I agree with You, wvmarle. Unless the coins are thicker then the gap between the IR diode and the photo sensor, letting the coin roll between them looks pretty good.

int coin10_state = digitalRead(coin10Pin);

// coin10_state has just been set equal to coin10Pin.  What is gained by comparing them?

 if (coin10_state == LOW && digitalRead(coin10Pin == LOW)) {  
      coin10counter++; // if the coin is detected, increment the counter
      digitalWrite(led10, HIGH);

This is going to cause problems. Because you call something a counter doesn't mean it functions like other counters you've seen. This one will increment the whole time the if() condition is true - so you may end up with one hundred-sixty-seven dimes counted for one passing by. You need to detect when the condition *becomes * true. Check out IDE/file/examples/digital/state change detection (close cousin of debouncing) to learn how to do this.