Browse Source

Merge branch 'nano-revision-2.1' into nano-revision-2.2

nano-revision-2.2
Stephan Richter 10 months ago
parent
commit
faf3f4ece3
  1. 6
      Software/LCDReceiver/LCDReceiver.ino
  2. 68
      Software/Sender/Sender.ino

6
Software/LCDReceiver/LCDReceiver.ino

@ -10,7 +10,7 @@
#include <SoftRS485.h> #include <SoftRS485.h>
#define Max485_RO 2 // read-output of Max485 #define Max485_RO 2 // read-output of Max485
#define Max485_RE 3 // not-read-enable of Max485 #define Max485_RE 8 // not-read-enable of Max485
#define Max485_DE 8 // data enable of Max485 #define Max485_DE 8 // data enable of Max485
#define Max485_DI 9 // data input of Max485 #define Max485_DI 9 // data input of Max485
@ -21,7 +21,7 @@
#define LCD_D6 6 #define LCD_D6 6
#define LCD_D7 7 #define LCD_D7 7
#define PROGRAM "RS485-Nano 2.1 / LCDReceive 1.2" #define PROGRAM "RS485-Nano 2.2 / LCDReceive 1.2"
// initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to // initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
@ -31,7 +31,7 @@ LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization: init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
invert485polarity(true); //invert485polarity(true);
// set up the LCD's number of columns and rows: // set up the LCD's number of columns and rows:
lcd.begin(16, 2); lcd.begin(16, 2);
// Print a message to the LCD. // Print a message to the LCD.

68
Software/Sender/Sender.ino

@ -9,7 +9,7 @@
// include the library code: // include the library code:
#include <SoftRS485.h> #include <SoftRS485.h>
#define PROGRAM "RS485-Nano 2.1 / Sender 1.1" #define PROGRAM "RS485-Nano 2.2 / Sender 1.2"
#define BTN_INT 3 // button interrupt pin #define BTN_INT 3 // button interrupt pin
#define Max485_RO 2 // read-output of Max485 #define Max485_RO 2 // read-output of Max485
@ -17,7 +17,7 @@
#define Max485_DE 8 // data enable of Max485 #define Max485_DE 8 // data enable of Max485
#define Max485_DI 9 // data input of Max485 #define Max485_DI 9 // data input of Max485
#define TRESHOLD 200000 // 200ms #define TRESHOLD 1024
#define ID 0 #define ID 0
// 0=Test // 0=Test
// 1=Arbeitszimmer // 1=Arbeitszimmer
@ -27,12 +27,19 @@
//#define LOG_TO_SERIAL //#define LOG_TO_SERIAL
#define SEND_485 #define SEND_485
boolean raw_states[8];
boolean states[8]; boolean states[8];
unsigned long capacitor[8];
unsigned long times[8]; unsigned long times[8];
void setup(){ void setup(){
for (int i=0; i<8;i++) {
capacitor[i] = 0;
raw_states[i] = LOW;
states[i] = LOW;
}
Serial.begin(115200); Serial.begin(115200);
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization: init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
pinMode(BTN_INT,INPUT_PULLUP); pinMode(BTN_INT,INPUT_PULLUP);
@ -45,7 +52,7 @@ void setup(){
pinMode(20,INPUT_PULLUP); pinMode(20,INPUT_PULLUP);
pinMode(21,INPUT_PULLUP); pinMode(21,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2),isr,CHANGE); attachInterrupt(digitalPinToInterrupt(BTN_INT),isr,CHANGE);
// Print a message to the LCD. // Print a message to the LCD.
Serial.println(PROGRAM); Serial.println(PROGRAM);
#ifdef LOG_TO_SERIAL #ifdef LOG_TO_SERIAL
@ -54,49 +61,56 @@ void setup(){
} }
void isr(){ void isr(){
states[0] = !digitalRead(14); raw_states[0] = analogRead(14)<400;
states[1] = !digitalRead(15); raw_states[1] = analogRead(15)<400;
states[2] = !digitalRead(16); raw_states[2] = analogRead(16)<400;
states[3] = !digitalRead(17); raw_states[3] = analogRead(17)<400;
states[4] = !digitalRead(18); raw_states[4] = analogRead(18)<400;
states[5] = !digitalRead(19); raw_states[5] = analogRead(19)<400;
states[6] = !(analogRead(20)>200); raw_states[6] = analogRead(20)<400;
states[7] = !(analogRead(21)>200); raw_states[7] = analogRead(21)<400;
#ifdef LOG_TO_SERIAL #ifdef LOG_TO_SERIAL
for (int i=0;i<8;i++){ 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(); Serial.println();
#endif #endif
} }
void send(int btn, long duration){
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",ms:"+String(duration)+"}";
#ifdef SEND_485 #ifdef SEND_485
void send(int btn){
String s = "{nano:"+String(ID)+",btn:"+String(btn)+"}";
for (int i = 0; i<10; i++){ for (int i = 0; i<10; i++){
if (send485(s.c_str())) break; if (send485(s.c_str())) break;
Serial.println("collision detected, trying again:"); Serial.println("collision detected, trying again:");
} }
} #else
Serial.println(s);
#endif #endif
}
void loop(){ void loop(){
unsigned long now = micros(); unsigned long now = micros();
boolean pause = true; boolean pause = true;
for (int i=0;i<8;i++){ for (int i=0;i<8;i++){
if (states[i]){ // we emulate a capacitor:
if (pause){ // when the button is pressed, it slowly loads, if the button is released, it unloads.
delay(5); capacitor[i] = raw_states[i] ? capacitor[i]+1 : capacitor[i]>>1;
pause=false;
} // if the capacitor reaches a certain treshold, the state is changed
if (now - times[i] > TRESHOLD){ boolean new_state = capacitor[i]>TRESHOLD;
#ifdef SEND_485
send(i+1); // TODO: drop state after a maximum HIGH time
#endif
// 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; times[i] = now;
} if (states[i]) send(i+1,diff); // old state was HIGH → we are going LOW
states[i] = 0; states[i] = new_state;
} }
} }
} }

Loading…
Cancel
Save