Code for Coin counter
const int pinIr10c = 2;
const int pinIr1c = 6;
const int pinIr5c = 8;
const int pinIr25c = 5;
int IRvalue10c = 0;
int IRvalue1c = 0;
int IRvalue5c = 0;
int IRvalue25c = 0;
const int pinIrStart = 6;
int pinMotor = 3;
int IRvalueStart = 0;
int coinCount = 0;
int MoneyWorth = 0;
String MessageToPrint = "c";
/* Display */
/* Include the HCMAX7219 and SPI library */
#include <HCMAX7219.h>
#include "SPI.h"
/* Set the LOAD (CS) digital pin number*/
#define LOAD 10
/* Create somewhere to store the message*/
String message = "BEEF";
/* Create an instance of the library */
HCMAX7219 HCMAX7219(LOAD);
void setup()
{
Serial.begin(9600);
pinMode(pinIr2c,INPUT);
pinMode(pinIrStart,INPUT);
pinMode(pinMotor,OUTPUT);
//Welcome message
byte Loopcounter;
byte Position;
}
void loop()
{
IRvalue10c = digitalRead(pinIr10c);
IRvalue1c = digitalRead(pinIr1c);
IRvalue5c = digitalRead(pinIr5c);
IRvalue25c = digitalRead(pinIr25c);
IRvalueStart = digitalRead(pinIrStart);
// if (IRvalueStart == 0) {
// Serial.println("Coin detected. Starting motor...");
// digitalWrite(pinMotor, HIGH);
// delay(200);
// }
if (IRvalue10c == 0) {
Serial.println("It was a 10c coin that was detected");
coinDetected();
MoneyWorth = MoneyWorth + 10;
displayAmountSerial();
displayAmount();
delay(200);
}
if (IRvalue1c == 0) {
Serial.println("It was a 1c coin that was detected");
coinDetected();
MoneyWorth = MoneyWorth + 1;
displayAmountSerial();
displayAmount();
delay(200);
}
if (IRvalue5c == 0) {
Serial.println("It was a 5c coin that was detected");
coinDetected();
MoneyWorth = MoneyWorth + 5;
displayAmountSerial();
displayAmount();
delay(200);
}
if (IRvalue25c == 0) {
Serial.println("It was a 25c coin that was detected");
coinDetected();
MoneyWorth = MoneyWorth + 25;
displayAmountSerial();
displayAmount();
delay(200);
}
}
void displayAmount() {
/* Clear the output buffer of the screen */
HCMAX7219.Clear();
/* Write some text and output it*/
if (MoneyWorth > 9999){
HCMAX7219.print7Seg(MoneyWorth,6);
}
else if (MoneyWorth > 999){
HCMAX7219.print7Seg(MoneyWorth,5);
}
else if (MoneyWorth > 99){
HCMAX7219.print7Seg(MoneyWorth,4);
}
else if (MoneyWorth > 9){
HCMAX7219.print7Seg(MoneyWorth,3);
}
else{
HCMAX7219.print7Seg(MoneyWorth,2);
}
HCMAX7219.print7Seg("c",1); //always add the 'c' sign
HCMAX7219.Refresh();
}
void displayAmountSerial(){
Serial.print("So that's ");
Serial.print(coinCount);
Serial.print(" coins now and the total value counted is ");
Serial.print(MoneyWorth);
Serial.println("c");
Serial.println();
}
void coinDetected() {
coinCount ++;
digitalWrite(pinMotor, LOW);
}
1st design for counter
This is what I am using for the Coin Counter. It uses an infrared laser that when broken by a coin falling will count the value of the coin that will be different for each hole.