From 1985ffbb372039e526ce0fc6ee3089482991302a Mon Sep 17 00:00:00 2001 From: Alexey Cherkasov Date: Thu, 4 Feb 2021 16:40:41 +0300 Subject: [PATCH] Sleep for LampCtrl --- LightCtrlMyS/src/main.cpp | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/LightCtrlMyS/src/main.cpp b/LightCtrlMyS/src/main.cpp index 3010f5c..c5e1218 100644 --- a/LightCtrlMyS/src/main.cpp +++ b/LightCtrlMyS/src/main.cpp @@ -8,8 +8,8 @@ #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 +#define BUTT1 2 +#define BUTT2 3 #include #include @@ -22,14 +22,18 @@ bool stat1, stat2, Ack; void presentation(); void sendData(MyMessage msg, bool status); void sendData(MyMessage msg, uint32_t status); +void sendData(MyMessage msg, float status); MyMessage msgLamp1(0, V_STATUS); MyMessage msgLamp2(1, V_STATUS); MyMessage msgMillis(2, V_VAR1); +MyMessage msgVoltage(2, V_VAR2); Bounce butt1 = Bounce(), butt2 = Bounce(); +int BATTERY_SENSE_PIN = A0; void setup() { + analogReference(INTERNAL); butt1.attach(BUTT1, INPUT_PULLUP); butt2.attach(BUTT2, INPUT_PULLUP); butt1.interval(25); @@ -38,6 +42,9 @@ void setup() { void loop() { static uint32_t cRun = millis() - 60000; + + int sensorValue = analogRead(BATTERY_SENSE_PIN); + float v = sensorValue * 0.004659498; butt1.update(); butt2.update(); if(butt1.fell()){ @@ -50,10 +57,9 @@ void loop() { sendData(msgLamp2, !stat2); Serial.print(F("Change Sock2: ")); Serial.println(stat2); } - if((cRun + 60000) <= millis()){ - cRun = millis(); - sendData(msgMillis, cRun); - } + sendData(msgMillis, cRun); + sendData(msgVoltage, v); + sleep(digitalPinToInterrupt(BUTT1), FALLING, digitalPinToInterrupt(BUTT2), FALLING, 300000); } void presentation() @@ -61,7 +67,7 @@ void presentation() sendSketchInfo("LampCtrl", "1.0"); present(0, S_BINARY, "Lamp1"); present(1, S_BINARY, "Lamp2"); - present(2, S_CUSTOM, "ESMillis"); + present(2, S_CUSTOM, "MillisVoltage"); } void sendData(MyMessage msg, bool status) @@ -96,3 +102,20 @@ void sendData(MyMessage msg, uint32_t val) } } } + +void sendData(MyMessage msg, float val) +{ + bool send_data = false; + uint8_t count = 0; + Ack = false; + while(send_data == false){ + count++; + send_data = send(msg.set(val, 2), true); + if(!send_data) + wait(1000, C_SET, msg.type); + if ((count == 3) && (send_data == 0)){ // Если сделано 3 попытки и нет подтверждения отправки + count = 0; // Обнуляем счётчик + send_data = 1; // Выходим из цикла + } + } +}