diff --git a/SoftRS485.cpp b/SoftRS485.cpp index 51df65e..8377e92 100644 --- a/SoftRS485.cpp +++ b/SoftRS485.cpp @@ -11,20 +11,23 @@ // Pins int _RO, _RE, _DE, _DI; -long _pulse_len = 616;// 1232; // Pulslänge von 1250µs - 18µs Zeit für Operationen -long _break = _pulse_len * 20; -// Receive vars + +const long _pulse_len = 616; +const long _break = _pulse_len * 20; boolean _line = LOW; -long _last_flip = 0; // this is the time when the bus switched to logical zero -byte _recv_msg[MSG_LEN]; -boolean _avail = false; +long _last_flip = 0; long _diff = 0; long _now = 0; -int _pos = 0; -int _idx = 0; long _count = 0; boolean _reset = false; +// Receive vars +byte _recv_msg[MSG_LEN]; +boolean _avail = false; +int _pos = 0; +int _idx = 0; + + diff --git a/examples/LCDReceive/LCDReceive.ino b/examples/LCDReceive/LCDReceive.ino index abae1d1..ea55f39 100644 --- a/examples/LCDReceive/LCDReceive.ino +++ b/examples/LCDReceive/LCDReceive.ino @@ -1,51 +1,15 @@ -/* - LiquidCrystal Library - Hello World - - Demonstrates the use a 16x2 LCD display. The LiquidCrystal - library works with all LCD displays that are compatible with the - Hitachi HD44780 driver. There are many of them out there, and you - can usually tell them by the 16-pin interface. - - This sketch prints "Hello World!" to the LCD - and shows the time. - - The circuit: - * LCD RS pin to digital pin 12 - * LCD Enable pin to digital pin 11 - * LCD D4 pin to digital pin 5 - * LCD D5 pin to digital pin 4 - * LCD D6 pin to digital pin 3 - * LCD D7 pin to digital pin 2 - * LCD R/W pin to ground - * LCD VSS pin to ground - * LCD VCC pin to 5V - * 10K resistor: - * ends to +5V and ground - * wiper to LCD VO pin (pin 3) - - Library originally added 18 Apr 2008 - by David A. Mellis - library modified 5 Jul 2009 - by Limor Fried (http://www.ladyada.net) - example added 9 Jul 2009 - by Tom Igoe - modified 22 Nov 2010 - by Tom Igoe - modified 7 Nov 2016 - by Arturo Guadalupi - - This example code is in the public domain. - - http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld - -*/ +/* + * Demonstrates how to use LCDisplay in conjunction with SoftRS485: + * + * The main _loop_ of this program listens on the RS485 bus. + * Whenever a message is received from the bus, it is displayed on the LCD. + */ // include the library code: #include #include -// 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 const int rs = 12, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); @@ -54,7 +18,7 @@ void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. - lcd.print("hello, world!"); + lcd.print("LCDReceive 1.0"); init485(2,3,8,9); // library initialization: // connect pin 2 to RO of Max485 // connect pin 3 to ^RE of Max485 @@ -62,12 +26,10 @@ void setup() { // connect pin 9 to DI of Max485 } -void loop() { - +void loop() { if (available485()) { - // set the cursor to column 0, line 1 - // (note: line 1 is the second row, since counting begins with 0): String s = get485message(); + Serial.println(s); lcd.clear(); lcd.setCursor(0, 0); lcd.print(s); diff --git a/examples/ReceiveDemo/ReceiveDemo.ino b/examples/ReceiveDemo/ReceiveDemo.ino index 7889c5d..732a87b 100644 --- a/examples/ReceiveDemo/ReceiveDemo.ino +++ b/examples/ReceiveDemo/ReceiveDemo.ino @@ -1,6 +1,6 @@ /* This sketch demonstrates, how to use the SoftRS485 library. - This library makes use of Arduinos timer 1 + It listens on the bus and echoes incoming messages to the serial console. */ #include diff --git a/examples/SendButton/SendButton.ino b/examples/SendButton/SendButton.ino deleted file mode 100644 index 50e85a0..0000000 --- a/examples/SendButton/SendButton.ino +++ /dev/null @@ -1,33 +0,0 @@ -/* - This sketch demonstrates, how to use the SoftRS485 library. - This library makes use of Arduinos timer 1 - */ - -#include - -const int buttonPin = 10; // the number of the pushbutton pin -boolean button_state = true; - -void setup() { - Serial.begin(115200); - - init485(2,3,4,5); // library initialization: - // connect pin 2 to RO of Max485 - // connect pin 3 to ^RE of Max485 - // connect pin 4 to DE of Max485 - // connect pin 5 to DI of Max485 - - pinMode(buttonPin, INPUT_PULLUP); -} - -void loop(){ - boolean new_state = digitalRead(buttonPin); - if (new_state != button_state){ - button_state = new_state; - if (new_state == LOW){ - send485("Button pressed"); - } else { - send485("Button released"); - } - } -} diff --git a/examples/SendOnButton/SendOnButton.ino b/examples/SendOnButton/SendOnButton.ino new file mode 100644 index 0000000..07f36d4 --- /dev/null +++ b/examples/SendOnButton/SendOnButton.ino @@ -0,0 +1,39 @@ +/* + This sketch demonstrates, how to use the SoftRS485 library. + It listens on the RS485 network bus. + When the button is pressed, it sends some messages on the bus. + */ + +#include + +#define BUTTON 7 + +boolean state = LOW; + +void setup() { + Serial.begin(115200); + Serial.println(F("This is ReceiveDemo, version 0.1")); + + init485(2,3,4,5); // library initialization: + // connect pin 2 to RO of Max485 + // connect pin 3 to ^RE of Max485 + // connect pin 4 to DE of Max485 + // connect pin 5 to DI of Max485 + pinMode(BUTTON,INPUT_PULLUP); +} + +void loop(){ + if (available485()) Serial.println("Received: "+get485message()); + + boolean new_state = digitalRead(BUTTON); + if (new_state != state){ + if (state){ + for (int i=1; i<16; i++){ + String s = "Button pressed "+String(i); + send485(s.c_str()); + delay(100); + } + } else send485("Button released!"); + state = new_state; + } +} diff --git a/examples/TempSend/TempSend.ino b/examples/TempSend/TempSend.ino index 7226460..81da200 100644 --- a/examples/TempSend/TempSend.ino +++ b/examples/TempSend/TempSend.ino @@ -2,10 +2,31 @@ #include #include +// TempSend +// +// This is an example for sending DHT11 measures over an RS485 multimaster network. +// It makes use of an internal timer to periodically query values from an DHT11 sensor, connected at pin 6: +// +// This timer is set up in the method "setupTimer". +// The _interrupt service routine_ (ISR) just sets a flag _readSample_, which is checked in the main loop. +// If the _readSample_ flag is set, a measure is fetched from the sensor and transmitted via _send485_ call. +// +// The main loop also listens for incoming messages and echos them on the Serial console. + int pinDHT11 = 6; int count = 0; +String s; +boolean readSample = false; + +SimpleDHT11 dht11(pinDHT11); // Sensor connected on pin 6 -SimpleDHT11 dht11(pinDHT11); +void setupTimer(){ + TCCR1A = 0; // Init Timer1A + TCCR1B = 0; // Init Timer1B + TCCR1B |= B00000101; // Prescaler = 1024 + OCR1A = 31250; // Timer Compare1A Register + TIMSK1 |= B00000010; // Enable Timer COMPA Interrupt +} void setup() { Serial.begin(115200); @@ -15,21 +36,32 @@ void setup() { // connect pin 3 to ^RE of Max485 // connect pin 4 to DE of Max485 // connect pin 5 to DI of Max485 - while (!send485("Hello world!")) {} + + while (!send485("Hello world!")) {} // send hello on network + setupTimer(); // start timer for DHT 11 measurements +} + +/* Interrupt service routine – sets _readSample_ flag */ +ISR(TIMER1_COMPA_vect){ + OCR1A += 31250; // Advance The COMPA Register + readSample = true; } void loop() { - delay(2000); - // read without samples. - byte temperature = 0; - byte humidity = 0; - int err = SimpleDHTErrSuccess; - if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { - Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err)); - Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000); - return; + if (available485()) Serial.println("Received: "+get485message()); + + if (readSample){ // it is time to take a reading! + readSample = false; + byte temperature = 0; + byte humidity = 0; + int err = SimpleDHTErrSuccess; + count++; + s = "Sample "+String(count); + if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { + s += " failed, err="+String(SimpleDHTErrCode(err))+","+String(SimpleDHTErrDuration(err)); + } else { + s += " ok: "+ String(temperature)+" *C, "+String(humidity)+"% rel"; + } + while (!send485(s.c_str())) { /* repeat until successfull */ } } - count++; - String s = "Sample "+String(count)+" ok: "+ String(temperature)+" *C, "+String(humidity)+"% rel"; - send485(s.c_str()); }