Skip to content

Instantly share code, notes, and snippets.

@ahmedlahrizi
Created September 7, 2021 21:27
Show Gist options
  • Save ahmedlahrizi/d665138b1261a3936464320a660e00c5 to your computer and use it in GitHub Desktop.
Save ahmedlahrizi/d665138b1261a3936464320a660e00c5 to your computer and use it in GitHub Desktop.
Code for a project
#include <Arduino.h>
#include <TimeLib.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#define CURRENT_TIMESTAMP 1631014463
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte smile[8] = { 0b00000, 0b00000, 0b01010, 0b00000, 0b10001, 0b01110, 0b00000, 0b00000 }; // :)
byte no_smile[8] = { 0b00000, 0b00000, 0b01010, 0b00000, 0b01110, 0b10001, 0b00000 ,0b00000 }; // :(
const unsigned short cups_to_drink_per_day = 6; // 1/4 L * 6 = 1.5L
short times[cups_to_drink_per_day] = {9, 11, 14, 16, 18, 20};
boolean times_drunk[cups_to_drink_per_day] = {false, false, false, false, false, false};
unsigned short cups_you_should_drink = 5;
const unsigned short green = 6;
const unsigned short red = 9;
const unsigned short buzzer = 8;
const unsigned short button = 7;
void alarm() {
for (unsigned short i = 444; i <= 700; ++i) {
tone(buzzer, i);
delay(1);
}
for (unsigned short i = 700; i >= 444; --i) {
tone(buzzer, i);
delay(1);
}
}
void panic(boolean panic = false) {
if (panic) {
analogWrite(green, 0);
analogWrite(red, 255);
} else {
analogWrite(red, 0);
analogWrite(green, 255);
}
}
void setup_lcd(unsigned short col = 0, unsigned short row = 0) {
lcd.clear();
lcd.setCursor(col, row);
}
void log_cups_to_drink(unsigned short amount_of_cups = 0) {
if (amount_of_cups == 0) {
panic(false);
setup_lcd();
lcd.print("You drank enough");
lcd.setCursor(7, 1);
lcd.write(1); // Happy :)
} else {
panic(true);
setup_lcd(0, 0);
lcd.print("You should ");
lcd.print("drink ");
lcd.setCursor(1, 1);
lcd.print(amount_of_cups);
lcd.print(" more");
lcd.print(amount_of_cups > 1 ? " cups " : " cup ");
lcd.write(2);
}
delay(300);
}
void input_buttons() {
if (digitalRead(button) && cups_you_should_drink > 0) {
delay(10);
--cups_you_should_drink;
log_cups_to_drink(cups_you_should_drink);
delay(10);
}
}
void setup() {
setTime(CURRENT_TIMESTAMP);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.createChar(1, smile);
lcd.createChar(2, no_smile);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(button, INPUT);
pinMode(buzzer, OUTPUT);
log_cups_to_drink(0);
}
void check_to_add_cup_to_drink() {
for (unsigned short i = 0; i < 8; ++i) {
if (hour() == times[i] && !(times_drunk[i])) {
times_drunk[i] = true;
++cups_you_should_drink;
log_cups_to_drink(cups_you_should_drink);
for (unsigned short j = 0; j < 5; ++j) {
alarm();
}
return;
}
}
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment