Smart Glass Counter - A Step-by-Step Arduino Hydration Tracking Project
Stay hydrated, stay healthy - with an Arduino-powered reminder system.
Components and supplies
1
XKC-Y26-V Contactless Liquid Level Sensor
1
IR Obstacle Sensor
1
Jumper wires (generic)
1
Piezo Buzzer
1
Arduino UNO R4 WiFi LED Matrix
Apps and platforms
1
Arduino IDE 2.0 (beta)
Project description
Code
Arduino Code
c
1// Pin assignments 2const int waterSensorPin = 2; 3const int irSensorPin = 3; 4const int buzzerPin = 8; 5 6// Variables 7int glassCount = 0; 8bool alreadyCounted = false; // Prevents double counting 9unsigned long lastReminderTime = 0; 10bool reminderActive = true; 11 12void setup() { 13 pinMode(waterSensorPin, INPUT); 14 pinMode(irSensorPin, INPUT); 15 pinMode(buzzerPin, OUTPUT); 16 17 Serial.begin(9600); 18 19 // Step 1: Power-on tone 20 powerOnTone(); 21 delay(300); 22 23 // Step 2: Activation tone 24 activationTone(); 25} 26 27void loop() { 28 int waterState = digitalRead(waterSensorPin); // HIGH if water present 29 int irState = digitalRead(irSensorPin); // LOW if glass detected 30 31 Serial.print("Water: "); 32 Serial.print(waterState); 33 Serial.print(" | IR: "); 34 Serial.print(irState); 35 Serial.print(" | Count: "); 36 Serial.println(glassCount); 37 38 // Step 3: Reminder tone every 3s if active 39 if (reminderActive && millis() - lastReminderTime >= 3000) { 40 reminderTone(); 41 lastReminderTime = millis(); 42 } 43 44 // If glass detected, stop initial reminder 45 if (irState == LOW && !alreadyCounted) { 46 reminderActive = false; 47 48 Serial.println("Glass detected, waiting before check..."); 49 delay(1000); // Settling time before starting the 3-second check 50 51 // Re-read sensors after settling 52 if (digitalRead(irSensorPin) == LOW) { 53 Serial.println("Starting 3-second empty check..."); 54 bool cancelCount = false; 55 56 for (int i = 0; i < 3; i++) { // Check for 3 seconds 57 delay(1000); 58 if (digitalRead(waterSensorPin) == HIGH) { 59 Serial.println("Water detected — cancel count."); 60 cancelCount = true; 61 break; 62 } 63 } 64 65 if (!cancelCount && digitalRead(waterSensorPin) == LOW) { 66 glassCount++; 67 alreadyCounted = true; 68 Serial.print("✅ Glass counted! Total: "); 69 Serial.println(glassCount); 70 countTone(); 71 72 // If water still not detected after counting, restart reminder 73 if (digitalRead(waterSensorPin) == LOW) { 74 reminderActive = true; 75 lastReminderTime = millis(); // Reset reminder timer 76 } 77 78 // Victory tone after 6 glasses 79 if (glassCount >= 6) { 80 victoryTone(); 81 glassCount = 0; // Reset after victory 82 reminderActive = true; // Reactivate reminder after victory 83 } 84 } 85 } 86 } 87 88 // Reset counting permission when glass removed or water present 89 if (irState == HIGH || waterState == HIGH) { 90 alreadyCounted = false; 91 } 92 93 delay(100); // Small polling delay 94} 95 96// --------- Tone Functions ---------- 97void powerOnTone() { 98 tone(buzzerPin, 500, 200); 99 delay(250); 100 tone(buzzerPin, 700, 200); 101 delay(250); 102 noTone(buzzerPin); 103} 104 105void activationTone() { 106 tone(buzzerPin, 1000, 200); 107 delay(250); 108 tone(buzzerPin, 1500, 200); 109 delay(250); 110 noTone(buzzerPin); 111} 112 113void reminderTone() { 114 tone(buzzerPin, 400, 150); 115 delay(150); 116 noTone(buzzerPin); 117} 118 119void countTone() { 120 tone(buzzerPin, 800, 200); 121 delay(250); 122 noTone(buzzerPin); 123} 124 125void victoryTone() { 126 tone(buzzerPin, 800, 150); 127 delay(200); 128 tone(buzzerPin, 1000, 150); 129 delay(200); 130 tone(buzzerPin, 1200, 200); 131 delay(250); 132 noTone(buzzerPin); 133}
Comments
Only logged in users can leave comments