Browse Source

Entrpellung implementiert – noch nicht getestet

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
entprellung
Stephan Richter 10 months ago
parent
commit
1a1b74609d
  1. 63
      Software/Sender/Sender.ino

63
Software/Sender/Sender.ino

@ -17,7 +17,7 @@ @@ -17,7 +17,7 @@
#define Max485_DE 8 // data enable of Max485
#define Max485_DI 9 // data input of Max485
#define TRESHOLD 200000 // 200ms
#define TRESHOLD 100
#define ID 0
// 0=Test
// 1=Arbeitszimmer
@ -26,13 +26,21 @@ @@ -26,13 +26,21 @@
//#define LOG_TO_SERIAL
#define SEND_485
#define DEBUG
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);
@ -54,26 +62,26 @@ void setup(){ @@ -54,26 +62,26 @@ 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] = !digitalRead(14);
raw_states[1] = !digitalRead(15);
raw_states[2] = !digitalRead(16);
raw_states[3] = !digitalRead(17);
raw_states[4] = !digitalRead(18);
raw_states[5] = !digitalRead(19);
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
}
#ifdef SEND_485
void send(int btn){
String s = "{nano:"+String(ID)+",btn:"+String(btn)+"}";
void send(int btn, long duration){
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",dura:"+String(duration)+"}";
for (int i = 0; i<10; i++){
if (send485(s.c_str())) break;
Serial.println("collision detected, trying again:");
@ -85,18 +93,31 @@ void loop(){ @@ -85,18 +93,31 @@ 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;
// 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;
#ifdef DEBUG
if (capacitor[i]) {
Serial.print("cap "); Serial.print(i); Serial.print(": "); Serial.println(capacitor[i]);
}
if (now - times[i] > TRESHOLD){
#ifdef SEND_485
send(i+1);
#endif
// 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];
times[i] = now;
#ifdef SEND_485
if (states[i]){ // old state was HIGH → we are going LOW
send(i+1,diff);
}
states[i] = 0;
#endif
states[i] = new_state;
}
}
}

Loading…
Cancel
Save