Rohan Barnwal
Published © GPL3+

Smart Glass Counter - Arduino Hydration Tracker Project

Stay hydrated, stay healthy - with an Arduino-powered reminder system.

IntermediateFull instructions provided1 hour23
Smart Glass Counter - Arduino Hydration Tracker Project

Things used in this project

Hardware components

UNO R4 WiFi
Arduino UNO R4 WiFi
×1
Jumper wires (generic)
Jumper wires (generic)
×1
IR Sensor
×1
XKC-Y26-V Water Level Sensor
×1
Buzzer
Buzzer
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code

Arduino
// Pin assignments
const int waterSensorPin = 2;
const int irSensorPin = 3;
const int buzzerPin = 8;

// Variables
int glassCount = 0;
bool alreadyCounted = false; // Prevents double counting
unsigned long lastReminderTime = 0;
bool reminderActive = true;

void setup() {
  pinMode(waterSensorPin, INPUT);
  pinMode(irSensorPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

  Serial.begin(9600);

  // Step 1: Power-on tone
  powerOnTone();
  delay(300);

  // Step 2: Activation tone
  activationTone();
}

void loop() {
  int waterState = digitalRead(waterSensorPin); // HIGH if water present
  int irState = digitalRead(irSensorPin);       // LOW if glass detected

  Serial.print("Water: ");
  Serial.print(waterState);
  Serial.print(" | IR: ");
  Serial.print(irState);
  Serial.print(" | Count: ");
  Serial.println(glassCount);

  // Step 3: Reminder tone every 3s if active
  if (reminderActive && millis() - lastReminderTime >= 3000) {
    reminderTone();
    lastReminderTime = millis();
  }

  // If glass detected, stop initial reminder
  if (irState == LOW && !alreadyCounted) {
    reminderActive = false;

    Serial.println("Glass detected, waiting before check...");
    delay(1000); // Settling time before starting the 3-second check

    // Re-read sensors after settling
    if (digitalRead(irSensorPin) == LOW) {
      Serial.println("Starting 3-second empty check...");
      bool cancelCount = false;

      for (int i = 0; i < 3; i++) { // Check for 3 seconds
        delay(1000);
        if (digitalRead(waterSensorPin) == HIGH) {
          Serial.println("Water detected — cancel count.");
          cancelCount = true;
          break;
        }
      }

      if (!cancelCount && digitalRead(waterSensorPin) == LOW) {
        glassCount++;
        alreadyCounted = true;
        Serial.print("✅ Glass counted! Total: ");
        Serial.println(glassCount);
        countTone();

        // If water still not detected after counting, restart reminder
        if (digitalRead(waterSensorPin) == LOW) {
          reminderActive = true;
          lastReminderTime = millis(); // Reset reminder timer
        }

        // Victory tone after 6 glasses
        if (glassCount >= 6) {
          victoryTone();
          glassCount = 0; // Reset after victory
          reminderActive = true; // Reactivate reminder after victory
        }
      }
    }
  }

  // Reset counting permission when glass removed or water present
  if (irState == HIGH || waterState == HIGH) {
    alreadyCounted = false;
  }

  delay(100); // Small polling delay
}

// --------- Tone Functions ----------
void powerOnTone() {
  tone(buzzerPin, 500, 200);
  delay(250);
  tone(buzzerPin, 700, 200);
  delay(250);
  noTone(buzzerPin);
}

void activationTone() {
  tone(buzzerPin, 1000, 200);
  delay(250);
  tone(buzzerPin, 1500, 200);
  delay(250);
  noTone(buzzerPin);
}

void reminderTone() {
  tone(buzzerPin, 400, 150);
  delay(150);
  noTone(buzzerPin);
}

void countTone() {
  tone(buzzerPin, 800, 200);
  delay(250);
  noTone(buzzerPin);
}

void victoryTone() {
  tone(buzzerPin, 800, 150);
  delay(200);
  tone(buzzerPin, 1000, 150);
  delay(200);
  tone(buzzerPin, 1200, 200);
  delay(250);
  noTone(buzzerPin);
}

Credits

Rohan Barnwal
31 projects • 35 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments