// include libraries
// libraries are pre-programmed codes that can be used when imported by user
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// initialise screen setup
// 0x27 is the I2C address of the LCD display
// 16 columns
// 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// declare and initialise integer variables
// declare means giving a name to a location in a computer's memory (example: f1, c1)
// initialise means setting a starting or initial value of the variables
int f1 = 0, f2 = 0, f3 = 0;
int numberOfTenCents = 0, numberOfTwentyCents = 0, numberOfFiftyCents = 0;
// void setup() is function of the code that run once when program starts
void setup() {
lcd.init(); // initialises LCD hardware
lcd.backlight(); // enables backlight of LCD
// setCursor(0,0) means set position at coordinate (0,0). (0,0) means top left
lcd.setCursor(0, 0);
lcd.print(" ARDUINO BASED "); // this text will be at top left of the screen
// setCursor(0,1) means set position at coordinate (0,1)
lcd.setCursor(0, 1);
lcd.print(" COIN SORTING ");
delay(2000); // delay for 2 seconds
lcd.clear(); // text on LCD screen is cleared
}
// void loop() is function of the code that runs repeatedly
void loop() {
// analogRead() is a function to read values from the Arduino pins
// the values of these functions are set to the s1 - s4 variables
int s1 = analogRead(A0);
int s2 = analogRead(A1);
int s3 = analogRead(A2);
// text position on the screen is set to top left
lcd.setCursor(0, 0);
lcd.print("RM 0.1 0.2 0.5");
// "if..else if" conditions are used to check for presence of each coins
if (s1 >= 200 && f1 == 0) {
f1 = 1;
} else if (s1 < 200 && f1 == 1) {
f1 = 0;
numberOfTenCents++;
}
if (s2 >= 200 && f2 == 0) {
f2 = 1;
} else if (s2 < 200 && f2 == 1) {
f2 = 0;
numberOfTwentyCents++;
}
if (s3 >= 200 && f3 == 0) {
f3 = 1;
} else if (s3 < 200 && f3 == 1) {
f3 = 0;
numberOfFiftyCents++;
}
// coordinate of c1 is set to (1,1) of the LCD
lcd.setCursor(1, 1);
lcd.print(numberOfTenCents);
// coordinate of c2 is set to (7,1) of the LCD
lcd.setCursor(7, 1);
lcd.print(numberOfTwentyCents);
// coordinate of numberOfFiftyCents is set to (14,1) of the LCD
lcd.setCursor(14, 1);
lcd.print(numberOfFiftyCents);
}