Hello,
I am using a RTC and Arduino Mega to make an interactive object that has a number of relays contorlled by the Arduino. 4 of the relays will go to lights #'d 1-4 that will flash #1 then #2 then #3 then #4 at 15 minutes past the hour. Then at 30 minutes past the hour the lights will flash #1, then #2, then #3 then #4 then #1 then #2 then #3 then #4. At 45 minutes the sequence will go 3 times. At the top of the hour the sequence will go 4 times followed by all four lights imultaneously flashing on and off based on the hour. This is similar to a bell tower.
Currently I can get that to work using the Time Alarms, but this will require me writing in an alarm for every 15 minutes. I want to figure out how to code that so I don't have to repeat myself in the code so much.
That light / clock sequence will be running while I have other relays turn on fans, motors and things on a controlled program so I need the rest of the program to run while the lights are doing their clock thing.
Here is what I have so far.
Full Code:
/*-----( Import needed libraries )-----*/
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>
/*-----( Declare Constants and Pin Numbers )-----*/
const int relayPin1 = 30; // Light #1
const int relayPin2 = 31; // Light #2
const int relayPin3 = 32; // Light #3
const int relayPin4 = 33; // Light #4
const int relayPin5 = 2; // Fan #1
const int relayPin6 = 3; // Fan #2
const int relayPin7 = 4; // Motor #1
const int relayPin8 = 5; // Motor #2
const int relayPin9 = 6; // Motor #3
const int relayPin10 = 7; // Motor #4
/*-----( Declare objects )-----*/
RTC_DS1307 rtc; // Create a RealTimeClock object
uint32_t syncProvider() //function which sets up the RTC as the source of external time
{
return rtc.now().unixtime();
}
/*-----( Declare Variables )-----*/
// Variables will change:
int relayState1 = LOW;
int relayState2 = LOW;
int relayState3 = LOW;
int relayState4 = LOW;
int relayState5 = LOW;
int relayState6 = LOW;
int relayState7 = LOW;
int relayState8 = LOW;
int relayState9 = LOW;
int relayState10 = LOW;
int i;
int ding;
int n;
int hr;
void setup() /****** SETUP: RUNS ONCE ******/
{
pinMode(relayPin1, OUTPUT); // sets the digital pin as output
pinMode(relayPin2, OUTPUT); // sets the digital pin as output
pinMode(relayPin3, OUTPUT); // sets the digital pin as output
pinMode(relayPin4, OUTPUT); // sets the digital pin as output
pinMode(relayPin5, OUTPUT); // sets the digital pin as output
pinMode(relayPin6, OUTPUT); // sets the digital pin as output
pinMode(relayPin7, OUTPUT); // sets the digital pin as output
pinMode(relayPin8, OUTPUT); // sets the digital pin as output
pinMode(relayPin9, OUTPUT); // sets the digital pin as output
pinMode(relayPin10, OUTPUT); // sets the digital pin as output
digitalWrite(relayPin1, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin2, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin3, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin4, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin5, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin6, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin7, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin8, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin9, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin10, HIGH); // Prevents relays from starting up engaged
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(__DATE__, __TIME__)); //comment this out when the RTC has been set
setSyncProvider(syncProvider); // the function to get the time from the RTC
// Have the allarm trigger at 00, 15, 30, 45, 00, 15, 30, 45 ....
Alarm.alarmRepeat(11,00,0,BlinkHour);
Alarm.alarmRepeat(11,15,0,Blink15Min);
Alarm.alarmRepeat(11,30,0,Blink30Min);
Alarm.alarmRepeat(11,45,0,Blink45Min);
Alarm.alarmRepeat(12,00,0,BlinkHour);
Alarm.alarmRepeat(12,15,0,Blink15Min);
// There must be a better way then typing all of these individually ....
Serial.begin(57600); // Set up for Serial Monitor to be able to see this work
}//--(end setup )---
void loop(){
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void Blink4Times(){
Serial.println("Alarm: - turn lights on and off in sequence at 15 minutes");
// if the LED is off turn it on and vice-versa:
digitalWrite(relayPin1, LOW);
Alarm.delay(1000); // wait one second between clock display
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, LOW);
Alarm.delay(1000); // wait one second between clock display
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, LOW);
Alarm.delay(1000); // wait one second between clock display
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, LOW);
Alarm.delay(1000); // wait one second between clock display
digitalWrite(relayPin4, HIGH);
Alarm.delay(3000); // wait one second between clock display
}
void Blink15Min(){
// Code to get some value of "n"
n=1;
for (i = 0; i < n; i++) {
Blink4Times();
Serial.print(n); // Do "something"
}
}
void Blink30Min(){
// Code to get some value of "n"
n=2;
for (i = 0; i < n; i++) {
Blink4Times();
Serial.print(n); // Do "something"
}
}
void Blink45Min(){
// Code to get some value of "n"
n=3;
for (i = 0; i < n; i++) {
Blink4Times();
Serial.print(n); // Do "something"
}
}
void BlinkHour(){
// Code to get some value of "n"
n=4;
for (i = 0; i < n; i++) {
Blink4Times();
Serial.print(n); // Do "something"
}
// Code to get some value of "n"
hr=(hour());
for (i = 0; i < hr; i++) {
BlinkAll();
Serial.print(n); // Do "something"
}
}
void BlinkAll (){
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
Alarm.delay(1000); // wait one second between clock display
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
Alarm.delay(1000); // wait one second between clock display
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
Seemingly redundant hand written 15 minute alarms
// Have the allarm trigger at 00, 15, 30, 45, 00, 15, 30, 45 ....
Alarm.alarmRepeat(11,00,0,BlinkHour);
Alarm.alarmRepeat(11,15,0,Blink15Min);
Alarm.alarmRepeat(11,30,0,Blink30Min);
Alarm.alarmRepeat(11,45,0,Blink45Min);
Alarm.alarmRepeat(12,00,0,BlinkHour);
Alarm.alarmRepeat(12,15,0,Blink15Min);
// There must be a better way then typing all of these individually ....