Offline
Newbie
Karma: 0
Posts: 21
|
 |
« on: February 07, 2013, 05:25:38 am » |
I wish to program timer alarm every 800,1200,2000,2400 Hrs but my code count all 24 Hrs and reset. My code #include <LiquidCrystal.h>
int second=0, minute=0, hour=0; int x=0;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup(){ lcd.print("load..."); delay(1000); lcd.begin(16, 2); lcd.setCursor(0, 1); lcd.print(" Timer "); }
void loop(){ static unsigned long lastTick = 0; if (millis() - lastTick >= 1000) { lastTick = millis(); second++; }
// Move forward one minute every 60 seconds if (second >= 60) { minute++; second = 0; // Reset seconds to zero }
// Move forward one hour every 60 minutes if (minute >=60) { hour++; minute = 0; // Reset minutes to zero }
if (hour >=24) { hour=0; minute = 0; // Reset minutes to zero } if (second >= 20) { lcd.setCursor(0, 1); lcd.print(" MOTOR "); } digitalClockDisplay(); }
void printDigits(byte digits){ if(digits < 10) lcd.print('0'); lcd.print(digits); }
char sep() { x = millis()/500; if(x%2==0) { lcd.print(":"); } else{ lcd.print(" "); } }
void digitalClockDisplay(){ lcd.setCursor(4,0); printDigits(hour); sep(); printDigits(minute); sep(); printDigits(second); }
I try to solve program on sec to 150 but can not work ::::::::::::::::::::::::::: // Move forward one minute every 60 seconds if (second >= 150) { minute++; second = 0; // Reset seconds to zero :::::::::::::::::::::::::;: it 100 and than not to 150 minute++; second = 0 how to edit code for count to 2400 Hrs. Thank you in deed
|
|
|
|
« Last Edit: February 07, 2013, 05:30:11 am by shitokung »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #1 on: February 07, 2013, 05:27:14 am » |
Read this before posting a programming questionPlease edit your post, select the code, and put it between [code] ... [/code] tags. You can do that by hitting the # button above the posting area.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #2 on: February 07, 2013, 05:29:49 am » |
It would be easier to use the TimeAlarms library. It is designed to do exactly what you want in very few lines of code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #3 on: February 07, 2013, 05:32:27 am » |
TimeAlarms library it work but i don't know how to alarm such as 10,20,30 Hrs How to check that 10 Hrs and alarm
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #4 on: February 07, 2013, 05:38:40 am » |
Have a look at this from the readme for TimeAlarms
"Alarm.timerRepeat(Period, TimerFunction); Description: Continuously calls user provided TimerFunction after the given period in seconds has elapsed.
Alarm.timerRepeat(Hour, Minute, Second, TimerFunction); Description: As timerRepeat above, but period is the number of seconds in the given Hour, Minute and Second parameters"
Alarm.timerRepeat(10, 0, 0, TimerFunction); would seem to be what you want but I have not tested it.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #5 on: February 07, 2013, 05:48:17 am » |
Thank you " UKHeliBob" for suggestion i will try to program TimeAlarms.h but my old program I can program 0000:00:00 how to ? 
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #6 on: February 07, 2013, 12:57:55 pm » |
4 digit hours ? The TimeAlarms Alarm.timerRepeat() function sets how far ahead the alarm should be triggered, not what time the alarm should be triggered. Subtle difference. Try this #include <Time.h> #include <TimeAlarms.h>
void setup() { Serial.begin(9600); Alarm.timerRepeat(0, 1, 10, TimerFunction); }
void loop() { Alarm.delay(0); }
void TimerFunction() { Serial.print("Repeat alarm triggered at "); digitalClockDisplay(); }
void digitalClockDisplay() { Serial.print(hour()); printDigits(minute()); printDigits(second()); Serial.println(); }
void printDigits(int digits) { Serial.print(":"); if(digits < 10) Serial.print('0'); Serial.print(digits); }
It triggers every 1 minute and 10 seconds. I have not had time to try it for 10 hours, nor did I set the clock time at the beginning. Sample output Repeat alarm triggered at 0:01:10 Repeat alarm triggered at 0:02:20 Repeat alarm triggered at 0:03:30
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #7 on: February 07, 2013, 06:10:13 pm » |
4 digit hours ? When I'm at work, yeah, seems like that some times.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #8 on: February 07, 2013, 08:37:20 pm » |
thanks all now i used old program #include <LiquidCrystal.h> int second=0, minute=0, hour=0; int x=0;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup(){ lcd.print("load..."); delay(1000); lcd.begin(16, 2); lcd.setCursor(0, 1); lcd.print(" Timer "); }
void loop(){ static unsigned long lastTick = 0; if (millis() - lastTick >= 1000) { lastTick = millis(); second++; }
// Move forward one minute every 60 seconds if (second >= 60) { minute++; second = 0; // Reset seconds to zero }
// Move forward one hour every 60 minutes if (minute >=60) { hour++; minute = 0; // Reset minutes to zero }
if (hour >=24) { hour=0; minute = 0; // Reset minutes to zero } if (second >= 20) { lcd.setCursor(0, 1); lcd.print(" MOTOR "); } digitalClockDisplay(); }
void printDigits(byte digits){ if(digits < 10) lcd.print('0'); lcd.print(digits); }
char sep() { x = millis()/500; if(x%2==0) { lcd.print(":"); } else{ lcd.print(" "); } }
void digitalClockDisplay(){ lcd.setCursor(4,0); printDigits(hour); sep(); printDigits(minute); sep(); printDigits(second); }
I edited my progeam if (second >= 199) { minute++; second = 0; // Reset seconds to zero }
it's work when count unit 199 and minute++ well done but position third is still stay but no problem because i will use it on Hrs btw now i use arduino board model UNO R3 in my code if (second >= 20) { lcd.setCursor(0, 1); lcd.print(" MOTOR "); }
when second >= 20 my lcd will show MOTOR but i dont know how to... use button on board for code program such when sec > = 20 show MOTOR but when push button on board show GEAR
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #9 on: February 08, 2013, 02:20:34 am » |
What is this all about ? if (second >= 199) { minute++; second = 0; // Reset seconds to zero } Earlier in the thread you seemed to have 4 digit hours, now you have minutes with 200 seconds in them. Do you want to (a) display "MOTOR" after 20 seconds then wait for a button press before displaying "GEAR" (b) display "GEAR" after 20 seconds only if the button is pressed, otherwise display "MOTOR" and ignore the button (c) show "GEAR" at any time the button is pressed (d) something else You can learn to read buttons by looking at the Button example in the IDE.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #10 on: February 08, 2013, 04:45:12 am » |
^ ^ ok if (second >= 60) { minute++; second = 0; // Reset seconds to zero }
// Move forward one hour every 60 minutes if (minute >=60) { hour++; minute = 0; // Reset minutes to zero }
if (hour >=2400) { hour=0; minute = 0; // Reset minutes to zero }
i use int for this it can count to -32768 to 32767 so i count to 2400 it can ?? then my clock can count XXXX:XX:XX right? btw now i use arduino board model UNO R3 it has 6 buttons ,select,left,up,down,right,rst i will use these buttons for change ALARM display examplewhen time 20 sec alarm "MOTOR" but if sec > 20 and press button such as left button alarm change "GEAR" .....how to
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #11 on: February 08, 2013, 06:01:54 am » |
Come back and ask more questions when you have tried and understood the Button example.
I still don't get what you are on about with "my clock can count XXXX:XX:XX "
Can you give me an example of a real time in that format ?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Online
Edison Member
Karma: 29
Posts: 1589
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #12 on: February 08, 2013, 07:51:06 am » |
I don't know what kind of clock you have but, you could maybe do a nested loop to give you those hours you want. But any chance does it log Days as well? instead of doing it by hours, do it by days.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6406
-
|
 |
« Reply #13 on: February 08, 2013, 08:48:54 am » |
i use int for this it can count to -32768 to 32767 so i count to 2400 it can ?? then my clock can count XXXX:XX:XX right?
The code fragment looks reasonable to me and would enable you to count up to 2400 hours (100 days) showing hours, minutes and seconds.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #14 on: February 08, 2013, 11:49:58 am » |
We won't know until the OP replies, but I can't help feeling that there is some confusion between 2400 hours (ie midnight) and the need to allow for 24 hours in the day.
|
|
|
|
|
Logged
|
|
|
|
|
|