Ok, sorry, let me try again. Second year uni student struggling to understand Arduino code, not got a great teacher.
I have to code a pill dispenser. The top row of the LCD is a normal clock, been asked to start it at 09:59:00. Second line of display is a timer (MdT) to count up to 25 seconds then stop at 25 seconds at which point a red LED and buzzer go off until the take medicine button is pressed, resetting this timer. Also on the second row is MdStk (number of pills in stock). The take medicine button takes one off the medicine count, a second button is pressed to add 5 to the number. There is so much more to this about heart rate and SD card recordings that I just feel so overwhelmed.
If anyone can (simply) explain how to make the MdT timer stop at 25 seconds, the take medicine button to reset the MdT timer and decrease MdStk and the take medicine button to increase MStk by 5, I would really appreciate it.
#include <LiquidCrystal.h>
const int rs = 2, en = 4, d4 = 8, d5 = 9, d6 = 10, d7 = 6;
int hh = 9, mm = 59, ss = 0, ct = 0, pt = 0, MdT = 0, MdStk = 10;
String hstr, mstr, sstr, mdstr, mdstkstr;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int pinLDR = A0, pinGLED = A1, pinRLED_Bz = A2;
int pinTM = A5, swbtn = A4, topupbtn = A3; //arduino location for take medicine, switch feature and top up medicine buttons
int circuit_status = 0; // 0 represents green leds on and 1 represents red led and buzzer on
int pb_val = 0, ldr_val;
int feature_flag = 1;
int valTM = 0, valswbtn = 0, valtopupbtn = 5;
void setup() {
lcd.begin(16, 2);
pinMode(A0, INPUT); // LDR
pinMode(A5, INPUT); // Pushbutton for take medicine
pinMode(A4, INPUT); //Switch feature button (between taking readings and dispensing medication)
pinMode(A3, INPUT); //Top up medicine button
pinMode(A1, OUTPUT); // Green LED
pinMode(A2, OUTPUT); // RED LED & Buzzer
digitalWrite(pinGLED, HIGH);
digitalWrite(pinRLED_Bz, LOW);
}
void loop() {
if (digitalRead(swbtn) > 0) {
if (feature_flag == 1)
feature_flag = 0;
else
feature_flag = 1;
}
if (feature_flag == 1) {
ct = millis() / 1000;
if (MdT < 25)
digitalWrite(A1, HIGH);
else
digitalWrite(A1, LOW);
if (MdT >= 25)
digitalWrite(A2, HIGH);
else
digitalWrite(A2, LOW);
if (ct > pt) {
pt = ct;
MdT = MdT + 1;
ss = ss + 1;
if (ss >= 60) {
ss = 0;
mm = mm + 1;
if (mm >= 60) {
mm = 0;
hh = hh + 1;
}
}
if (ss < 10)
sstr = "0" + String(ss);
else
sstr = String(ss);
if (mm < 10)
mstr = "0" + String(mm);
else
mstr = String(mm);
if (hh < 10)
hstr = "0" + String(hh);
else
hstr = String(hh);
if (MdT < 10)
mdstr = "0" + String(MdT);
else
mdstr = String(MdT);
lcd.setCursor(0, 0);
lcd.print("Time ");
lcd.print(hstr + ":" + mstr + ":" + sstr);
} // end if(ct>pt){
lcd.setCursor(0, 1);
lcd.print("MdT:");
lcd.print(mdstr);
mdstkstr = String(MdStk) ;
lcd.print(", MdStk=");
lcd.print(mdstkstr);
}// if(feature_flag==1)
else {
//code for pulse-0ximiter
lcd.setCursor(0, 0);
lcd.print("Pill Dispenser ");
lcd.setCursor(0, 1);
lcd.print("Code is running");
}
}