The Pill Dispenser 1.0
Automatic pill dispenser set for certain time intervals, with function for reminding to replace pills and buzzer.
Components and supplies
1
Buzzer, Piezo
3
Positional Micro Servo
4
Resistor 220 ohm
1
Quad OR Gate
1
Alphanumeric LCD, 16 x 2
3
5 mm LED: Red
1
Arduino Leonardo
1
Grove - 12-Channel Capacitive Touch Keypad (ATtiny1616)
Apps and platforms
1
Tinkercad
Project description
Code
Code for the pill dispenser
c_cpp
1#include <Keypad.h> 2#include <LiquidCrystal.h> 3#include <Servo.h> 4 5Servo servoA; 6Servo servoB; 7Servo servoC; 8 9int led1 = 0; 10int led2 = 1; 11int led3 = 2; 12 13LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); 14 15const byte ROWS =4; 16const byte COLS =4; 17char hexaKeys[ROWS][COLS] ={ 18 {'1','2','3','A'}, 19 {'4','5','6','B'}, 20 {'7','8','9','C'}, 21 {'*','0','#','D'} 22}; 23byte rowPins[ROWS] ={10,9,8,7}; 24byte colPins[COLS] ={6,5,4,3}; 25 26Keypad Tastenfeld=Keypad( 27 makeKeymap(hexaKeys),rowPins,colPins,ROWS,COLS); 28 29char InputA; 30char InputB; 31int C_per; 32int A_per; 33int B_per; 34bool flag=true; 35long A_times[10] = {}; 36long B_times[10] = {}; 37long C_times[10] = {}; 38int PillA_amt = 100; 39int PillB_amt = 100; 40int PillC_amt = 100; 41long counter = 0; 42long seconds; 43long minutes; 44long hours; 45 46long set_a_time(String prompt = "Enter Time:"){ 47 lcd.clear(); 48 String time = ""; 49 String eingabe; 50 char Taste; 51 long return_value; 52 bool first = true; 53 //SET THE TIME 54 lcd.print(prompt); 55 lcd.setCursor(0, 1); 56 lcd.print("18:30 as 1830!"); 57 lcd.setCursor(0, 0); 58 //loop until he entered a time 59 while(time == "") { 60 flag=true; 61 eingabe = ""; 62 lcd.print(prompt); 63 lcd.setCursor(0, 1); 64 //loop until there are 4 key inputs 65 for(int i = 0; i < 4;){ 66 Taste = Tastenfeld.getKey(); 67 //If key is pressed, check if it is the first key 68 //and clear the lcd if thats the case 69 if(Taste){ 70 if(first){ 71 lcd.print(" "); 72 lcd.setCursor(0, 1); 73 first = false; 74 } 75 lcd.print(Taste); 76 eingabe += Taste; 77 i++; 78 } 79 } 80 lcd.clear(); 81 lcd.setCursor(0,0); 82 long timing = eingabe.toInt(); 83 String minute_str = eingabe.substring(2,4); 84 String hour_str = eingabe.substring(0,2); 85 long hour_check = hour_str.toInt(); 86 long minute_check = minute_str.toInt(); 87 return_value = hour_check*3600 + minute_check*60; 88 //check if the time is valid 89 if(timing<2401&&minute_check<60){ 90 //if it is ask user to save time 91 lcd.print("Save time with *"); 92 lcd.setCursor(0,1); 93 if(hour_check<10){ 94 lcd.print(0); 95 } 96 lcd.print(hour_check); 97 lcd.print(":"); 98 if(minute_check<10){ 99 lcd.print(0); 100 } 101 lcd.print(minute_check); 102 while(flag) { 103 Taste = Tastenfeld.getKey(); 104 if(Taste) { 105 if(Taste == '*') { 106 lcd.clear(); 107 lcd.setCursor(0,0); 108 lcd.print("Time saved"); 109 delay(1000); 110 lcd.clear(); 111 time = eingabe; 112 flag=false; 113 }//closes if(Taste == '*') 114 else if(Taste == '#') { 115 time =""; 116 lcd.clear(); 117 lcd.setCursor(0,0); 118 flag=false; 119 }//closes else if () 120 }//closes if(Taste) 121 }//closes while(flag) 122 }//closes if(timing<2401) 123 else{ 124 time =""; 125 lcd.clear(); 126 lcd.setCursor(0,0); 127 }//closes else 128 }//closes while(time == "") 129 return(return_value); 130} 131 132void setup() { 133 servoA.write(0); 134 servoB.write(0); 135 servoC.write(0); 136 pinMode(led1, OUTPUT); 137 pinMode(led2, OUTPUT); 138 pinMode(led3, OUTPUT); 139 // put your setup code here, to run once: 140 // set up the LCD's number of columns and rows: 141 lcd.begin(16, 2); 142 // set up all the LEDs as OUTPUT 143//Set the pill timings 144 delay(10); 145 lcd.setCursor(0,0); 146 lcd.print("How often do"); 147 lcd.setCursor(0,1); 148 lcd.print("you take Pill A?"); 149 for(int i=0;i<3;){ 150 InputA = Tastenfeld.getKey(); 151 lcd.setCursor(14,1); 152 if(i==2 && InputA){ 153 C_per = InputA - 48; 154 i++; 155 } 156 if(i==1 && InputA){ 157 B_per = InputA - 48; 158 lcd.print("C?"); 159 i++; 160 } 161 if(i==0 && InputA){ 162 A_per = InputA - 48; 163 lcd.print("B?"); 164 i++; 165 } 166 } 167 168 lcd.clear(); 169 for(int i = 0;i<A_per;i++){ 170 A_times[i] = set_a_time("Pill A) "+String(i+1)+". time."); 171 } 172 for(int i = 0;i<B_per;i++){ 173 B_times[i] = set_a_time("Pill B) "+String(i+1)+". time."); 174 } 175 for(int i = 0;i<C_per;i++){ 176 C_times[i] = set_a_time("Pill C) "+String(i+1)+". time."); 177 } 178 lcd.setCursor(0,0); 179 lcd.print("How many Pill A"); 180 lcd.setCursor(0,1); 181 lcd.print("did you put in?"); 182 for(int i=0;i<3;i++){ 183 bool confirmed = true; 184 bool fir = true; 185 String number = ""; 186 while(confirmed){ 187 InputB = Tastenfeld.getKey(); 188 if(InputB){ 189 if(fir){ 190 lcd.clear(); 191 lcd.print("Confirm with *"); 192 lcd.setCursor(0,1); 193 fir=false; 194 } 195 lcd.print(InputB); 196 number+=InputB; 197 if(i==2 && InputB == '*'){ 198 PillC_amt = number.toInt(); 199 confirmed=false; 200 } 201 if(i==1 && InputB == '*'){ 202 PillB_amt = number.toInt(); 203 lcd.clear(); 204 lcd.setCursor(0,0); 205 lcd.print("How many Pill C"); 206 lcd.setCursor(0,1); 207 lcd.print("did you put in?"); 208 confirmed=false; 209 } 210 if(i==0 && InputB == '*'){ 211 PillA_amt = number.toInt(); 212 lcd.clear(); 213 lcd.setCursor(0,0); 214 lcd.print("How many Pill B"); 215 lcd.setCursor(0,1); 216 lcd.print("did you put in?"); 217 confirmed=false; 218 } 219 } 220 } 221 } 222 lcd.clear(); 223 counter = set_a_time(); 224 lcd.clear(); 225} 226 227 228 229void loop() { 230 counter = counter+1; 231 seconds = counter; 232 hours = seconds/3600; 233 seconds = seconds%3600; 234 minutes = seconds/60; 235 seconds = seconds%60; 236 delay(1000); 237 lcd.setCursor(4,0); 238 if(hours<10){ 239 lcd.print(0); 240 } 241 lcd.print(hours); 242 lcd.print(":"); 243 244 if(minutes<10){ 245 lcd.print(0); 246 } 247 lcd.print(minutes); 248 lcd.print(":"); 249 250 if(seconds<10){ 251 lcd.print(0); 252 } 253 lcd.print(seconds); 254 lcd.print(" "); 255 lcd.setCursor(0,1); 256 if(PillA_amt <= 10){ 257 digitalWrite(led1, HIGH); 258 } 259 if(PillB_amt <= 10){ 260 digitalWrite(led2, HIGH); 261 } 262 if(PillC_amt <= 10){ 263 digitalWrite(led3, HIGH); 264 } 265 for(int i = 0; i<A_per; i++){ 266 if(A_times[i] == counter){ 267 servoA.attach(11); 268 servoA.write(180); 269 delay(2000); 270 servoA.write(90); 271 delay(2000); 272 PillA_amt--; 273 servoA.detach(); 274 } 275 } 276 for(int i = 0; i<B_per; i++){ 277 if(B_times[i] == counter){ 278 servoB.attach(12); 279 servoB.write(180); 280 delay(2000); 281 servoB.write(90); 282 delay(2000); 283 PillB_amt--; 284 servoB.detach(); 285 } 286 } 287 for(int i = 0; i<C_per; i++){ 288 if(C_times[i] == counter){ 289 servoC.attach(13); 290 servoC.write(180); 291 delay(2000); 292 servoC.write(0); 293 delay(2000); 294 PillC_amt--; 295 servoC.detach(); 296 } 297 } 298} 299/* 300#include <Servo.h> 301Servo myservo; 302Servo myservo2; 303Servo myservo3; 304int pos = 0; 305 306void setup() { 307 myservo.attach(11); 308 myservo2.attach(12); 309 myservo3.attach(13); 310} 311 312void loop() { 313 for (pos = 0; pos <= 180; pos += 1) { 314 // in steps of 1 degree 315 myservo.write(pos); 316 myservo2.write(pos); 317 myservo3.write(pos); 318 delay(15); 319 } 320 for (pos = 180; pos >= 0; pos -= 1) { 321 myservo.write(pos); 322 myservo2.write(pos); 323 myservo3.write(pos); 324 delay(15); 325 } 326} 327*/
Downloadable files
Video
Video of input, pill dispensing and LED lights in action.
Video
Mechanics Image
Image showing how the smint dispenser and micro servers work to dispense the "pills"
Mechanics Image

Circuit Diagram
Diagram of our circuit
Circuit Diagram
Comments
Only logged in users can leave comments