So I am trying to make a cat feeder but the code wont loop for some reason
any reply would be very helpful.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long hour = 0, minute = 10, second = 10; long countdown_time = (hour * 3600) + (minute * 60) + second;
int indication = 10; //Connect LED or Buzzer to digital pin 10
void setup() {
pinMode(indication, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(4, 0);
lcd.print("HH:MM:SS");
}
void loop() {
long countdowntime_seconds = countdown_time - (millis() / 1000);
if (countdowntime_seconds >= 0) {
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60) % 60);
long countdown_sec = countdowntime_seconds % 60;
lcd.setCursor(4, 1);
if (countdown_hour < 10) {
lcd.print("0");
}
lcd.print(countdown_hour);
lcd.print(":");
if (countdown_minute < 10) {
lcd.print("0");
}
lcd.print(countdown_minute);
lcd.print(":");
if (countdown_sec < 10) {
lcd.print("0");
}
lcd.print(countdown_sec);
if (countdowntime_seconds == 0) {
digitalWrite(indication, HIGH);
delay(2000);
digitalWrite(indication, LOW);
countdown_time = (hour * 3600) + (minute * 60) + second; // Reset countdown time
}
}
delay(500);
}
PaulRB
3
Please edit and fix your post above. Your attempt to use code tags didn't quite work right.
You may need to copy the code from the IDE again because the indentation may have been lost, and some characters may have gone missing.
Probably it is the delay 500
Since "countdown_time" is updated based on "millis() / 1000," which returns the time in seconds, the countdown
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long hour = 0, minute = 10, second = 10; long countdown_time = (hour * 3600) + (minute * 60) + second;
int indication = 10; //Connect LED or Buzzer to digital pin 10
void setup() {
pinMode(indication, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(4, 0);
lcd.print("HH:MM:SS");
}
void loop() {
long countdowntime_seconds = countdown_time - (millis() / 1000);
if (countdowntime_seconds >= 0) {
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60) % 60);
long countdown_sec = countdowntime_seconds % 60;
lcd.setCursor(4, 1);
if (countdown_hour < 10) {
lcd.print("0");
}
lcd.print(countdown_hour);
lcd.print(":");
if (countdown_minute < 10) {
lcd.print("0");
}
lcd.print(countdown_minute);
lcd.print(":");
if (countdown_sec < 10) {
lcd.print("0");
}
lcd.print(countdown_sec);
if (countdowntime_seconds == 0) {
digitalWrite(indication, HIGH);
delay(2000);
digitalWrite(indication, LOW);
countdown_time = (hour * 3600) + (minute * 60) + second; // Reset countdown time
}
}
delay(500);
}
I tried it but it did not work
gcjr
7
look this over
- delay() can interfere with the main timer loop
- use a separate counter to handle the LED
- sprintf() simplifies formatting the display string
# include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);
const byte On = LOW;
const byte Off = HIGH;
const byte IndicatorPin = 10;
int indicatorSec;
unsigned long MsecSec = 100; // set low for testing 1000
unsigned long msecLast;
const long TimerSec = 10 * 60;
long cntSec = TimerSec;
char s [90];
// -----------------------------------------------------------------------------
void loop ()
{
unsigned long msec = millis ();
if (msec - msecLast >= MsecSec) {
msecLast += MsecSec;
int hour = cntSec / 3600;
int mins = (cntSec % 3600) / 60;
int secs = (cntSec % 60);
sprintf (s, "%2d:%02d:%02d\n", hour, mins, secs);
lcd.print (s);
cntSec--;
if (0 > cntSec) {
cntSec = TimerSec;
indicatorSec = 2;
digitalWrite (IndicatorPin, On);
}
else if (0 == --indicatorSec)
digitalWrite (IndicatorPin, Off);
}
}
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (9600);
pinMode (IndicatorPin, OUTPUT);
digitalWrite (IndicatorPin, Off);
lcd.init ();
lcd.backlight ();
lcd.setCursor (4, 0);
}
PaulRB
9
Not sure what you did there, but you can see how the indentation has been lost in post #5, but it's ok in post #1.
system
Closed
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.