Professional Documents
Culture Documents
h>
#include <EEPROM.h>
#include <Wire.h>
#include <MenuBackend.h>
//Initialize LCD.
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//Menu variables.
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//initialize menuitems
MenuItem setupMode = MenuItem("setupMode");
MenuItem setupHours = MenuItem("setupHours");
MenuItem setupMinutes = MenuItem("setupMinutes");
MenuItem setupBottles = MenuItem("setupBottles");
MenuItem B1interval = MenuItem("B1interval");
MenuItem B1pills = MenuItem("B1pills");
MenuItem B2interval = MenuItem("B2interval");
MenuItem B2pills = MenuItem("B2pills");
MenuItem B3interval = MenuItem("B3interval");
MenuItem B3pills = MenuItem("B3pills");
void setup(){
//Wire handles communication with RTM.
Wire.begin();
//Serial is used for debugging purposes.
Serial.begin(9600);
//LCD is main output.
lcd.begin(16, 2);
//Set pins for LEDs.
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
//Control backlight.
pinMode(10, OUTPUT);
analogWrite(10, 100);
//Setup menu.
menu.getRoot().add(setupMode);
setupMode.add(setupBottles).addRight(setupHours).addRight(setupMinutes);
setupBottles.add(B1interval).addRight(B1pills).addRight(B2interval).addRight(B2pill
s).addRight(B3interval).addRight(B3pills);
menu.toRoot();
void loop(){
//Check alarms for each bottle.
checkAlarms();
//Manage GUI.
if(menu.getCurrent().getName()==menu.getRoot()){
readTime();
mainMenuAction();
}else if(menu.getCurrent().getName() == "setupHours" ||
menu.getCurrent().getName() == "setupMinutes"){
printTime();
}else if(menu.getCurrent().getName() == "B1interval"){
printNumber(EEPROM.read(ONE_PERIOD));
}else if(menu.getCurrent().getName() == "B1pills"){
printNumber(EEPROM.read(ONE_PILLS));
}else if(menu.getCurrent().getName() == "B2interval"){
printNumber(EEPROM.read(TWO_PERIOD));
}else if(menu.getCurrent().getName() == "B2pills"){
printNumber(EEPROM.read(TWO_PILLS));
}else if(menu.getCurrent().getName() == "B3interval"){
printNumber(EEPROM.read(THREE_PERIOD));
}else if(menu.getCurrent().getName() == "B3pills"){
printNumber(EEPROM.read(THREE_PILLS));
}else{
lcd.setCursor(0,0);
lcd.print(" ");
}
void navigateMenus(){
MenuItem currentMenu=menu.getCurrent();
switch (lcd_key)
{
case btnRIGHT:
{
menu.moveRight();
break;
}
case btnLEFT:
{
menu.moveLeft();
break;
}
case btnUP:
{
if(currentMenu.getName() == "setupHours"){
hour++;
if(hour>23){hour=0;}
setTime();
}else if(currentMenu.getName() == "setupMinutes"){
minute++;
if(minute>59){minute=0;}
setTime();
}else if(currentMenu.getName() == "B1pills"){
char newVal = EEPROM.read(ONE_PILLS);
newVal = newVal + 1;
if(newVal > 10){ newVal = 0;}
EEPROM.write(ONE_PILLS, (char)newVal);
}else if(currentMenu.getName() == "B1interval"){
char newVal = EEPROM.read(ONE_PERIOD);
newVal = newVal + 1;
if(newVal > 24){ newVal = 0;}
EEPROM.write(ONE_PERIOD, (char)newVal);
}else if(currentMenu.getName() == "B2pills"){
char newVal = EEPROM.read(TWO_PILLS);
newVal = newVal + 1;
if(newVal > 10){ newVal = 0;}
EEPROM.write(TWO_PILLS, (char)newVal);
}else if(currentMenu.getName() == "B2interval"){
char newVal = EEPROM.read(TWO_PERIOD);
newVal = newVal + 1;
if(newVal > 24){ newVal = 0;}
EEPROM.write(TWO_PERIOD, (char)newVal);
}else if(currentMenu.getName() == "B3pills"){
char newVal = EEPROM.read(THREE_PILLS);
newVal = newVal + 1;
if(newVal > 10){ newVal = 0;}
EEPROM.write(THREE_PILLS, (char)newVal);
}else if(currentMenu.getName() == "B3interval"){
char newVal = EEPROM.read(THREE_PERIOD);
newVal = newVal + 1;
if(newVal > 24){ newVal = 0;}
EEPROM.write(THREE_PERIOD, (char)newVal);
}else if(currentMenu.getName() == menu.getRoot()){
if(bottleReset == 1){
hourB1 = lastB1hour;
minsB1 = lastB1mins;
Serial.print("Last: ");
Serial.print(lastB1hour);
Serial.print(":");
Serial.println(lastB1mins);
Serial.print("Now: ");
Serial.print(hourB1);
Serial.print(":");
Serial.println(minsB1);
}
}
break;
}
case btnDOWN:
{
if(currentMenu.getName() == "setupHours"){
hour--;
if(hour<0){hour=23;}
setTime();
}else if(currentMenu.getName() == "setupMinutes"){
minute--;
if(minute<0){minute=59;}
setTime();
}else if(currentMenu.getName() == "B1pills"){
char newVal = EEPROM.read(ONE_PILLS);
newVal = newVal - 1;
if(newVal < 0){ newVal = 10;}
EEPROM.write(ONE_PILLS, (char)newVal);
}else if(currentMenu.getName() == "B1interval"){
char newVal = EEPROM.read(ONE_PERIOD);
newVal = newVal - 1;
if(newVal < 0){ newVal = 24;}
EEPROM.write(ONE_PERIOD, (char)newVal);
}else if(currentMenu.getName() == "B2pills"){
char newVal = EEPROM.read(TWO_PILLS);
newVal = newVal - 1;
if(newVal < 0){ newVal = 10;}
EEPROM.write(TWO_PILLS, (char)newVal);
}else if(currentMenu.getName() == "B2interval"){
char newVal = EEPROM.read(TWO_PERIOD);
newVal = newVal - 1;
if(newVal < 0){ newVal = 24;}
EEPROM.write(TWO_PERIOD, (char)newVal);
}else if(currentMenu.getName() == "B3pills"){
char newVal = EEPROM.read(THREE_PILLS);
newVal = newVal - 1;
if(newVal < 0){ newVal = 10;}
EEPROM.write(THREE_PILLS, (char)newVal);
}else if(currentMenu.getName() == "B3interval"){
char newVal = EEPROM.read(THREE_PERIOD);
newVal = newVal - 1;
if(newVal < 0){ newVal = 24;}
EEPROM.write(THREE_PERIOD, (char)newVal);
}
break;
}
case btnSELECT:
{
if(currentMenu.moveDown()){
menu.moveDown();
}else{
menu.use();
}
break;
}
case btnNONE:
{
break;
}
}
}
void checkAlarms(){
if(!b1){
if(minute == hourB1 && second >= minsB1){
b1 = true;
}
}
if(!b2){
if(minute == hourB2 && second >= minsB2){
b2 = true;
}
}
if(!b3){
if(minute == hourB3 && second >= minsB3){
b3 = true;
}
}
digitalWrite(11, b1);
digitalWrite(12, b2);
digitalWrite(13, b3);
}
//Print time.
void printTime(){
lcd.setCursor(0,0);
lcd.print(" ");
if (hour < 10){lcd.print("0");}
lcd.print(hour, DEC);
lcd.print(":");
if (minute < 10){lcd.print("0");}
lcd.print(minute, DEC);
lcd.print(":");
if (second < 10){lcd.print("0");}
lcd.print(second, DEC);
lcd.print(" ");
}
//Print a number.
void printNumber(int number){
lcd.setCursor(0,0);
lcd.print(" ");
if(number/10 == 0){
lcd.print("0");
}else{
lcd.print(" ");
}
lcd.print(number);
lcd.print(" ");
}
//Usage controls.
void menuUsed(MenuUseEvent used){
menu.toRoot(); //back to Main
}
//Find out which bottle(s) are out of the box, print instructions.
if(bottle1 < 800){
b1 = false;
char nextIn = EEPROM.read(ONE_PERIOD);
curr = 1;
printInstructions(EEPROM.read(ONE_PILLS), nextIn);
setAlarm(1,nextIn);
} else if(bottle2 < 800){
b2 = false;
curr = 2;
char nextIn = EEPROM.read(TWO_PERIOD);
printInstructions(EEPROM.read(TWO_PILLS), nextIn);
setAlarm(2,nextIn);
}else if(bottle3 < 800){
curr = 3;
b3 = false;
char nextIn = EEPROM.read(THREE_PERIOD);
printInstructions(EEPROM.read(THREE_PILLS), nextIn);
setAlarm(3,nextIn);
}else{
if(lastBottle != 0){
lcd.clear();
lastBottle = 0;
}
printTime();
}
}
//Print instructions for each bottle.
void printInstructions(int pills, int period){
//Don't clear if bottle hasn't changed.
if(lastBottle != curr){
lcd.clear();
}
lastBottle = curr;
bottleReset = curr;
lcd.setCursor(0,0);
lcd.print("Take ");
lcd.print(pills);
lcd.print(" pill(s)");
lcd.setCursor(0,1);
lcd.print("every ");
lcd.print(period);
lcd.print(" hours.");
return;
}