Hi ladies and gents
I was wondering if anyone could help? I've written some code to automatically send out an IR signal/command at set times. However, I've woke up this morning to see the progress of it and it hasn't worked. I've tested the commands individually by calling them in setup, then I've tested them by changing the times to +1 minute from the current time and the if statement works too. But when it's changed to the correct times I need, it isn't working. Would someone be able to lay their eyes on my code and shed some wisdom here, please?
#include <Arduino.h>
#include <IRremote.hpp>
#include "RTClib.h"
RTC_DS1307 rtc;
const int RELAY_PIN = 4;
#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.begin(57600);
if (!rtc.begin()) {
Serial.flush();
while (1) delay(10);
}
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) || defined(SERIALUSB_PID) || defined(ARDUINO_attiny3217)
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
#endif
#if defined(IR_SEND_PIN)
IrSender.begin(); // Start with IR_SEND_PIN as send pin and enable feedback LED at default feedback LED pin
#else
IrSender.begin(3, ENABLE_LED_FEEDBACK); // Specify send pin and enable feedback LED at default feedback LED pin
#endif
if (!rtc.isrunning() || rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void startChange() {
digitalWrite(RELAY_PIN, HIGH);
while (millis() < millis() + 5000) {}
digitalWrite(RELAY_PIN, LOW);
}
void sendUp() {
uint16_t irSignal[67] = {8930, 4520, 530, 570, 530, 620, 530, 570, 530, 620, 530, 620, 480, 620, 530, 570, 530, 620, 530, 1670, 530, 1720, 530, 1670, 530, 1720, 530, 1670, 530, 1720, 530, 1670, 530, 1720, 530, 1720, 480, 620, 530, 1670, 530, 620, 530, 570, 530, 620, 530, 620, 480, 620, 530, 620, 480, 1720, 530, 570, 530, 1720, 530, 1720, 480, 1720, 530, 1670, 530, 1720, 530}; // Protocol=NEC Address=0x0 Command=0x5 Raw-Data=0xFA05FF00 32 bits LSB first
IrSender.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), NEC_KHZ); // Note the approach used to automatically calculate the size of the array.
wait();
}
void sendOff() {
uint16_t irSignal[67] = {8930, 4520, 530, 620, 480, 620, 530, 570, 530, 620, 530, 570, 530, 620, 530, 620, 480, 620, 530, 1720, 480, 1720, 530, 1670, 530, 1720, 530, 1720, 480, 1720, 530, 1670, 530, 1720, 530, 620, 480, 1720, 530, 1720, 480, 620, 530, 570, 530, 620, 530, 570, 530, 620, 530, 1720, 480, 620, 530, 620, 480, 1720, 530, 1670, 530, 1720, 530, 1670, 530, 1720, 530}; // Protocol=NEC Address=0x0 Command=0x6 Raw-Data=0xF906FF00 32 bits LSB first
IrSender.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), NEC_KHZ); // Note the approach used to automatically calculate the size of the array.
wait();
}
void sendOn() {
uint16_t irSignal[67] = {8880, 4520, 530, 620, 480, 620, 530, 620, 480, 620, 530, 620, 480, 620, 530, 620, 480, 620, 530, 1720, 480, 1720, 530, 1720, 480, 1720, 530, 1720, 480, 1720, 530, 1720, 480, 1720, 530, 1720, 480, 1720, 530, 1720, 480, 620, 530, 620, 480, 620, 530, 620, 480, 620, 530, 620, 480, 620, 530, 620, 480, 1720, 530, 1720, 480, 1720, 530, 1720, 480, 1720, 530}; // Protocol=NEC Address=0x0 Command=0x7 Raw-Data=0xF807FF00 32 bits LSB first
IrSender.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), NEC_KHZ); // Note the approach used to automatically calculate the size of the array.
wait();
}
void sendBlue() {
const uint16_t irSignal[67] = {8930, 4470, 530, 620, 480, 620, 530, 620, 530, 570, 530, 620, 530, 570, 530, 620, 530, 570, 530, 1670, 580, 1670, 530, 1720, 530, 1670, 530, 1720, 530, 1670, 530, 1670, 580, 1670, 530, 620, 530, 1670, 530, 570, 580, 1670, 530, 620, 530, 570, 530, 620, 530, 620, 480, 1720, 530, 620, 480, 1720, 530, 570, 530, 1720, 530, 1720, 480, 1720, 530, 1720, 480}; // Protocol=NEC Address=0x0 Command=0xA Raw-Data=0xF50AFF00 32 bits LSB first
IrSender.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), NEC_KHZ);
wait();
}
void sendDown() {
uint16_t irSignal[67] = {8880, 4470, 580, 570, 530, 570, 580, 570, 530, 620, 530, 570, 530, 620, 530, 570, 530, 620, 530, 1670, 530, 1720, 530, 1720, 480, 1720, 530, 1720, 480, 1720, 530, 1670, 530, 1720, 530, 620, 480, 620, 530, 1670, 530, 620, 530, 570, 530, 620, 530, 570, 530, 620, 530, 1670, 530, 1720, 530, 570, 530, 1720, 530, 1670, 530, 1720, 530, 1670, 530, 1720, 530}; // Protocol=NEC Address=0x0 Command=0x4 Raw-Data=0xFB04FF00 32 bits LSB first
IrSender.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), NEC_KHZ);
wait();
}
void sendWhite() {
uint16_t irSignal[67] = {8930, 4520, 480, 620, 530, 620, 480, 620, 530, 620, 480, 620, 530, 620, 480, 620, 530, 620, 480, 1720, 530, 1720, 480, 1720, 530, 1670, 530, 1720, 530, 1720, 480, 1720, 530, 1670, 530, 1720, 530, 1670, 530, 620, 530, 1720, 480, 620, 530, 620, 480, 620, 530, 620, 480, 620, 530, 620, 480, 1720, 530, 620, 480, 1720, 530, 1720, 480, 1720, 530, 1670, 530}; // Protocol=NEC Address=0x0 Command=0xB Raw-Data=0xF40BFF00 32 bits LSB first
IrSender.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), NEC_KHZ);
wait();
}
void wait() {
while (millis() < millis() + 1000) {}
}
void loop() {
DateTime now = rtc.now();
if (now.hour() == 6 && now.minute() == 30 && now.second() < 2) {
sendOn(); // light on & 1 blue
} else if (now.hour() == 7 && now.minute() == 0 && now.second() < 2) {
sendUp(); // 2 blue
} else if (now.hour() == 7 && now.minute() == 30 && now.second() < 2) {
sendUp(); // 3 blue
} else if (now.hour() == 8 && now.minute() == 0 && now.second() < 2) {
sendWhite(); // white on
} else if (now.hour() == 8 && now.minute() == 1 && now.second() < 2) {
sendUp(); // 4 white
} else if (now.hour() == 8 && now.minute() == 30 && now.second() < 2) {
sendUp(); // 5 white
} else if (now.hour() == 9 && now.minute() == 0 && now.second() < 2) {
sendUp(); // 6 white
} else if (now.hour() == 9 && now.minute() == 30 && now.second() < 2) {
sendUp(); // 7 white
} else if (now.hour() == 9 && now.minute() == 31 && now.second() < 2) {
sendUp(); // 8 white
} else if (now.hour() == 19 && now.minute() == 30 && now.second() < 2) {
sendDown(); // 7 white
} else if (now.hour() == 19 && now.minute() == 31 && now.second() < 2) {
sendDown(); // 6 white
} else if (now.hour() == 20 && now.minute() == 0 && now.second() < 2) {
sendDown(); // 5 white
} else if (now.hour() == 20 && now.minute() == 30 && now.second() < 2) {
sendDown(); // 4 white
} else if (now.hour() == 21 && now.minute() == 0 && now.second() < 2) {
sendDown(); // 3 white
} else if (now.hour() == 21 && now.minute() == 1 && now.second() < 2) {
sendBlue(); // 3 blue
} else if (now.hour() == 21 && now.minute() == 30 && now.second() < 2) {
sendDown(); // 2 blue
} else if (now.hour() == 22 && now.minute() == 0 && now.second() < 2) {
sendDown(); // 1 blue
} else if (now.hour() == 22 && now.minute() == 30 && now.second() < 2) {
sendOff(); // light off & 1 blue
}
if(now.dayOfTheWeek() == 1 && now.hour() == 8 && now.minute() < 2) {
//startChange();
}
}
Any help appreciated.
Many thanks
James
P.S - for anyone wanting to do a similar project now or in the future, these ir signals/commands are for the aquarium "bar" type lights with the normal "chinese" type remote that comes with almost all LED products. Please feel free to use them!