Pill dispenser with heart rate monitor

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");
  }
}

No, you didn't mention where you have problems.

If You provide the necessary documentation like code, schematics and links to circuit datasheet, yes. Else, no.

Have you tried coding it or you want someone to code it for you?

It always amazes me at the scant and ambiguous descriptions accompanied by a plea for help.

how anyone can expect any help by telling noting about how the project will be built.

Read and follow this link telling what helpers expect: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

1 Like

Have amended with more detailed description & code

I don't know where to even start coding it, was hoping that it was just something I could find myself but all I've found so far are countdown timers and delays.

No, that happens very seldom. Programs, code, is not like big LEGO bricks. It's a hell lot more delicate.

Thanks for the code. A piece of advice for the future, post new, or updated information in the next post You make.

I'll take a look at the code later.

A general role is to make code for one device at the time, learning and verifying You know how to use it.
When each peripheral works, then You can integrate their code into one build.

Smashing all together, like putting them into a sack, shaking it and hoping it will work is the absolutely most difficult way to go.

Then the work is to make the parts do what You want.

What works and what doesn't?

Schematics please! How are they wired?

Sure, I can help. Firstly, I'll say that for educational purposes, what you're trying to accomplish is fine. Don't use this in as an actual medical assistance device, Arduino is not suitable for any medical apps or life safety devices.
So that's out of the way. Now you have a large problem of trying to solve a large project problem with multiple elements to it and you're lost. Don't worry, it happens. What you need to do is to break it down into a bunch little projects and solve each one reliably on its own first. Sounds like your project is no weekend gizmo. If my experience is relatable to others (I'm a fairly experienced amateur) what you think will take two hours will take two days, and what you think will take two days will take two weeks.
Once you get each part of the project working on its own, write that loop into a function and save it on its own. Eventually you will be at the point where you're sort of pasting premade, tested functions together and merging them as needed to make the overall project do what it's supposed to do. Good luck!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.