Components and supplies
1
Push Button
1
Arduino Nano R3
1
Scale with HX711 interface
1
OLED 128x32 Display
Project description
Code
Coin Counter Arduino Code
arduino
1// 2// FILE: Coin_Count.ino 3// AUTHOR: Carlo Stramaglia 4// URL: https://www.youtube.com/c/CarloStramaglia 5// 6 7 8#include "HX711.h" 9#include <U8x8lib.h> 10#include "OneButton.h" 11 12HX711 scale; 13 14uint8_t dataPin = 6; 15uint8_t clockPin = 7; 16OneButton button(4, true); 17U8X8_SSD1306_128X32_UNIVISION_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); 18 19int status = 0; 20float Cent = 0; 21float TwoCent = 0; 22float FiveCent = 0; 23float TenCent = 0; 24float TwentyCent = 0; 25float FiftyCent = 0; 26float OneEuro = 0; 27float TwoEuro = 0; 28int TotalCents = 0; 29float TotalEuro = 0; 30 31 32 33void setup() 34{ 35 Serial.begin(115200); 36 scale.begin(dataPin, clockPin); 37 u8x8.begin(); 38 u8x8.setPowerSave(0); 39 button.attachClick(Click); 40} 41 42void loop() 43{ 44 char ValueEuro [8]; 45 button.tick(); 46 u8x8.setFont(u8x8_font_chroma48medium8_r); 47 delay (50); 48 49 if (status == 0) { 50 Serial.println("\ 51Empty the scale and press the button to continue"); 52 u8x8.drawString(0,1,"Empty scale"); 53 u8x8.drawString(0,2,"Press button"); 54 u8x8.drawString(0,3,"To Continue"); 55 } 56 if (status == 1) { 57 Serial.println("\ 58Place 4 coins of 2 and press the button to continue"); 59 u8x8.drawString(0,1,"Put 4 2E coins"); 60 u8x8.drawString(0,2,"Press button"); 61 u8x8.drawString(0,3,"To Continue"); 62 } 63 if (status == 2) { 64 Serial.println("\ 65Place 1 Cent coins and press the button to continue"); 66 u8x8.drawString(0,1,"Put 1 cent coins"); 67 u8x8.drawString(0,2,"Press button"); 68 u8x8.drawString(0,3,"To Continue"); 69 } 70 if (status == 3) { 71 Serial.println("\ 72Add 2 Cents coins and press the button to continue"); 73 u8x8.drawString(0,1,"Put 2 cent coins"); 74 u8x8.drawString(0,2,"Press button"); 75 u8x8.drawString(0,3,"To Continue"); 76 } 77 if (status == 4) { 78 Serial.println("\ 79Add 5 Cents coins and press the button to continue"); 80 u8x8.drawString(0,1,"Put 5 cent coins"); 81 u8x8.drawString(0,2,"Press button"); 82 u8x8.drawString(0,3,"To Continue"); 83 } 84 if (status == 5) { 85 Serial.println("\ 86Add 10 Cents coins and press the button to continue"); 87 u8x8.drawString(0,1,"Put 10 cent coin"); 88 u8x8.drawString(0,2,"Press button"); 89 u8x8.drawString(0,3,"To Continue"); 90 } 91 if (status == 6) { 92 Serial.println("\ 93Add 20 Cents coins and press the button to continue"); 94 u8x8.drawString(0,1,"Put 20 cent coin"); 95 u8x8.drawString(0,2,"Press button"); 96 u8x8.drawString(0,3,"To Continue"); 97 } 98 if (status == 7) { 99 Serial.println("\ 100Add 50 Cents coins and press the button to continue"); 101 u8x8.drawString(0,1,"Put 50 cent coin"); 102 u8x8.drawString(0,2,"Press button"); 103 u8x8.drawString(0,3,"To Continue"); 104 } 105 if (status == 8) { 106 Serial.println("\ 107Add 1 Euro coins and press the button to continue"); 108 u8x8.drawString(0,1,"Put 1 Euro coins"); 109 u8x8.drawString(0,2,"Press button"); 110 u8x8.drawString(0,3,"To Continue"); 111 } 112 if (status == 9) { 113 Serial.println("\ 114Add 2 Euro coins and press the button to continue"); 115 u8x8.drawString(0,1,"Put 2 Euro coins"); 116 u8x8.drawString(0,2,"Press button"); 117 u8x8.drawString(0,3,"To Continue"); 118 } 119 if (status == 10) { 120 sprintf(ValueEuro, "%d", TotalEuro); 121 dtostrf(TotalEuro, 2, 2, ValueEuro); 122 u8x8.drawString(0,1,ValueEuro); 123 ValueEuro[5]=byte(0xb0); 124 ValueEuro[6]='C'; 125 ValueEuro[7]='\\0'; 126 u8x8.drawString(0,2," "); 127 u8x8.drawString(0,3,"End of process"); 128 Serial.println(ValueEuro); 129 } 130 131// Serial.print("UNITS: "); 132// Serial.println(scale.get_units(10)); 133// delay(150); 134} 135 136void Click() { 137 Serial.println ("Button pressed"); 138 if (status == 9) { 139 Serial.read(); 140 Serial.print("2 Euro Coins float: "); 141 TwoEuro = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent-TwentyCent-FiftyCent-OneEuro; 142 TotalCents = TotalCents + ((int (TwoEuro/8.50+0.5))*200); 143 TotalEuro = (float) TotalCents/100; 144 Serial.print("Total Cents so far: "); 145 Serial.println(TotalEuro); 146 status = 10; 147 Serial.print(""); 148 } 149 else if (status == 8) { 150 Serial.read(); 151 Serial.print("1 Euro Coins float: "); 152 OneEuro = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent-TwentyCent-FiftyCent; 153 TotalCents = TotalCents + ((int (OneEuro/7.50+0.5))*100); 154 Serial.print("Total Cents so far: "); 155 Serial.println(TotalCents); 156 status = 9; 157 Serial.print(""); 158 } 159 else if (status == 7) { 160 Serial.read(); 161 Serial.print("50 Cent Coins float: "); 162 FiftyCent = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent-TwentyCent; 163 TotalCents = TotalCents + ((int (FiftyCent/7.80+0.5))*50); 164 Serial.print("Total Cents so far: "); 165 Serial.println(TotalCents); 166 status = 8; 167 Serial.print(""); 168 } 169 else if (status == 6) { 170 Serial.read(); 171 Serial.print("20 Cent Coins float: "); 172 TwentyCent = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent; 173 TotalCents = TotalCents + ((int (TwentyCent/5.74+0.5))*20); 174 Serial.print("Total Cents so far: "); 175 Serial.println(TotalCents); 176 status = 7; 177 Serial.print(""); 178 } 179 else if (status == 5) { 180 Serial.read(); 181 Serial.print("10 Cent Coins float: "); 182 TenCent = scale.get_units(10)-Cent-TwoCent-FiveCent; 183 TotalCents = TotalCents + ((int (TenCent/4.1+0.5))*10); 184 Serial.print("Total Cents so far: "); 185 Serial.println(TotalCents); 186 status = 6; 187 Serial.print(""); 188 } 189 else if (status == 4) { 190 Serial.read(); 191 Serial.print("5 Cent Coins float: "); 192 FiveCent = scale.get_units(10)-Cent-TwoCent; 193 TotalCents = TotalCents + ((int (FiveCent/3.92+0.5))*5); 194 Serial.print("Total Cents so far: "); 195 Serial.println(TotalCents); 196 status = 5; 197 Serial.print(""); 198 } 199 else if (status == 3) { 200 Serial.read(); 201 Serial.print("2 Cent Coins float: "); 202 TwoCent = scale.get_units(10)-Cent; 203 TotalCents = TotalCents + ((int (TwoCent/3.06+0.5))*2); 204 Serial.print("Total Cents so far: "); 205 Serial.println(TotalCents); 206 status = 4; 207 Serial.print(""); 208 } 209 else if (status == 2) { 210 Serial.read(); 211 Serial.print("1 Cent Coins float: "); 212 Cent = scale.get_units(10); 213 TotalCents = int (Cent/2.30+0.5); 214 Serial.print("Total Cents so far: "); 215 Serial.println(TotalCents); 216 status = 3; 217 Serial.print(""); 218 } 219 else if (status == 1) { 220 Serial.read(); 221 scale.callibrate_scale(34, 5); 222 Serial.print("Grams: "); 223 Serial.println(scale.get_units(10)); 224 status = 2; 225 Serial.print("Calibrated"); 226 } 227 else if (status == 0) { 228 Serial.read(); 229 scale.tare(); 230 Serial.print("Grams: "); 231 Serial.println(scale.get_units(10)); 232 status = 1; 233 } 234 235 236} 237
Coin Counter Arduino Code
arduino
1// 2// FILE: Coin_Count.ino 3// AUTHOR: Carlo Stramaglia 4// URL: https://www.youtube.com/c/CarloStramaglia 5// 6 7 8#include "HX711.h" 9#include <U8x8lib.h> 10#include "OneButton.h" 11 12HX711 scale; 13 14uint8_t dataPin = 6; 15uint8_t clockPin = 7; 16OneButton button(4, true); 17U8X8_SSD1306_128X32_UNIVISION_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); 18 19int status = 0; 20float Cent = 0; 21float TwoCent = 0; 22float FiveCent = 0; 23float TenCent = 0; 24float TwentyCent = 0; 25float FiftyCent = 0; 26float OneEuro = 0; 27float TwoEuro = 0; 28int TotalCents = 0; 29float TotalEuro = 0; 30 31 32 33void setup() 34{ 35 Serial.begin(115200); 36 scale.begin(dataPin, clockPin); 37 u8x8.begin(); 38 u8x8.setPowerSave(0); 39 button.attachClick(Click); 40} 41 42void loop() 43{ 44 char ValueEuro [8]; 45 button.tick(); 46 u8x8.setFont(u8x8_font_chroma48medium8_r); 47 delay (50); 48 49 if (status == 0) { 50 Serial.println("\ 51Empty the scale and press the button to continue"); 52 u8x8.drawString(0,1,"Empty scale"); 53 u8x8.drawString(0,2,"Press button"); 54 u8x8.drawString(0,3,"To Continue"); 55 } 56 if (status == 1) { 57 Serial.println("\ 58Place 4 coins of 2 and press the button to continue"); 59 u8x8.drawString(0,1,"Put 4 2E coins"); 60 u8x8.drawString(0,2,"Press button"); 61 u8x8.drawString(0,3,"To Continue"); 62 } 63 if (status == 2) { 64 Serial.println("\ 65Place 1 Cent coins and press the button to continue"); 66 u8x8.drawString(0,1,"Put 1 cent coins"); 67 u8x8.drawString(0,2,"Press button"); 68 u8x8.drawString(0,3,"To Continue"); 69 } 70 if (status == 3) { 71 Serial.println("\ 72Add 2 Cents coins and press the button to continue"); 73 u8x8.drawString(0,1,"Put 2 cent coins"); 74 u8x8.drawString(0,2,"Press button"); 75 u8x8.drawString(0,3,"To Continue"); 76 } 77 if (status == 4) { 78 Serial.println("\ 79Add 5 Cents coins and press the button to continue"); 80 u8x8.drawString(0,1,"Put 5 cent coins"); 81 u8x8.drawString(0,2,"Press button"); 82 u8x8.drawString(0,3,"To Continue"); 83 } 84 if (status == 5) { 85 Serial.println("\ 86Add 10 Cents coins and press the button to continue"); 87 u8x8.drawString(0,1,"Put 10 cent coin"); 88 u8x8.drawString(0,2,"Press button"); 89 u8x8.drawString(0,3,"To Continue"); 90 } 91 if (status == 6) { 92 Serial.println("\ 93Add 20 Cents coins and press the button to continue"); 94 u8x8.drawString(0,1,"Put 20 cent coin"); 95 u8x8.drawString(0,2,"Press button"); 96 u8x8.drawString(0,3,"To Continue"); 97 } 98 if (status == 7) { 99 Serial.println("\ 100Add 50 Cents coins and press the button to continue"); 101 u8x8.drawString(0,1,"Put 50 cent coin"); 102 u8x8.drawString(0,2,"Press button"); 103 u8x8.drawString(0,3,"To Continue"); 104 } 105 if (status == 8) { 106 Serial.println("\ 107Add 1 Euro coins and press the button to continue"); 108 u8x8.drawString(0,1,"Put 1 Euro coins"); 109 u8x8.drawString(0,2,"Press button"); 110 u8x8.drawString(0,3,"To Continue"); 111 } 112 if (status == 9) { 113 Serial.println("\ 114Add 2 Euro coins and press the button to continue"); 115 u8x8.drawString(0,1,"Put 2 Euro coins"); 116 u8x8.drawString(0,2,"Press button"); 117 u8x8.drawString(0,3,"To Continue"); 118 } 119 if (status == 10) { 120 sprintf(ValueEuro, "%d", TotalEuro); 121 dtostrf(TotalEuro, 2, 2, ValueEuro); 122 u8x8.drawString(0,1,ValueEuro); 123 ValueEuro[5]=byte(0xb0); 124 ValueEuro[6]='C'; 125 ValueEuro[7]='\\0'; 126 u8x8.drawString(0,2," "); 127 u8x8.drawString(0,3,"End of process"); 128 Serial.println(ValueEuro); 129 } 130 131// Serial.print("UNITS: "); 132// Serial.println(scale.get_units(10)); 133// delay(150); 134} 135 136void Click() { 137 Serial.println ("Button pressed"); 138 if (status == 9) { 139 Serial.read(); 140 Serial.print("2 Euro Coins float: "); 141 TwoEuro = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent-TwentyCent-FiftyCent-OneEuro; 142 TotalCents = TotalCents + ((int (TwoEuro/8.50+0.5))*200); 143 TotalEuro = (float) TotalCents/100; 144 Serial.print("Total Cents so far: "); 145 Serial.println(TotalEuro); 146 status = 10; 147 Serial.print(""); 148 } 149 else if (status == 8) { 150 Serial.read(); 151 Serial.print("1 Euro Coins float: "); 152 OneEuro = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent-TwentyCent-FiftyCent; 153 TotalCents = TotalCents + ((int (OneEuro/7.50+0.5))*100); 154 Serial.print("Total Cents so far: "); 155 Serial.println(TotalCents); 156 status = 9; 157 Serial.print(""); 158 } 159 else if (status == 7) { 160 Serial.read(); 161 Serial.print("50 Cent Coins float: "); 162 FiftyCent = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent-TwentyCent; 163 TotalCents = TotalCents + ((int (FiftyCent/7.80+0.5))*50); 164 Serial.print("Total Cents so far: "); 165 Serial.println(TotalCents); 166 status = 8; 167 Serial.print(""); 168 } 169 else if (status == 6) { 170 Serial.read(); 171 Serial.print("20 Cent Coins float: "); 172 TwentyCent = scale.get_units(10)-Cent-TwoCent-FiveCent-TenCent; 173 TotalCents = TotalCents + ((int (TwentyCent/5.74+0.5))*20); 174 Serial.print("Total Cents so far: "); 175 Serial.println(TotalCents); 176 status = 7; 177 Serial.print(""); 178 } 179 else if (status == 5) { 180 Serial.read(); 181 Serial.print("10 Cent Coins float: "); 182 TenCent = scale.get_units(10)-Cent-TwoCent-FiveCent; 183 TotalCents = TotalCents + ((int (TenCent/4.1+0.5))*10); 184 Serial.print("Total Cents so far: "); 185 Serial.println(TotalCents); 186 status = 6; 187 Serial.print(""); 188 } 189 else if (status == 4) { 190 Serial.read(); 191 Serial.print("5 Cent Coins float: "); 192 FiveCent = scale.get_units(10)-Cent-TwoCent; 193 TotalCents = TotalCents + ((int (FiveCent/3.92+0.5))*5); 194 Serial.print("Total Cents so far: "); 195 Serial.println(TotalCents); 196 status = 5; 197 Serial.print(""); 198 } 199 else if (status == 3) { 200 Serial.read(); 201 Serial.print("2 Cent Coins float: "); 202 TwoCent = scale.get_units(10)-Cent; 203 TotalCents = TotalCents + ((int (TwoCent/3.06+0.5))*2); 204 Serial.print("Total Cents so far: "); 205 Serial.println(TotalCents); 206 status = 4; 207 Serial.print(""); 208 } 209 else if (status == 2) { 210 Serial.read(); 211 Serial.print("1 Cent Coins float: "); 212 Cent = scale.get_units(10); 213 TotalCents = int (Cent/2.30+0.5); 214 Serial.print("Total Cents so far: "); 215 Serial.println(TotalCents); 216 status = 3; 217 Serial.print(""); 218 } 219 else if (status == 1) { 220 Serial.read(); 221 scale.callibrate_scale(34, 5); 222 Serial.print("Grams: "); 223 Serial.println(scale.get_units(10)); 224 status = 2; 225 Serial.print("Calibrated"); 226 } 227 else if (status == 0) { 228 Serial.read(); 229 scale.tare(); 230 Serial.print("Grams: "); 231 Serial.println(scale.get_units(10)); 232 status = 1; 233 } 234 235 236} 237
Downloadable files
Diagram of the Coin Counter project
Diagram of the Coin Counter project
Comments
Only logged in users can leave comments