78 lines
2.1 KiB
C++
78 lines
2.1 KiB
C++
#include <Arduino.h>
|
|
|
|
#define MY_DEBUG
|
|
//#define MY_WITH_LEDS_BLINKING_INVERSE
|
|
#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
|
|
#define MY_DEFAULT_RX_LED_PIN 5 // Receive led pin
|
|
#define MY_DEFAULT_TX_LED_PIN 6 // the PCB, on board LED
|
|
|
|
// Enable and select radio type attached
|
|
#define MY_RADIO_RF24
|
|
#define MY_RF24_CHANNEL (105)
|
|
#include <MySensors.h>
|
|
|
|
#define RELAY_PIN 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
|
|
|
|
//MyMessage msgLev(1, V_DIMMER);
|
|
uint16_t lightLevel;
|
|
bool lightOn = false;
|
|
MyMessage msgMillis(1, V_VAR1);
|
|
|
|
void before()
|
|
{
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
pinMode(RELAY_PIN, OUTPUT);
|
|
lightLevel = loadState(0);
|
|
}
|
|
|
|
void presentation()
|
|
{
|
|
// Send the sketch version information to the gateway and Controller
|
|
sendSketchInfo("Night Light", "2.1");
|
|
present(0, S_DIMMER, "Dimmer");
|
|
present(1, S_CUSTOM, "Service");
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
static uint32_t cRun = millis();
|
|
if((cRun + 10000) <= millis()){
|
|
cRun = millis();
|
|
send(msgMillis.set(cRun));
|
|
}
|
|
}
|
|
|
|
void receive(const MyMessage &message)
|
|
{
|
|
switch(message.getType()){
|
|
case V_STATUS:
|
|
if(message.getBool() == true) {
|
|
analogWrite(RELAY_PIN, lightLevel);
|
|
lightOn = true;
|
|
}
|
|
else {
|
|
analogWrite(RELAY_PIN, 0);
|
|
lightOn = false;
|
|
}
|
|
Serial.print("Incoming change for sensor:");
|
|
Serial.print(message.getSensor());
|
|
Serial.print(", New status: ");
|
|
Serial.println(message.getBool());
|
|
break;
|
|
case V_PERCENTAGE:
|
|
uint16_t lev = message.getUInt();
|
|
if(lev > 100) lev = 100;
|
|
saveState(0, int(lev * 2.55));
|
|
lightLevel = int(lev * 2.55);
|
|
if(lightOn) analogWrite(RELAY_PIN, lightLevel);
|
|
Serial.print("Incoming change for dimmer:");
|
|
Serial.print(message.getSensor());
|
|
Serial.print(", New value: ");
|
|
Serial.println(message.getUInt());
|
|
break;
|
|
}
|
|
}
|