Loop break

Hi i am making a personal project , i want to make a pill dispenser , i want to make a 20second break so that the elderly will be given enough time to take the pills before it start counting down again for the next timing of dispense. I don't know how to make a break whereas it doesn't do anyth for 20 secs then the next countdown will happen. And i want it to be switched off after the third dispense of the day and turned on before the first dispenser.

This is my code i am halfway to completion.

#include <LiquidCrystal.h>

int S = 10 ;
int M = 0;
int H = 0;

LiquidCrystal lcd(9,7,10,11,12,13); // pins connected to LCD

void setup()
{
lcd.begin(16,2);//set up the LCD's number of columns and rows
}
void loop()
{
lcd.setCursor(3,0);
lcd.print ("Dispensing");
lcd.setCursor(6,1);
lcd.print(":");
lcd.setCursor(9,1);
lcd.print(":");

S--;
delay(1000);

if(S<0)
{
M--;
S=59;
}
if(M<0)
{
H--;
M=59;
}
if(H<0) { H=6; M=0; S=0; } if(M>9)
{
lcd.setCursor(7,1);
lcd.print(M);
}
else
{
lcd.setCursor(7,1);
lcd.print("0");
lcd.setCursor(8,1);
lcd.print(M);
lcd.setCursor(9,1);
lcd.print(":");
}

if(S>9)
{
lcd.setCursor(10,1);
lcd.print(S);
}
else
{
lcd.setCursor(10,1);
lcd.print("0");
lcd.setCursor(11,1);
lcd.print(S);
lcd.setCursor(12,1);
lcd.print(" ");
}

if(H>9)
{
lcd.setCursor(4,1);
lcd.print (H);
}
else
{
lcd.setCursor(4,1);
lcd.print("0");
lcd.setCursor(5,1);
lcd.print(H);
lcd.setCursor(6,1);
lcd.print(":");
}
}

// BELOW IS A CODE I TRIED BUT IT DOESN'T WORK due to it slowing down the number of seconds going down by 7secs. And there's something wrong with the display on the screen.

#include <LiquidCrystal.h>

int S = 2;
int M = 0;
int H = 0;
static const uint32_t DELAY_1_S = 1000UL;
static const uint32_t DELAY_1_MINUTE = DELAY_1_S * 60UL;
static const uint32_t DELAY_1_HOUR = DELAY_1_MINUTE * 60UL;

LiquidCrystal lcd(9,7,10,11,12,13); // pins connected to LCD

void setup()
{
lcd.begin(16,2);//set up the LCD's number of columns and rows
}
void loop()
{
lcd.setCursor(3,0);
lcd.print ("Dispensing");
lcd.setCursor(6,1);
lcd.print(":");
lcd.setCursor(9,1);
lcd.print(":");

S--;
delay(1000);

if(S<0)
{
M--;
S=59;
}
if(M<0)
{
H--;
M=59;
}
if(H<0) { H=6; M=0; S=0; } if(M>9)
{
lcd.setCursor(7,1);
lcd.print(M);
delay(7UL * DELAY_1_S); // Delay 7 seconds

}
else
{
lcd.setCursor(7,1);
lcd.print("0");
lcd.setCursor(8,1);
lcd.print(M);
lcd.setCursor(9,1);
lcd.print(":");
}

if(S>9)
{
lcd.setCursor(10,1);
lcd.print(S);
}
else
{
lcd.setCursor(10,1);
lcd.print("0");
lcd.setCursor(11,1);
lcd.print(S);
lcd.setCursor(12,1);
lcd.print(" ");
}

if(H>9)
{
lcd.setCursor(4,1);
lcd.print (H);
}
else
{
lcd.setCursor(4,1);
lcd.print("0");
lcd.setCursor(5,1);
lcd.print(H);
lcd.setCursor(6,1);
lcd.print(":");
}
}

  1. Please post your code inside [​code]code tags[​/code] when using the forum. You can insert them with the </> button on the toolbar.
  2. Don’t try to time things with multiple delays. It’s not accurate, especially if you are writing to an LCD in the meantime. Learn how to correctly use millis() for timing instead.
  3. Trying to count with separate minutes and second variables is a hiding to nothing IMO. Count in total seconds, (hours * 3600) + (minutes * 60) + seconds, then divide out for display. It’s a lot easier...
total_seconds = total_seconds - 1;
hours = total_seconds / 3600;
minutes = (total_seconds / 60) % 60;
seconds = total_seconds % 60;
  1. No idea how you are going to switch yourself off and on, or even what that means.

When i put in delay , the countdown get slower due to delay. But i don't want it to happen , i just want it to be a 20 seconds delay before it starts the next countdown. Can someone help me with the code??

[#include <LiquidCrystal.h>

int S = 2;
int M = 0;
int H = 0;

LiquidCrystal lcd(9,7,10,11,12,13); // pins connected to LCD

void setup()
{
lcd.begin(16,2);//set up the LCD's number of columns and rows
}
void loop()
{
lcd.setCursor(3,0);
lcd.print ("Dispensing");
lcd.setCursor(6,1);
lcd.print(":");
lcd.setCursor(9,1);
lcd.print(":");

S--;
delay(1000);

if(S<0)
{
M--;
S=59;
}
if(M<0)
{
H--;
M=59;
} // before it restarts the countdown timer i want to have a 20 seconds delay
if(H<0) { H=6; M=0; S=0; } if(M>9)
{
lcd.setCursor(7,1);
lcd.print(M);

}
else
{
lcd.setCursor(7,1);
lcd.print("0");
lcd.setCursor(8,1);
lcd.print(M);
lcd.setCursor(9,1);
lcd.print(":");
}

if(S>9)
{
lcd.setCursor(10,1);
lcd.print(S);
}
else
{
lcd.setCursor(10,1);
lcd.print("0");
lcd.setCursor(11,1);
lcd.print(S);
lcd.setCursor(12,1);
lcd.print(" ");
}

if(H>9)
{
lcd.setCursor(4,1);
lcd.print (H);
}
else
{
lcd.setCursor(4,1);
lcd.print("0");
lcd.setCursor(5,1);
lcd.print(H);
lcd.setCursor(6,1);
lcd.print(":");
}
}

]

Put the code in code tags for the forum as advised. You can even ‘copy for forum’ in the arduino ide and the tags are included. People can’t read your code when it is spread out all over the page. Try reading the posts at the beginning of every forum on how to use this site.

Look up how to use millis in tutorials such as ‘blink without delay’