From b7ba521c597fddfc799567626e7008624f5cee63 Mon Sep 17 00:00:00 2001 From: Dominik Bartsch Date: Fri, 25 Jun 2021 10:55:27 +0200 Subject: [PATCH 1/5] first approach with sensor controlled led sequence --- include/pwm.h | 5 +++++ src/main.cpp | 7 ++++--- src/pwm.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/include/pwm.h b/include/pwm.h index 68e7c3e..846adec 100644 --- a/include/pwm.h +++ b/include/pwm.h @@ -2,6 +2,9 @@ #include "PCA9685.h" +#define SENSOR1 15 +#define SENSOR2 12 + class Treppe { private: uint8_t stairs; @@ -10,8 +13,10 @@ class Treppe { uint16_t active_brightness = 2048; uint8_t direction = 0; + uint8_t switch_direction = 0; uint8_t state = 0; uint8_t switch_state = 0; + uint8_t finish = 1; // initialize with i2c-Address 0, use Wire Library PCA9685 pwmController; diff --git a/src/main.cpp b/src/main.cpp index c77f429..82b4c00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -104,16 +104,17 @@ void setup() { os_timer_setfn(&timer1, timerCallback, &timer_flag); os_timer_arm(&timer1, 20, true); - stairs.setState(1); - stairs.setDirection(1); + //stairs.setState(1); + //stairs.setDirection(1); } void loop() { - if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0); + /*if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0); if(millis() > 45000 && stairs.getDirection() == 1){ stairs.setState(1); stairs.setDirection(0); } + */ TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA"); TIMEIF_US(httpServer.handleClient(), 1000, "HTTP"); } \ No newline at end of file diff --git a/src/pwm.cpp b/src/pwm.cpp index a217124..d3a6870 100644 --- a/src/pwm.cpp +++ b/src/pwm.cpp @@ -41,7 +41,6 @@ void Treppe::ledsequence(){ static int8_t led = 0; static uint16_t brightness = 0; static uint16_t lastbrightness = 0; - static uint8_t finish = 1; static uint16_t status = 0; uint16_t status_build = 0; status_build |= direction << 8; @@ -94,17 +93,54 @@ void Treppe::ledsequence(){ void Treppe::setup(){ pwmController.resetDevices(); - // Deactive PCA9685 due to LED Flickering + // Deactive PCA9685 Phase Balancer due to LED Flickering // https://github.com/NachtRaveVL/PCA9685-Arduino/issues/15 // see also lib/PCA9685-Arduin/PCA9685.h:204 pwmController.init(PCA9685_PhaseBalancer_None); pwmController.setPWMFrequency(200); + + pinMode(SENSOR1, INPUT); + pinMode(SENSOR2, INPUT); Serial.println("Hello from Treppe"); Serial.print("Treppe: initial parameters: stairs="); Serial.println(stairs); } void Treppe::task(){ - ledsequence(); + + if(finish){ + direction = switch_direction; + state = switch_state; + } + static uint8_t last_sensor_state[2] = {0,0}; + uint8_t current_sensor_state[2] = {0,0}; + current_sensor_state[0] = digitalRead(SENSOR1); + current_sensor_state[1] = digitalRead(SENSOR2); + + if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){ + setDirection(1); + setState(1); + } + + if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){ + setDirection(0); + setState(1); + } + + // first switch - off approach, use timer later + if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){ + setDirection(1); + setState(0); + } + + if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){ + setDirection(0); + setState(0); + } + + last_sensor_state[0] = current_sensor_state[0]; + last_sensor_state[1] = current_sensor_state[1]; + ledsequence(); + } uint16_t Treppe::setIdle(uint16_t _idle_brightness){ @@ -124,17 +160,21 @@ uint16_t Treppe::setTime(uint16_t _time_per_stair){ } uint8_t Treppe::setDirection(uint8_t _direction){ - direction = _direction; + switch_direction = _direction; Serial.println("Treppe: Direction changed!"); + if(finish) Serial.println("apply direction request immediately"); + else Serial.println("currently active, dir change afterwards"); // to do: implement state command variable to determine dimm-state - return direction; + return switch_direction; } uint8_t Treppe::setState(uint8_t _state){ if(state == _state) return 1; else{ - state = _state; - Serial.println("Treppe: State changed!"); + switch_state = _state; + Serial.println("Treppe: State Request changed!"); + if(finish) Serial.println("apply state request immediately"); + else Serial.println("currently active, state changes after activity"); } return 0; } \ No newline at end of file From 7e5a6fac9f8b9969161f13ec0d8c2e87b6d70089 Mon Sep 17 00:00:00 2001 From: Dominik Bartsch Date: Fri, 25 Jun 2021 11:03:17 +0200 Subject: [PATCH 2/5] doku --- doku.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doku.md b/doku.md index 29a0b86..ae12d46 100644 --- a/doku.md +++ b/doku.md @@ -54,6 +54,12 @@ else server.streamFile(f, mime::getContentType(SRH::_path), requestMethod); ``` +LED_sequence_v1: +TODO: +- disable led stripes after defined time (webserver paramter?) +- define what to do if people enter stairs from different directions +- CAUTION: Sensor-Deadtime at LEAST 8 seconds +- --> switch off with person leaving stairs might not be possible Webserver on v0.3.0 now working with LittleFS TODO: From a8ceb258b8e90889d4c072ef3e87d7b87aa4c9d8 Mon Sep 17 00:00:00 2001 From: Dominik Bartsch Date: Fri, 25 Jun 2021 15:17:38 +0200 Subject: [PATCH 3/5] leds light up at boot --- src/pwm.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pwm.cpp b/src/pwm.cpp index d3a6870..4b911f3 100644 --- a/src/pwm.cpp +++ b/src/pwm.cpp @@ -98,6 +98,7 @@ void Treppe::setup(){ // see also lib/PCA9685-Arduin/PCA9685.h:204 pwmController.init(PCA9685_PhaseBalancer_None); pwmController.setPWMFrequency(200); + pwmController.setAllChannelsPWM(idle_brightness); pinMode(SENSOR1, INPUT); pinMode(SENSOR2, INPUT); From 38c81ae1827a13816d8c28d54873ccd3d23cb078 Mon Sep 17 00:00:00 2001 From: Dominik Bartsch Date: Fri, 25 Jun 2021 15:25:09 +0200 Subject: [PATCH 4/5] main cleanup --- src/main.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 82b4c00..8d5a49a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,18 +103,9 @@ void setup() { os_timer_setfn(&timer1, timerCallback, &timer_flag); os_timer_arm(&timer1, 20, true); - - //stairs.setState(1); - //stairs.setDirection(1); } void loop() { - /*if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0); - if(millis() > 45000 && stairs.getDirection() == 1){ - stairs.setState(1); - stairs.setDirection(0); - } - */ TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA"); TIMEIF_US(httpServer.handleClient(), 1000, "HTTP"); } \ No newline at end of file From 33499beb32820511c5752575f32c46ecfabc9cad Mon Sep 17 00:00:00 2001 From: Dominik Bartsch Date: Fri, 25 Jun 2021 15:41:34 +0200 Subject: [PATCH 5/5] zero duty cycle fix --- include/pwm.h | 7 ++++--- src/pwm.cpp | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/include/pwm.h b/include/pwm.h index 846adec..19c957e 100644 --- a/include/pwm.h +++ b/include/pwm.h @@ -5,12 +5,13 @@ #define SENSOR1 15 #define SENSOR2 12 +#define INT_TIME 20 // interrupt intervall [ms] class Treppe { private: uint8_t stairs; - uint16_t time_per_stair = 300; // dimmtime per stair [ms] - uint16_t idle_brightness = 200; - uint16_t active_brightness = 2048; + uint16_t time_per_stair = 200; // dimmtime per stair [ms] + uint16_t idle_brightness = 0; + uint16_t active_brightness = 1048; uint8_t direction = 0; uint8_t switch_direction = 0; diff --git a/src/pwm.cpp b/src/pwm.cpp index 4b911f3..13c7397 100644 --- a/src/pwm.cpp +++ b/src/pwm.cpp @@ -20,12 +20,10 @@ uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){ pwmController.setChannelPWM(led, (uint16_t)startval); lastled = led; current_pwm = startval; - stepsize = 20*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval + stepsize = INT_TIME*abs(stopval - startval)/(float)time_per_stair; // only valid at 1ms function call interval return 1; } - if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize) return 0; - // todo: duty cycle zero! if(startval > stopval){ current_pwm -= stepsize; } @@ -34,6 +32,10 @@ uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){ } Serial.println((uint16_t)current_pwm); pwmController.setChannelPWM(led, (uint16_t)current_pwm); + if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){ + if(stopval == 0) pwmController.setChannelPWM(led, 0); + return 0; + } return 1; }