From adfd9654e358a9f3b2526a8d043587d759ddbf5a Mon Sep 17 00:00:00 2001 From: feanaro Date: Thu, 10 Jun 2021 18:10:22 +0200 Subject: [PATCH 1/2] Bugfixes --- GetWired New Firmware/RShutterControl.cpp | 60 +++++++++++++---------- GetWired New Firmware/RShutterControl.h | 8 +-- GetWired New Firmware/main.ino | 14 ++++-- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/GetWired New Firmware/RShutterControl.cpp b/GetWired New Firmware/RShutterControl.cpp index dcd2d6b..3fdc730 100644 --- a/GetWired New Firmware/RShutterControl.cpp +++ b/GetWired New Firmware/RShutterControl.cpp @@ -52,55 +52,63 @@ void RShutterControl::Calibration(uint8_t UpTime, uint8_t DownTime) { EEPROM.put(EEA_RS_TIME_UP, _UpTime); EEPROM.put(EEA_RS_POSITION, Position); - Calibrated = true; + Calibrated = true; } // Read Message -uint8_t RShutterControl::ReadMessage(uint8_t Order) { +uint32_t RShutterControl::ReadMessage(uint8_t Order) { + + uint32_t MovementTime; if(State == 2) { NewState = Order; if(Order == 0) { - return _UpTime; + MovementTime = _UpTime * 1000; } else if(Order == 1) { - return _DownTime; + MovementTime = _DownTime * 1000; } else if(Order == 2) { - return 0; + MovementTime = 0; } } else { NewState = 2; - return 0; + MovementTime = 0; } + + return MovementTime; } // Read Buttons -uint8_t RShutterControl::ReadButtons(uint8_t Button) { +uint32_t RShutterControl::ReadButtons(uint8_t Button) { + + uint32_t MovementTime; // Roller shutter is stopped -> start moving if(State == 2) { NewState = Button; if(Button == 0) { - return _UpTime; + MovementTime = _UpTime * 1000; } else if(Button == 1) { - return _DownTime; + MovementTime = _DownTime * 1000; } } // Roller shutter is moving -> stop else { NewState = 2; - return 0; + MovementTime = 0; } + + return MovementTime; } // Read New Position -uint8_t RShutterControl::ReadNewPosition(uint8_t NewPosition) { +uint32_t RShutterControl::ReadNewPosition(int NewPosition) { - uint8_t MovementRange = NewPosition - Position; // Downward => MR > 0; Upward MR < 0 + int MovementRange = NewPosition - Position; // Downward => MR > 0; Upward MR < 0 bool MovementDirection = MovementRange > 0 ? 1 : 0; // MovementDirection: 1 -> Down; 0 -> Up uint32_t MovementTime; @@ -123,43 +131,43 @@ uint8_t RShutterControl::Movement() { if(NewState == 2) { digitalWrite(_UpPin, _RelayOff); digitalWrite(_DownPin, _RelayOff); - State = NewState; - return State; } // Move upward else if(NewState == 0) { digitalWrite(_DownPin, _RelayOff); digitalWrite(_UpPin, _RelayOn); - State = NewState; - return State; } // Move downward else if(NewState == 1) { digitalWrite(_UpPin, _RelayOff); digitalWrite(_DownPin, _RelayOn); - State = NewState; - return State; } + + State = NewState; + return State; } // Calculate new position -void RShutterControl::CalculatePosition(bool Direction, unsigned long MeasuredTime) { +void RShutterControl::CalculatePosition(bool Direction, uint32_t MeasuredTime) { uint8_t DirectionTime; + int NewPosition = Position; if(Direction) { - DirectionTime = _DownTime * 1000; + DirectionTime = _DownTime; } else { - DirectionTime = _UpTime * 1000; + DirectionTime = _UpTime; } - uint8_t PositionChange = (float) MeasuredTime / (float) DirectionTime; - PositionChange = PositionChange * 100; + float PositionChange = (float) MeasuredTime / (float) DirectionTime; + PositionChange = PositionChange / 10; + + NewPosition += Direction == 1 ? PositionChange : -PositionChange; + NewPosition = NewPosition > 100 ? 100 : NewPosition; + NewPosition = NewPosition < 0 ? 0 : NewPosition; - Position += Direction == 1 ? PositionChange : -PositionChange; - Position = Position > 100 ? 100 : Position; - Position = Position < 0 ? 0 : Position; + Position = (uint8_t)NewPosition; EEPROM.put(EEA_RS_POSITION, Position); } diff --git a/GetWired New Firmware/RShutterControl.h b/GetWired New Firmware/RShutterControl.h index 8e6dca8..6fca627 100644 --- a/GetWired New Firmware/RShutterControl.h +++ b/GetWired New Firmware/RShutterControl.h @@ -21,11 +21,11 @@ class RShutterControl bool Calibrated; void Calibration(uint8_t UpTime=0, uint8_t DownTime=0); // Finds up and down time by measuring current, save values to eeprom for use after power failure - uint8_t ReadMessage(uint8_t Order); - uint8_t ReadButtons(uint8_t Button); - uint8_t ReadNewPosition(uint8_t NewPosition); + uint32_t ReadMessage(uint8_t Order); + uint32_t ReadButtons(uint8_t Button); + uint32_t ReadNewPosition(int NewPosition); uint8_t Movement(); - void CalculatePosition(bool Direction, unsigned long MeasuredTime); + void CalculatePosition(bool Direction, uint32_t MeasuredTime); private: uint8_t _UpPin; diff --git a/GetWired New Firmware/main.ino b/GetWired New Firmware/main.ino index fd52812..feefaaa 100644 --- a/GetWired New Firmware/main.ino +++ b/GetWired New Firmware/main.ino @@ -53,7 +53,7 @@ // RShutter #ifdef ROLLER_SHUTTER bool Direction; - uint32_t MovementTime = 0; + uint32_t MovementTime; uint32_t StartTime; #endif @@ -587,7 +587,7 @@ void receive(const MyMessage &message) { else if (message.type == V_PERCENTAGE) { #ifdef ROLLER_SHUTTER if(message.sensor == RS_ID) { - uint8_t NewPosition = atoi(message.data); + int NewPosition = atoi(message.data); NewPosition = NewPosition > 100 ? 100 : NewPosition; NewPosition = NewPosition < 0 ? 0 : NewPosition; MovementTime = RS.ReadNewPosition(NewPosition); @@ -898,6 +898,14 @@ void RSUpdate() { StopTime = millis(); send(msgRS3); } + if(millis() < StartTime) { + uint32_t Temp = 4294967295 - StartTime + millis(); + wait(MovementTime - Temp); + RS.NewState = 2; + RS.Movement(); + send(msgRS3); + StopTime = MovementTime; + } } if(StopTime > 0) { @@ -1111,7 +1119,7 @@ void loop() { // Reset LastUpdate if millis() has overflowed if(LastUpdate > millis()) { LastUpdate = millis(); - } + } // Checking out sensors which report at a defined interval if ((millis() > LastUpdate + INTERVAL) || CheckNow == true) { From 6f9f9f63ed215f5a519bda319ac4baacdad72119 Mon Sep 17 00:00:00 2001 From: feanaro Date: Tue, 15 Jun 2021 15:01:35 +0200 Subject: [PATCH 2/2] Bugfixes --- GetWired New Firmware/main.ino | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/GetWired New Firmware/main.ino b/GetWired New Firmware/main.ino index 39a214a..799cd5e 100644 --- a/GetWired New Firmware/main.ino +++ b/GetWired New Firmware/main.ino @@ -160,6 +160,16 @@ void setup() { float Vcc = ReadVcc(); // mV + // POWER SENSOR + #if defined(POWER_SENSOR) && !defined(FOUR_RELAY) + PS.SetValues(PS_PIN, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); + #elif defined(POWER_SENSOR) && defined(FOUR_RELAY) + PS[RELAY_ID_1].SetValues(PS_PIN_1, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); + PS[RELAY_ID_2].SetValues(PS_PIN_2, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); + PS[RELAY_ID_3].SetValues(PS_PIN_3, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); + PS[RELAY_ID_4].SetValues(PS_PIN_4, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); + #endif + // OUTPUT #ifdef DOUBLE_RELAY IOD[RELAY_ID_1].SetValues(RELAY_OFF, 4, BUTTON_1, RELAY_1); @@ -236,16 +246,6 @@ void setup() { #endif #endif - // POWER SENSOR - #if defined(POWER_SENSOR) && !defined(FOUR_RELAY) - PS.SetValues(PS_PIN, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); - #elif defined(POWER_SENSOR) && defined(FOUR_RELAY) - PS[RELAY_ID_1].SetValues(PS_PIN_1, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); - PS[RELAY_ID_2].SetValues(PS_PIN_2, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); - PS[RELAY_ID_3].SetValues(PS_PIN_3, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); - PS[RELAY_ID_4].SetValues(PS_PIN_4, MVPERAMP, RECEIVER_VOLTAGE, MAX_CURRENT, POWER_MEASURING_TIME, Vcc); - #endif - // EXTERNAL THERMOMETER #ifdef EXTERNAL_TEMP #ifdef DHT22 @@ -589,6 +589,8 @@ void receive(const MyMessage &message) { int NewPosition = atoi(message.data); NewPosition = NewPosition > 100 ? 100 : NewPosition; NewPosition = NewPosition < 0 ? 0 : NewPosition; + RS.NewState = 2; + RSUpdate(); MovementTime = RS.ReadNewPosition(NewPosition); } #endif