#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo servoA;
Servo servoB;
Servo servoC;
int led1 = 0;
int led2 = 1;
int led3 = 2;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
const byte ROWS =4;
const byte COLS =4;
char hexaKeys[ROWS][COLS] ={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] ={10,9,8,7};
byte colPins[COLS] ={6,5,4,3};
Keypad Tastenfeld=Keypad(
makeKeymap(hexaKeys),rowPins,colPins,ROWS,COLS);
char InputA;
char InputB;
int C_per;
int A_per;
int B_per;
bool flag=true;
long A_times[10] = {};
long B_times[10] = {};
long C_times[10] = {};
int PillA_amt = 100;
int PillB_amt = 100;
int PillC_amt = 100;
long counter = 0;
long seconds;
long minutes;
long hours;
long set_a_time(String prompt = "Enter Time:"){
lcd.clear();
String time = "";
String eingabe;
char Taste;
long return_value;
bool first = true;
//SET THE TIME
lcd.print(prompt);
lcd.setCursor(0, 1);
lcd.print("18:30 as 1830!");
lcd.setCursor(0, 0);
//loop until he entered a time
while(time == "") {
flag=true;
eingabe = "";
lcd.print(prompt);
lcd.setCursor(0, 1);
//loop until there are 4 key inputs
for(int i = 0; i < 4;){
Taste = Tastenfeld.getKey();
//If key is pressed, check if it is the first key
//and clear the lcd if thats the case
if(Taste){
if(first){
lcd.print(" ");
lcd.setCursor(0, 1);
first = false;
}
lcd.print(Taste);
eingabe += Taste;
i++;
}
}
lcd.clear();
lcd.setCursor(0,0);
long timing = eingabe.toInt();
String minute_str = eingabe.substring(2,4);
String hour_str = eingabe.substring(0,2);
long hour_check = hour_str.toInt();
long minute_check = minute_str.toInt();
return_value = hour_check*3600 + minute_check*60;
//check if the time is valid
if(timing<2401&&minute_check<60){
//if it is ask user to save time
lcd.print("Save time with *");
lcd.setCursor(0,1);
if(hour_check<10){
lcd.print(0);
}
lcd.print(hour_check);
lcd.print(":");
if(minute_check<10){
lcd.print(0);
}
lcd.print(minute_check);
while(flag) {
Taste = Tastenfeld.getKey();
if(Taste) {
if(Taste == '*') {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time saved");
delay(1000);
lcd.clear();
time = eingabe;
flag=false;
}//closes if(Taste == '*')
else if(Taste == '#') {
time ="";
lcd.clear();
lcd.setCursor(0,0);
flag=false;
}//closes else if ()
}//closes if(Taste)
}//closes while(flag)
}//closes if(timing<2401)
else{
time ="";
lcd.clear();
lcd.setCursor(0,0);
}//closes else
}//closes while(time == "")
return(return_value);
}
void setup() {
servoA.write(0);
servoB.write(0);
servoC.write(0);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// put your setup code here, to run once:
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// set up all the LEDs as OUTPUT
//Set the pill timings
delay(10);
lcd.setCursor(0,0);
lcd.print("How often do");
lcd.setCursor(0,1);
lcd.print("you take Pill A?");
for(int i=0;i<3;){
InputA = Tastenfeld.getKey();
lcd.setCursor(14,1);
if(i==2 && InputA){
C_per = InputA - 48;
i++;
}
if(i==1 && InputA){
B_per = InputA - 48;
lcd.print("C?");
i++;
}
if(i==0 && InputA){
A_per = InputA - 48;
lcd.print("B?");
i++;
}
}
lcd.clear();
for(int i = 0;i<A_per;i++){
A_times[i] = set_a_time("Pill A) "+String(i+1)+". time.");
}
for(int i = 0;i<B_per;i++){
B_times[i] = set_a_time("Pill B) "+String(i+1)+". time.");
}
for(int i = 0;i<C_per;i++){
C_times[i] = set_a_time("Pill C) "+String(i+1)+". time.");
}
lcd.setCursor(0,0);
lcd.print("How many Pill A");
lcd.setCursor(0,1);
lcd.print("did you put in?");
for(int i=0;i<3;i++){
bool confirmed = true;
bool fir = true;
String number = "";
while(confirmed){
InputB = Tastenfeld.getKey();
if(InputB){
if(fir){
lcd.clear();
lcd.print("Confirm with *");
lcd.setCursor(0,1);
fir=false;
}
lcd.print(InputB);
number+=InputB;
if(i==2 && InputB == '*'){
PillC_amt = number.toInt();
confirmed=false;
}
if(i==1 && InputB == '*'){
PillB_amt = number.toInt();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("How many Pill C");
lcd.setCursor(0,1);
lcd.print("did you put in?");
confirmed=false;
}
if(i==0 && InputB == '*'){
PillA_amt = number.toInt();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("How many Pill B");
lcd.setCursor(0,1);
lcd.print("did you put in?");
confirmed=false;
}
}
}
}
lcd.clear();
counter = set_a_time();
lcd.clear();
}
void loop() {
counter = counter+1;
seconds = counter;
hours = seconds/3600;
seconds = seconds%3600;
minutes = seconds/60;
seconds = seconds%60;
delay(1000);
lcd.setCursor(4,0);
if(hours<10){
lcd.print(0);
}
lcd.print(hours);
lcd.print(":");
if(minutes<10){
lcd.print(0);
}
lcd.print(minutes);
lcd.print(":");
if(seconds<10){
lcd.print(0);
}
lcd.print(seconds);
lcd.print(" ");
lcd.setCursor(0,1);
if(PillA_amt <= 10){
digitalWrite(led1, HIGH);
}
if(PillB_amt <= 10){
digitalWrite(led2, HIGH);
}
if(PillC_amt <= 10){
digitalWrite(led3, HIGH);
}
for(int i = 0; i<A_per; i++){
if(A_times[i] == counter){
servoA.attach(11);
servoA.write(180);
delay(2000);
servoA.write(90);
delay(2000);
PillA_amt--;
servoA.detach();
}
}
for(int i = 0; i<B_per; i++){
if(B_times[i] == counter){
servoB.attach(12);
servoB.write(180);
delay(2000);
servoB.write(90);
delay(2000);
PillB_amt--;
servoB.detach();
}
}
for(int i = 0; i<C_per; i++){
if(C_times[i] == counter){
servoC.attach(13);
servoC.write(180);
delay(2000);
servoC.write(0);
delay(2000);
PillC_amt--;
servoC.detach();
}
}
}
/*
#include <Servo.h>
Servo myservo;
Servo myservo2;
Servo myservo3;
int pos = 0;
void setup() {
myservo.attach(11);
myservo2.attach(12);
myservo3.attach(13);
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
myservo2.write(pos);
myservo3.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
myservo2.write(pos);
myservo3.write(pos);
delay(15);
}
}
*/
Comments