#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <EEPROM.h>
#define TEN_VAL 10
#define FIVE_VAL 5
#define ONE_VAL 1
//define keypad
const byte ROWS = 4;
const byte COLMS = 4;
char keys[ROWS][COLMS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {12, 11, 10, 9};//initialize what pins each row is set too
byte colmPins[COLMS] = {8, 7, 6, 5};//initialize what pin each colum is set too
//initialize all
boolean presentValue = false;
boolean final= false;
boolean next=false;
String num1;
int total,d1,d2,d3,coin,total1;
char op,key;
int c1a,c2b,c3c;
//create servo object
Servo myservo1;
Servo myservo2;
Servo myservo3;
int pos = 0;
//create a keypad from the setup above for the arduino to recognize
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colmPins, ROWS, COLMS);
//lcd
LiquidCrystal_I2C lcd(0x27,16,2);
int f1=0,f2=0,f3=0;
int c1,c2,c3;
void setup(){
//display setup
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" ARDUINO BASED ");
lcd.setCursor(0,1);
lcd.print(" COIN SORTING ");
delay(2000);
lcd.clear();
//servo setup
Serial.begin(9600);
myservo1.attach(3);
myservo2.attach(13);
myservo3.attach(4);
//memory setup
int g=123;
if(EEPROM.read(256-255)!=g){
EEPROM.write(256-255,g);
}else{
EEPROM.get(0-255,c1);
EEPROM.get(3-255,c2);
EEPROM.get(5-255,c3);
}
}
void loop(){
//ir sensor for coin counting
int s1=analogRead(A0);
int s2=analogRead(A1);
int s3=analogRead(A2);
if(s1>=200 && f1==0){
f1=1;
}
else if(s1<200 && f1==1){
f1=0;
c1++;
}
if(s2>=200 && f2==0){
f2=1;
}
else if(s2<200 && f2==1){
f2=0;
c2++;
}
if(s3>=200 && f3==0){
f3=1;
}
else if(s3<200 && f3==1){
f3=0;
c3++;
}
int savedAmount=(c1*10)+(c2*5)+(c3*1);
//alphanumeric keypad for input
key =keypad.getKey();
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
{
if (presentValue != true)
{
num1 = num1 + key;
int numLength = num1.length();
lcd.clear();
lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
lcd.print(num1);
final=true;
}
}
else if (presentValue == false && key != NO_KEY && (key == 'D' || key == 'C' || key == 'B' || key == 'A'))
{
if(key=='A'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("P10 P5 P1");
lcd.setCursor(1,1);
lcd.print(c1);
lcd.setCursor(7,1);
lcd.print(c2);
lcd.setCursor(13,1);
lcd.print(c3);
}
else if(key=='B'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Savings Amount ");
lcd.setCursor(7,1);
lcd.print(savedAmount);
}
}
else if (final == true && key != NO_KEY && key == '#'){
total = num1.toInt();
total1=total;
}
else if (key != NO_KEY && key == '*'){
presentValue = false;
final = false;
num1 = "";
total = 0;
op = ' ';
lcd.clear();
}
//coin dispensing mechanism
if (savedAmount>=total && total>0){
//coin calculation, verification, and dispense
d1=calculateCoin(c1,total,TEN_VAL);
if(d1>0){
d2=calculateCoin(c2,d1,FIVE_VAL);
if(d2>5){
d3=calculateCoin(c3,d2,ONE_VAL);
if (d3==0){
total = calcNumCoins(c1,TEN_VAL, total, clockTurn1);
total = calcNumCoins(c2,FIVE_VAL, total, clockTurn3);
total = calcNumCoins(c3,ONE_VAL, total, clockTurn2);
next=true;
}else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Not Enough ");
lcd.setCursor(0,1);
lcd.print(" Coin ");
}
}else if(d2>0 && d2<5){
d3=calculateCoin(c3,d2,ONE_VAL);
if (d3==0){
total = calcNumCoins(c1,TEN_VAL, total, clockTurn1);
total = calcNumCoins(c2,FIVE_VAL, total, clockTurn3);
total = calcNumCoins(c3,ONE_VAL, total, clockTurn2);
next=true;
}
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Not Enough ");
lcd.setCursor(0,1);
lcd.print(" Coin ");
}
}else{
total = calcNumCoins(c1,TEN_VAL, total, clockTurn1);
total = calcNumCoins(c2,FIVE_VAL, total, clockTurn3);
total = calcNumCoins(c3,ONE_VAL, total, clockTurn2);
next=true;
}
}else if(d1==0){
total = calcNumCoins(c1,TEN_VAL, total, clockTurn1);
total = calcNumCoins(c2,FIVE_VAL, total, clockTurn3);
total = calcNumCoins(c3,ONE_VAL, total, clockTurn2);
next=true;
}
//dispensed coin deduction to coin savings
if(next==true){
c1a=coinDeduction(c1,total1,TEN_VAL);
total1=calculateCoin(c2,d1,TEN_VAL);
c2b=coinDeduction(c2,total1,FIVE_VAL);
total1=calculateCoin(c2,d1,FIVE_VAL);
c3c=coinDeduction(c3,total1,ONE_VAL);
c1-=c1a;
c2-=c2b;
c3-=c3c;
EEPROM.put(c1,0-255);
EEPROM.put(c2,3-255);
EEPROM.put(c3,5-255);
int savedAmount=(c1*10)+(c2*5)+(c3*1);
}
//deletion of any input
delay(2000);
lcd.clear();
num1 = "";
total = 0;
op = ' ';
}else if(savedAmount<total){
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" NOT ENOUGH ");
lcd.setCursor(0,1);
lcd.print(" SAVINGS ");
//reset input
delay(2000);
lcd.clear();
num1 = "";
total = 0;
op = ' ';
}
}
//servo turn for 10 peso coin
void clockTurn1(){
myservo1.write(270);
delay(150);
myservo1.write(90);
delay(150);
}
//servo turn for 5 peso coin
void clockTurn3(){
myservo3.write(270);
delay(150);
myservo3.write(90);
delay(150);
}
//servo turn for 1 peso coin
void clockTurn2(){
myservo2.write(270);
delay(150);
myservo2.write(90);
delay(150);
}
//servo motor function for coin dispense
int calcNumCoins (int c,int coin_Val, int total, void *servoTurn()){
int coin;
if (total / coin_Val >= 1 ){
coin = total / coin_Val;
if (coin<=c){
for (int i = 1; i <= coin; i++){
servoTurn(); //run of the servo function
}
total = total - (coin * coin_Val);
return(total);
}else if(coin>c){
for (int i = 1; i <= c; i++){
servoTurn(); //run of the servo function
}
total = total - (c * coin_Val);
return(total);
}else{
return(total);
}
}
}
//coin calculation to verify coin adequacy
int calculateCoin(int c,int total, int coin_val){
int tot,mult;
int coin=total / coin_val;
if (c>=coin){
mult=coin*coin_val;
tot= total-mult;
return(tot);
}else if(coin>c){
mult=c*coin_val;
tot=total-mult;
return(tot);
}else if(coin==0){
tot=total;
return(tot);
}
}
//dispensed coin deduction to coin savings
int coinDeduction(int c,int total, int coin_val){
int coin=total / coin_val;
int tot;
if (c>=coin){
int mult=coin*coin_val;
tot= total-mult;
return(coin);
}else if(coin>c){
int mult=c*coin_val;
tot=total-mult;
return(c);
}else if(coin==0){
tot=total;
return(0);
}
}