|
|
|
/*
|
|
|
|
* Demonstrates how to use Read Buttons in conjunction with SoftRS485:
|
|
|
|
*
|
|
|
|
* If a button is pressed, a corresponding flag is set.
|
|
|
|
* The main loop checks for the state of those flags and
|
|
|
|
* sends a message on the RS485 bus whenever a flag is set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// include the library code:
|
|
|
|
#include <SoftRS485.h>
|
|
|
|
|
|
|
|
// 0=Test
|
|
|
|
// 1=Arbeitszimmer
|
|
|
|
// 2=Küche
|
|
|
|
// 3=Bad
|
|
|
|
// 4=Treppenhaus
|
|
|
|
|
|
|
|
#define ID 1
|
|
|
|
#define PROGRAM "Sender"
|
|
|
|
#define VERSION "1.3"
|
|
|
|
#define REVISION "2.0"
|
|
|
|
|
|
|
|
#define BTN_INT 2 // button interrupt pin
|
|
|
|
#define Max485_RO 3 // read-output of Max485
|
|
|
|
#define Max485_RE 5 // not-read-enable of Max485
|
|
|
|
#define Max485_DE 9 // data enable of Max485
|
|
|
|
#define Max485_DI 8 // data input of Max485
|
|
|
|
|
|
|
|
#define TRESHOLD 1024
|
|
|
|
#define TRIGGER_LEVEL 200
|
|
|
|
|
|
|
|
#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);
|
|
|
|
pinMode(14,INPUT_PULLUP);
|
|
|
|
pinMode(15,INPUT_PULLUP);
|
|
|
|
pinMode(16,INPUT_PULLUP);
|
|
|
|
pinMode(17,INPUT_PULLUP);
|
|
|
|
pinMode(18,INPUT_PULLUP);
|
|
|
|
pinMode(19,INPUT_PULLUP);
|
|
|
|
pinMode(20,INPUT_PULLUP);
|
|
|
|
pinMode(21,INPUT_PULLUP);
|
|
|
|
|
|
|
|
attachInterrupt(digitalPinToInterrupt(BTN_INT),isr,CHANGE);
|
|
|
|
String s = "{nano:"+String(ID)+",hw:"+REVISION+","+PROGRAM+":"+VERSION+"}";
|
|
|
|
Serial.println(s);
|
|
|
|
send485(s.c_str());
|
|
|
|
#ifdef LOG_TO_SERIAL
|
|
|
|
Serial.println("14 15 16 17 18 19 20 21");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void isr(){
|
|
|
|
raw_states[0] = analogRead(14)<TRIGGER_LEVEL;
|
|
|
|
raw_states[1] = analogRead(15)<TRIGGER_LEVEL;
|
|
|
|
raw_states[2] = analogRead(16)<TRIGGER_LEVEL;
|
|
|
|
raw_states[3] = analogRead(17)<TRIGGER_LEVEL;
|
|
|
|
|
|
|
|
raw_states[4] = analogRead(18)<TRIGGER_LEVEL;
|
|
|
|
raw_states[5] = analogRead(19)<TRIGGER_LEVEL;
|
|
|
|
raw_states[6] = analogRead(20)<TRIGGER_LEVEL;
|
|
|
|
raw_states[7] = analogRead(21)<TRIGGER_LEVEL;
|
|
|
|
#ifdef LOG_TO_SERIAL
|
|
|
|
for (int i=0;i<8;i++){
|
|
|
|
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)+",ms:"+String(duration)+"}";
|
|
|
|
#ifdef SEND_485
|
|
|
|
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();
|
|
|
|
for (int i=0;i<8;i++){
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|