Hello Forum,
I try to make a program to control my AC Dimmer in time.
I already tried to use the Timealarms libary.
But some how it doesn't work.
/*
* TimeAlarmExample.pde
*
* This example calls alarm functions at 8:30 am and at 5:45 pm (17:45)
* and simulates turning lights on at night and off in the morning
* A weekly timer is set for Saturdays at 8:30:30
*
* A timer is called every 15 seconds
* Another timer is called once only after 10 seconds
*
* At startup the time is set to Jan 1 2011 8:29 am
*/
// Questions? Ask them here:
// http://forum.arduino.cc/index.php?topic=66054.0
#include <TimeLib.h>
#include <TimeAlarms.h>
AlarmId id;
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms, to trigger at specific times
Alarm.alarmRepeat(8,30,0, MorningAlarm); // 8:30am every day
Alarm.alarmRepeat(17,45,0,EveningAlarm); // 5:45pm every day
Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm); // 8:30:30 every Saturday
// create timers, to trigger relative to when they're created
Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
id = Alarm.timerRepeat(2, Repeats2); // timer for every 2 seconds
Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
}
void loop() {
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void MorningAlarm() {
Serial.println("Alarm: - turn lights off");
}
void EveningAlarm() {
Serial.println("Alarm: - turn lights on");
}
void WeeklyAlarm() {
Serial.println("Alarm: - its Monday Morning");
}
void ExplicitAlarm() {
Serial.println("Alarm: - this triggers only at the given date and time");
}
void Repeats() {
Serial.println("15 second timer");
}
void Repeats2() {
Serial.println("2 second timer");
}
void OnceOnly() {
Serial.println("This timer only triggers once, stop the 2 second timer");
// use Alarm.free() to disable a timer and recycle its memory.
Alarm.free(id);
// optional, but safest to "forget" the ID after memory recycled
id = dtINVALID_ALARM_ID;
// you can also use Alarm.disable() to turn the timer off, but keep
// it in memory, to turn back on later with Alarm.enable().
}
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);
}
Example:
18:00 hours 100% both outputs.
21:00 hours 75% output 1 and 25% ouput 2.
/*
AC Light Dimmer
AC Voltage dimmer with Zero cross detection
Attach the Zero cross pin of the module to Arduino External Interrupt pin
Select the correct Interrupt # from the below table
Pin | Interrrupt # | Arduino Platform
---------------------------------------
2 | 0 | All
3 | 1 | All
18 | 5 | Arduino Mega Only
19 | 4 | Arduino Mega Only
20 | 3 | Arduino Mega Only
21 | 2 | Arduino Mega Only
*/
int AC_LOAD1 = 10; // Output to Opto Triac pin
int AC_LOAD2 = 11; // Output to Opto Triac pin
int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
void setup()
{
pinMode(AC_LOAD1, OUTPUT); // Set the AC Load as output
pinMode(AC_LOAD2, OUTPUT); // Set the AC Load as output
attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}
void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
// (10000us - 10us) / 128 = 75 (Approx)
int dimtime1 = (75*dimming1);
int dimtime2 = (75*dimming2);
if ( dimtime1 > dimtime2)
{ delayMicroseconds(dimtime2);
digitalWrite(AC_LOAD2, HIGH);
delayMicroseconds(10);
digitalWrite(AC_LOAD2, LOW);
delayMicroseconds( dimtime1 - dimtime2);
digitalWrite(AC_LOAD1, HIGH);
delayMicroseconds(10);
digitalWrite(AC_LOAD1, LOW);
}
else
{ delayMicroseconds(dimtime1);
digitalWrite(AC_LOAD1, HIGH);
delayMicroseconds(10);
digitalWrite(AC_LOAD1, LOW);
delayMicroseconds( dimtime2 - dimtime1);
digitalWrite(AC_LOAD2, HIGH);
delayMicroseconds(10);
digitalWrite(AC_LOAD2, LOW);
}
}
void loop()
{
dimming1 = 128; // Min Setting 124
dimming2 = 128;
delay(1000);
}
Best Regards,
Henk,
