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)
