Hi!
I'm new to the world of Arduino, and would love to get som help on a project I have.
What I am making is a device that will turn an RGB LED to a certain colour (red, green or blue) depending on the time of day and/or whether a button has been pressed.
Scenario 1:
At midnight the program will "reset" and the RED LED will turn ON (others off).
Scenario 2:
If a button is pressed, the GREEN LED will turn ON (others off), as long as the time is less than 15:00 (when something else shall happen, e.g. RED LED turns on).
...and other similar scenarios.
I have got the RTC up and running using RTClib, and know how to read/use time from it. However, I am not experienced enough to know what commands (conditionals?) to use. Do i use "IF" command? If so; how?
I have also read abous "switch/case", but I don't really understand how to use this, and cannot find any descent tutorials on Youtube..
I would be very pleased if someone could help me out here, and possibly explain how the code works. I am very willing to learn!
Below is what I am working with now. The IF statements were only tests to see if i could use data from the clock like that. The issue is that the LED turns off again when the given time is not true anymore..
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
const int rLED = 13;
const int gLED = 12;
const int bLED = 8;
const int button = 7;
void setup () {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, INPUT);
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.day(), DEC);
Serial.print('-');
Serial.print(now.month(), DEC);
Serial.print('-');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// if the time/date is at the given point, rLED will be on, else it will be off
if(now.minute() == 45) {
digitalWrite(rLED, HIGH);
}
else {
digitalWrite(rLED, LOW);
}
// when clock hits given time, rLED will go on, other LEDs will be off
if(now.hour() == 2 && now.minute() == 0 && now.second() == 0) {
digitalWrite(rLED, HIGH);
digitalWrite(gLED, LOW);
digitalWrite(bLED, LOW);
}
Serial.println();
delay(1000);
}