From 97a0e5b5b40577b09283718ffbd03e06ccdf7c02 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 16 Dec 2023 17:52:30 +0100 Subject: [PATCH 1/2] Dateien geordnet Signed-off-by: Stephan Richter --- Nano845.fzz => Hardware/Nano845.fzz | Bin {BiDi => Software/BiDi}/BiDi.ino | 0 {ManualSend => Software/ManualSend}/ManualSend.ino | 0 {ManualSend => Software/ManualSend}/Readme.md | 0 {Receiver => Software/Receiver}/Receiver.ino | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename Nano845.fzz => Hardware/Nano845.fzz (100%) rename {BiDi => Software/BiDi}/BiDi.ino (100%) rename {ManualSend => Software/ManualSend}/ManualSend.ino (100%) rename {ManualSend => Software/ManualSend}/Readme.md (100%) rename {Receiver => Software/Receiver}/Receiver.ino (100%) diff --git a/Nano845.fzz b/Hardware/Nano845.fzz similarity index 100% rename from Nano845.fzz rename to Hardware/Nano845.fzz diff --git a/BiDi/BiDi.ino b/Software/BiDi/BiDi.ino similarity index 100% rename from BiDi/BiDi.ino rename to Software/BiDi/BiDi.ino diff --git a/ManualSend/ManualSend.ino b/Software/ManualSend/ManualSend.ino similarity index 100% rename from ManualSend/ManualSend.ino rename to Software/ManualSend/ManualSend.ino diff --git a/ManualSend/Readme.md b/Software/ManualSend/Readme.md similarity index 100% rename from ManualSend/Readme.md rename to Software/ManualSend/Readme.md diff --git a/Receiver/Receiver.ino b/Software/Receiver/Receiver.ino similarity index 100% rename from Receiver/Receiver.ino rename to Software/Receiver/Receiver.ino From d316dc83aa6b67f3fd07c4c08002cbca8fd24426 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 19 Dec 2023 00:12:41 +0100 Subject: [PATCH 2/2] Entprellung implementiert Signed-off-by: Stephan Richter --- Software/Sender/Sender.ino | 75 +++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/Software/Sender/Sender.ino b/Software/Sender/Sender.ino index 95c3bcd..a14aec2 100644 --- a/Software/Sender/Sender.ino +++ b/Software/Sender/Sender.ino @@ -9,7 +9,7 @@ // include the library code: #include -#define PROGRAM "RS485-Nano 2.0 / Sender 1.1" +#define PROGRAM "RS485-Nano 2.0 / Sender 1.2" #define BTN_INT 2 // button interrupt pin #define Max485_RO 3 // read-output of Max485 @@ -17,22 +17,30 @@ #define Max485_DE 9 // data enable of Max485 #define Max485_DI 8 // data input of Max485 -#define TRESHOLD 200000 // 200ms +#define TRESHOLD 1024 #define ID 0 // 0=Test // 1=Arbeitszimmer // 2=Küche // 3=Bad -//#define LOG_TO_SERIAL -#define SEND_485 +#define DEBUG +#define LOG_TO_SERIAL +//#define SEND_485 +boolean raw_states[8]; boolean states[8]; +unsigned long capacitor[8]; unsigned long times[8]; void setup(){ + for (int i=0; i<8;i++) { + capacitor[i] = 0; + raw_states[i] = LOW; + states[i] = LOW; + } Serial.begin(115200); init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization: pinMode(BTN_INT,INPUT_PULLUP); @@ -45,7 +53,7 @@ void setup(){ pinMode(20,INPUT_PULLUP); pinMode(21,INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(2),isr,CHANGE); + attachInterrupt(digitalPinToInterrupt(BTN_INT),isr,CHANGE); // Print a message to the LCD. Serial.println(PROGRAM); #ifdef LOG_TO_SERIAL @@ -54,49 +62,56 @@ void setup(){ } void isr(){ - states[0] = !digitalRead(14); - states[1] = !digitalRead(15); - states[2] = !digitalRead(16); - states[3] = !digitalRead(17); - - states[4] = !digitalRead(18); - states[5] = !digitalRead(19); - states[6] = !(analogRead(20)>200); - states[7] = !(analogRead(21)>200); + raw_states[0] = analogRead(14)<400; + raw_states[1] = analogRead(15)<400; + raw_states[2] = analogRead(16)<400; + raw_states[3] = analogRead(17)<400; + + raw_states[4] = analogRead(18)<400; + raw_states[5] = analogRead(19)<400; + raw_states[6] = analogRead(20)<400; + raw_states[7] = analogRead(21)<400; #ifdef LOG_TO_SERIAL for (int i=0;i<8;i++){ - Serial.print(" "); Serial.print(states[i]); Serial.print(" "); + Serial.print(" "); + Serial.print(raw_states[i]); + Serial.print(" "); } Serial.println(); #endif } +void send(int btn, long duration){ + String s = "{nano:"+String(ID)+",btn:"+String(btn)+",dura:"+String(duration)+"}"; #ifdef SEND_485 -void send(int btn){ - String s = "{nano:"+String(ID)+",btn:"+String(btn)+"}"; for (int i = 0; i<10; i++){ if (send485(s.c_str())) break; Serial.println("collision detected, trying again:"); } -} +#else + Serial.println(s); #endif +} void loop(){ unsigned long now = micros(); boolean pause = true; for (int i=0;i<8;i++){ - if (states[i]){ - if (pause){ - delay(5); - pause=false; - } - if (now - times[i] > TRESHOLD){ -#ifdef SEND_485 - send(i+1); -#endif - times[i] = now; - } - states[i] = 0; + // we emulate a capacitor: + // when the button is pressed, it slowly loads, if the button is released, it unloads. + capacitor[i] = raw_states[i] ? capacitor[i]+1 : capacitor[i]>>1; + + // if the capacitor reaches a certain treshold, the state is changed + boolean new_state = capacitor[i]>TRESHOLD; + + // TODO: drop state after a maximum HIGH time + + // when the state changes, the duration of the last state is calculated + if (states[i] != new_state){ + long diff = (now - times[i])/1000; + times[i] = now; + if (states[i]) send(i+1,diff); // old state was HIGH → we are going LOW + states[i] = new_state; } } }