I need to turn on an LED for half a second every ten minutes between 8 and 18 hrs (8am-6pm). My boss bought a Arduino Uno R3 and a DS1307 RTC from Adafruit Industries and told me to figure it out. I've never used Arduino before and am hoping someone can help me with the sketch so I can get this thing set up quickly.
I've poked around for the last couple hours but the code is a little over my head. I've got the Wire and RTClib in my Arduino IDE, and I have the RTC hooked up to the Arduino w/ an LED in pin 13. Just need the sketch to make it work...
Trying to turn an LED on for .5 seconds every 10 minutes between 8 and 18hrs (8am-6pm). Timing doesn't have to be ultra-precise, just within the minute or so.
Thank you very much, I appreciate any help I can get.
You can start with this. I just wrote this for what you need. You will probably need to change a few things and figure some things out for yourself, but it should work. You need to make sure that you can get a reading from your RTC before this code will work. You will also need to set the time on your RTC first. Once you get that figured out, then you can try to use this code. Right now the LED is set to turn on for 2 seconds, then it will turn off.
#include <Time.h>
#include <DS1307RTC.h>
#include <Wire.h>
#define DS1307_ADDRESS 0x68
const int ledPin = 13;
tmElements_t tm;
int Hour;
int Minute;
int Second;
void setup() {
delay(5000);
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
pinMode(ledPin,OUTPUT);
Wire.begin();
}
void loop() {
updateTime();
if(Hour >= 8 && Hour <= 18){
if(Minute == 0 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 10 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 20 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 30 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 40 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 50 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else{
delay(1000);
}
}
else{
delay(1000);
}
}
void updateTime(){
Hour = tm.Hour;
Minute = tm.Minute;
Second = tm.Second;
}
I'll tip someone $10 usd via bitcoin if you help me figure this out the rest of the way.
The sketch below is what I've got going right now. The RTC is set to the correct time (used the Adafruit RTC tutorial to look at it in the serial monitor). The Arduino starts, the LED in pin 13 flashes a few times as it starts, then the onboard LED turns on for 5 sec, then nothing else happens... Time goes by. Any ideas?
#include <Time.h>
#include <DS1307RTC.h>
#include <Wire.h>
#define DS1307_ADDRESS 0x68
const int ledPin = 13;
tmElements_t tm;
int Hour;
int Minute;
int Second;
void setup() {
delay(5000);
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
pinMode(ledPin,OUTPUT);
Wire.begin();
}
void loop() {
updateTime();
if(Hour >= 8 && Hour <= 18){
if(Minute == 0 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 10 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 20 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 30 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 40 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else if(Minute == 50 && Second <= 4){
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
else{
delay(1000);
}
}
else{
delay(1000);
}
}
void updateTime(){
Hour = tm.Hour;
Minute = tm.Minute;
Second = tm.Second;
}
Are you sure you have this library properly installed? Can you see the library listed under File- "examples" in the IDE? Can you run the example sketches ReadTest and SetTime shown there?
Glad to have been of help. Don't worry about the bitcoin. I hope you continue to learn about Arduino and programming and pass on what you learn to others.
You should take a look at johnwassers suggestion on how to simplify your code. Use it to understand the % (modulo) http://arduino.cc/en/Reference/Modulo
With the DS1307RTC.h library you do not need #define DS1307_ADDRESS 0x68 The address is taken car of by the library.
Its been awhile since I have used a modulo. I completely forgot about that. That would be a great way to simplify the code even further. Nice catch on the missing "get time" in the Update function. I forgot to add that in. This thread has been helpful for my own understanding.
A note to the OP:
Take this and use it as a way for yourself to better understand arduino code. Try simple projects and do them yourself. The best way to learn is to figure it out yourself.
That DS1307 library is probably the Henning Karlsen one, which I had problems with too. That's odd since his 1302 version is very good, as are other of his libraries. My problems also went away when I switched to DS1307RTC, ie it started to work .
There's also a time alarms library, which might simplify things further.