Add LightCtrl project
This commit is contained in:
98
LightCtrlMyS/src/main.cpp
Normal file
98
LightCtrlMyS/src/main.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <Arduino.h>
|
||||
#define MY_DEBUG
|
||||
#define MY_RADIO_RF24
|
||||
#define MY_RF24_CHANNEL (105)
|
||||
#define MY_RF24_PA_LEVEL RF24_PA_MAX
|
||||
#define MY_REPEATER_FEATURE
|
||||
|
||||
#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
|
||||
#define BUTT1 8
|
||||
#define BUTT2 7
|
||||
|
||||
#include <MySensors.h>
|
||||
#include <Bounce2.h>
|
||||
|
||||
|
||||
// #undef MY_NODE_ID
|
||||
// #define MY_NODE_ID 14
|
||||
|
||||
bool stat1, stat2, Ack;
|
||||
void presentation();
|
||||
void sendData(MyMessage msg, bool status);
|
||||
void sendData(MyMessage msg, uint32_t status);
|
||||
|
||||
MyMessage msgLamp1(0, V_STATUS);
|
||||
MyMessage msgLamp2(1, V_STATUS);
|
||||
MyMessage msgMillis(2, V_VAR1);
|
||||
|
||||
Bounce butt1 = Bounce(), butt2 = Bounce();
|
||||
|
||||
void setup() {
|
||||
butt1.attach(BUTT1, INPUT_PULLUP);
|
||||
butt2.attach(BUTT2, INPUT_PULLUP);
|
||||
butt1.interval(25);
|
||||
butt2.interval(25);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static uint32_t cRun = millis() - 60000;
|
||||
butt1.update();
|
||||
butt2.update();
|
||||
if(butt1.fell()){
|
||||
stat1 = !stat1;
|
||||
sendData(msgLamp1, !stat1);
|
||||
Serial.print(F("Change Sock1: ")); Serial.println(stat1);
|
||||
}
|
||||
if(butt2.fell()){
|
||||
stat2 = !stat2;
|
||||
sendData(msgLamp2, !stat2);
|
||||
Serial.print(F("Change Sock2: ")); Serial.println(stat2);
|
||||
}
|
||||
if((cRun + 60000) <= millis()){
|
||||
cRun = millis();
|
||||
sendData(msgMillis, cRun);
|
||||
}
|
||||
}
|
||||
|
||||
void presentation()
|
||||
{
|
||||
sendSketchInfo("LampCtrl", "1.0");
|
||||
present(0, S_BINARY, "Lamp1");
|
||||
present(1, S_BINARY, "Lamp2");
|
||||
present(2, S_CUSTOM, "ESMillis");
|
||||
}
|
||||
|
||||
void sendData(MyMessage msg, bool status)
|
||||
{
|
||||
bool send_data = false;
|
||||
uint8_t count = 0;
|
||||
while(send_data == false){
|
||||
count++;
|
||||
send_data = send(msg.set(status), true);
|
||||
if(!send_data)
|
||||
wait(1000, C_SET, msg.type);
|
||||
if ((count == 3 )&&(send_data == 0)){ // Если сделано 3 попытки и нет подтверждения отправки
|
||||
count = 0; // Обнуляем счётчик
|
||||
send_data = 1; // Выходим из цикла
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sendData(MyMessage msg, uint32_t val)
|
||||
{
|
||||
bool send_data = false;
|
||||
uint8_t count = 0;
|
||||
Ack = false;
|
||||
while(send_data == false){
|
||||
count++;
|
||||
send_data = send(msg.set(val), true);
|
||||
if(!send_data)
|
||||
wait(1000, C_SET, msg.type);
|
||||
if ((count == 3) && (send_data == 0)){ // Если сделано 3 попытки и нет подтверждения отправки
|
||||
count = 0; // Обнуляем счётчик
|
||||
send_data = 1; // Выходим из цикла
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user