Dear everyone,
I am currently working on a project : a device that is rings a bell (by activating a solenoid) and fade in and out a led every hours.
As i am not a super programmer I have used the "TimeAlarm action" that quite easy to use with an RTC clock. As I don't want to hear the bell during the night, I have a loop for the night and loop for the day. But it seams that there is a limit to alarm time library.... every thing works fine in my programm exept that nothing happens at the programed moments (every hours). It works only when there is just 5 alarm times programmed. Is the "TimeAlarm function limited?
Here is my code. Advise welcome! ;D
#include "Wire.h"
#include <Time.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h>
const byte ledPin = 9; // LED to pin 9
int magnetPin = 7; // SOLENOID to digit 7
void setup()
{
byte second, minute, hour;
Wire.begin();
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(magnetPin, OUTPUT);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
Alarm.alarmRepeat(8,0,0,alarm1);// Alarm with bell
Alarm.alarmRepeat(9,0,0,alarm1);
Alarm.alarmRepeat(10,0,0,alarm1);
Alarm.alarmRepeat(11,0,0,alarm1);
Alarm.alarmRepeat(12,0,0,alarm1);
Alarm.alarmRepeat(13,0,0,alarm1);
Alarm.alarmRepeat(14,0,0,alarm1);
Alarm.alarmRepeat(15,0,0,alarm1);
Alarm.alarmRepeat(16,0,0,alarm1);
Alarm.alarmRepeat(17,0,0,alarm1);
Alarm.alarmRepeat(18,0,0,alarm1);
Alarm.alarmRepeat(19,0,0,alarm1);
Alarm.alarmRepeat(20,0,0,alarm1);
Alarm.alarmRepeat(21,25,0,alarm1);
Alarm.alarmRepeat(22,0,0,alarm2);// Alarm without bell
Alarm.alarmRepeat(23,0,0,alarm2);
Alarm.alarmRepeat(0,0,0,alarm2);
Alarm.alarmRepeat(1,0,0,alarm2);
Alarm.alarmRepeat(2,0,0,alarm2);
Alarm.alarmRepeat(3,0,0,alarm2);
Alarm.alarmRepeat(4,0,0,alarm2);
Alarm.alarmRepeat(5,0,0,alarm2);
Alarm.alarmRepeat(6,0,0,alarm2);
Alarm.alarmRepeat(7,0,0,alarm2);
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// fading in function
void fadeIn(long duration, int pinum) {
const long delayTime = duration / 255;
for (byte i = 0; i < 255; ++i) {
analogWrite(pinum, i);
delay(delayTime);
}
digitalWrite(pinum, HIGH);
}
// fading out function
void fadeOut(long duration, int pinum) {
const long delayTime = duration / 255;
for (byte i = 255; i > 0; --i) {
analogWrite (pinum, i);
delay(delayTime);
}
digitalWrite(pinum, LOW);
}
// solenoid control
void magnet(){
digitalWrite(magnetPin, HIGH);
delay(20);
digitalWrite(magnetPin, LOW);
}
// alarm 1 with sound and light
void alarm1(){
Serial.println("ALARM 1-with sound");
fadeIn(30, ledPin);
magnet();
fadeOut(15000, ledPin);
}
// alarm 2 light only
void alarm2(){
Serial.println("ALARM 2-without sound");
fadeIn(30, ledPin);
fadeOut(15000, ledPin);
}
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);
}