Instead of one (1) push button and one (1) LED as shown in the example sketch "lora-demo3.ino" I need two (2) push buttons and two (2) LEDs. Same hardware setup on both sides, addresses swapped. Hardware: Nano 3 with 12VDC supply and ADA RFM9x, 868MHz (Europe) with 5V supply. I included a 2nd push button and 2nd LED in the sketch. The result: It works sometimes but hangs after a few push button inputs. Since I am not really familiar with LoRa and since I need this setup for one project (a farm in a rural area) only, I ask for help. The farmer relies on what I will install. The dab on the i would be the additional implementation of a temperature-/humidity sensor. Dear folks, I spent hours seaching the web but did not find a solution to my problem. Again, please help. Thanks in advance.
@sunnyboy69 Use the <> to post the code so it can be copied ^
why do you need two PB's and LED? Can you state a requirement, not a possible solution?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@sunnyboy69 Without a statement of requirements I have to guess at what you are trying to do so my guess is you want two different messages to be sent if the corresponding button is pressed. Your code has two errors that prevent that.
1. You do not check button 2 to see if it is pressed.
2. you can only check button 2 if button 1 was pressed.
This is my guess at what you want, it may be incorrect as I can only guess
void loop() { // Button 1 code starts here sendButton1State = digitalRead(button1Pin); // Get pushbutton state // Send packet if button pressed if (sendButton1State == LOW) { // Compose and send message outMessage = button1Press; sendMessage(outMessage); delay(500); LoRa.receive(); // Place LoRa back into Receive Mode } // Button 1 code ends here // Button 2 code starts here sendButton2State = digitalRead(button2Pin); // Get pushbutton state // Send packet if button pressed if (sendButton2State == LOW) { // Compose and send message outMessage = button2Press; sendMessage(outMessage); delay(500); LoRa.receive(); // Place LoRa back into Receive Mode } // Button 2 code ends here }
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@sunnyboy69 I got bored so decided to do some repairs to your code. It now compiles clean, but I doubt the logic is correct, if it is, it's by chance not good design. I see lots of room for improvement. NOTE, look for //RCA for notes to you.
/* LoRa Demo 3 lora-demo3.ino Bi-directional LED control (duplex communications) Requires LoRa Library by Sandeep Mistry - https://github.com/sandeepmistry/arduino-LoRa sendMessage & onReceive functions based upon "LoRaDuplexCallback" code sample by Tom Igoe DroneBot Workshop 2023 https://dronebotworkshop.com */ // Include required libraries #include <SPI.h> #include <LoRa.h> // Define the pins used by the LoRa module const int csPin = 4; // LoRa radio chip select const int resetPin = 2; // LoRa radio reset const int irqPin = 3; // Must be a hardware interrupt pin // LED connection const int ledPin = 5; //RCA //RCA The extensive use of String that is dynamic as opposed to static or at least only on exception //RCA will eventually cause a crash, use a big enough fixded size array of char //RCA // Outgoing message variable String outMessage; // Message counter byte msgCount = 0; // Receive message variables String contents = ""; String button1Press = "button1 pressed"; String button2Press = "button2 pressed"; bool rcvButtonState; //RCA do you need 2 of these??? // Source and destination addresses byte localAddress = 0xBB; // address of this device byte destination = 0xFF; // destination to send to // Pushbutton variables int button1Pin = 8; int button2Pin = 7; //RCA pick a valid pin int sendButton1State; int sendButton2State; void setup() { // Set pushbutton as input pinMode(button1Pin, INPUT_PULLUP); pinMode(button2Pin, INPUT_PULLUP); // Set LED as output pinMode(ledPin, OUTPUT); Serial.begin(9600); while (!Serial) ; Serial.println("LoRa Duplex with callback"); // Setup LoRa module LoRa.setPins(csPin, resetPin, irqPin); // Start LoRa module at local frequency // 433E6 for Asia // 866E6 for Europe // 915E6 for North America if (!LoRa.begin(915E6)) { Serial.println("Starting LoRa failed!"); while (1) ; } // Set Receive Call-back function LoRa.onReceive(onReceive); // Place LoRa in Receive Mode LoRa.receive(); Serial.println("LoRa init succeeded."); } void loop() { // Button 1 code starts here sendButton1State = digitalRead(button1Pin); // Get pushbutton state // Send packet if button pressed if (sendButton1State == LOW) { // Compose and send message outMessage = button1Press; sendMessage(outMessage); } // Button 1 code ends here // Button 2 code starts here sendButton2State = digitalRead(button2Pin); // Get pushbutton state // Send packet if button pressed if (sendButton2State == LOW) { // Compose and send message outMessage = button2Press; sendMessage(outMessage); } // Button 2 code ends here delay(500); LoRa.receive(); // Place LoRa back into Receive Mode } // Send LoRa Packet void sendMessage(String outgoing) { //RCA Did you mean to NOT differentiate between button1 and button2 ??? LoRa.beginPacket(); // start packet LoRa.write(destination); // add destination address LoRa.write(localAddress); // add sender address LoRa.write(msgCount); // add message ID LoRa.write(outgoing.length()); // add payload length LoRa.print(outgoing); // add payload LoRa.endPacket(); // finish packet and send it msgCount++; // increment message ID } // Receive Callback Function void onReceive(int packetSize) { if (packetSize == 0) return; // if there's no packet, return // Read packet header bytes: int recipient = LoRa.read(); // recipient address byte sender = LoRa.read(); // sender address byte incomingMsgId = LoRa.read(); // incoming msg ID byte incomingLength = LoRa.read(); // incoming msg length String incoming = ""; // payload of packet while (LoRa.available()) { // can't use readString() in callback, so incoming += (char)LoRa.read(); // add bytes one by one } if (incomingLength != incoming.length()) { // check length for error Serial.println("error: message length does not match length"); return; // skip rest of function } // If the recipient isn't this device or broadcast, if (recipient != localAddress && recipient != 0xFF) { Serial.println("This message is not for me."); return; // skip rest of function } // If message is for this device, or broadcast, print details: Serial.println("Received from: 0x" + String(sender, HEX)); Serial.println("Sent to: 0x" + String(recipient, HEX)); Serial.println("Message ID: " + String(incomingMsgId)); Serial.println("Message length: " + String(incomingLength)); Serial.println("Message: " + incoming); Serial.println("RSSI: " + String(LoRa.packetRssi())); Serial.println("Snr: " + String(LoRa.packetSnr())); Serial.println(); //RCA Do you need 2 rcvButtonStates??? // Toggle button state if (incoming.equals(button1Press)) { rcvButtonState = !rcvButtonState; } //RCA Only 1 LED? // Drive LED if (rcvButtonState == true) { digitalWrite(ledPin, HIGH); Serial.println("led on"); } else { digitalWrite(ledPin, LOW); Serial.println("led off"); } }
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
I think there's a bit of misattribution going on here.
As I understand this, @sunnyboy69 wants to modify the 3rd example sketch, (the Bi-directional LED control sketch) from Bill's article LoRa – Getting Started with Arduino, ESP32 & Pico. He wants to modify the sketch to add a second LED that is controlled in the same way as the first LED.
The author attribution for the sketch is Bill, who based some of it on code from Tom Igoe. So it's Bill's design and is intended for demonstration purposes; So don't analyze it too closely.
But before making changes, you need to understand what it does and how it works. And that's what Bill video and article provide. So, I'd rewatch the video and reread the article to be sure you understand what the sketch does.
From your sample code, it looks like you're not quite sure how to control multiple buttons. I tried to search for a video Bill did that covered buttons on the Arduino but didn't find a specific one. However, I know he does cover them so I would think it's one of the introductory Arduino videos. I'd look for them.
Also, you mention adding a temperature sensor to the sketch. I wouldn't try to push this demo sketch too far before you refactor it for your needs.
It's been a long time since I've used an Arduino so I'm not sure about this. The code the OP provided defined the push button pins as
//... // Pushbutton variables int button1Pin = 14; int button2Pin = 15; //...
and I don't think the Arduino has those pins. If so, how did this sketch work? What happens? I'm guessing the code
// Get pushbutton 1 state sendButton1State = digitalRead(button1Pin);
produces random results. That would explain the reported behavior.
(Another day I'll argue about String and dynamic memory. A far-off day; very far.)
The one who has the most fun, wins!
Thanks everybody for the replies. Arduino Nano Pin D14 and D15 are analogue and digital input pins.
I stated at my post that I work on a real project within I need 2 inputs and 2 outputs on either side of the LoRa point to point network.
"I got bored so decided to do some repairs to your code. It now compiles clean, but I doubt the logic is correct, if it is, it's by chance not good design. I see lots of room for improvement. NOTE, look for //RCA for notes to you." I do not understand what this means. If one can see "room for improvements", then please!! offer ideas. Thanks.
@sunnyboy69 The room for improvements is the work of a 50 yrs of experience vs a noob. If you don't travel the path yourself you do not benefit.
NOW back to the original problem. What is it you want to do, the version I gave you is an OR, either one button or the other can be activated (or both). Your original code only allowed button2 after button1 was pressed.
Did you read what I wrote? I have added some comments that you can search on as //RCA. The comment will tell you what you need to do. Some of them are critical!
WHAT DO YOU WANT.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Hello Ron, I do appreciate your support. You are experienced, I am not. Thanks a thousand times.
Please give me some time so that I can study your suggested details. Once again, what I want: 2 push buttons on one station (Arduino Nano 3) should independently switch 2 LEDs (relays) on the other station (also Arduino Nano 3). So, each station has 2 push buttons and 2 LEDs (relays).
@sunnyboy69 FYI @tfmccarthy ARGH, I modified Bill's sketch, not the OP. MY VERY BAD!!!!
If the OP would tell us the requirements, we would be better able to comment.
ALSO, please do a Tools/Auto-Format before re-posting your code in <>. That way it will be consistently formatted and we can take a copy to work with to assist you.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Arduino Nano Pin D14 and D15 are analogue and digital input pins.
The pinouts that I have for the Arduino Nano are
image reference: https://robu.in/arduino-pin-configuration/
What pins are you referring to as D14 or D15?
The Arduino pins aren't analog and digital. They're analog or digital. You specify the input or output mode of the pin at runtime., e.g.,
int buttonPin = 8; //... void setup() { // Set pushbutton as input pinMode(button1Pin, INPUT_PULLUP); //... }
@zander wrote
NOTE, look for //RCA for notes to you.
I do not understand what this means.
zander provided his code modifications and has left notes to you in the comments, e.g.,
int button2Pin = 7; //RCA pick a valid pin
His notes have the prefix "RCA" for easy identification.
If one can see "room for improvements", then please!! offer ideas.
"give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime"
- Chinese philosopher Lao Tzu
@zander has provided you with the code that uses a second button; He's given you the fish. I'll make a few comments that highlight the button changes.
The general strategy is for every button variable iin the original sketch for a single button, e.g.,
String buttonPress = "button pressed";
rename the variable with a button index and add a second variable for the second button,
String button1Press = "button1 pressed"; String button2Press = "button2 pressed";
For instructional purposes, I'll strip out all the code except these changes
//... String button1Press = "button1 pressed"; String button2Press = "button2 pressed"; //... // Pushbutton variables int button1Pin = 8; int button2Pin = 7; //RCA pick a valid pin int sendButton1State; int sendButton2State; void setup() { // Set pushbutton as input pinMode(button1Pin, INPUT_PULLUP); pinMode(button2Pin, INPUT_PULLUP); //... } void loop() { // Button 1 code starts here sendButton1State = digitalRead(button1Pin); // Get pushbutton state // Send packet if button pressed if (sendButton1State == LOW) { //... } // Button 2 code starts here sendButton2State = digitalRead(button2Pin); // Get pushbutton state // Send packet if button pressed if (sendButton2State == LOW) { //... } //... } //...
If we look at your loop function code, we have
void loop() { // Get pushbutton 1 state sendButton1State = digitalRead(button1Pin); // Send packet if button 1 pressed if (sendButton1State == LOW) { // ... // Get pushbutton 2 state sendButton2State = digitalRead(button2Pin); // ... } }
This code will only read the second button state after the first button has been pressed. Further, I don't see any code that checks the value of sendButton2State after reading it. This is why I thought you weren't sure about how to control multiple buttons.
Hopefully, this helps you get further on.
The one who has the most fun, wins!
@tfmccarthy Sorry to point this out Tim. I ALWAYS use the Arduino.cc web site for official documents including pin outs. I have most of the pdf files downloaded. Here is a screen shot from there showing pins D14/A0 and D15/A1. D14 and D15 DO indeed exist and as is often the case they are dual use, meaning both can be either Digital or Analogue as the diagram says. You should NOT trust 3rd party pin to diagrams especially when they are right there on the official Arduino web site. I am including the official pdf file from Arduino.cc hardware section.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@tfmccarthy @sunnyboy69 Until the OP tells us what his requirements are, it is impossible to suggest code changes. It could be as simple as if I press either button in any order, or even both at the same time, send a msg saying buttonX was pressed. HOWEVER, if what I want is to send a msg whenever button2 is pressed, BUT only send a msg if button1 was pressed within 372ms of button 2's press either after or before and indicate which plus time delay in the msg. AND literally hundreds of other possibilities (tic)
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@tfmccarthy I understand that it was the fault of the kbd. I often tell it to say one thing and it says another. Are you going to believe your lying eyes, or me is what I always say (under my breath)
Seriously though, be VERY wary of websites ending in .in
Check your messages, I sent one earlier but I want to send another and some guys miss the first msg so at least reply 'got it' to the one there now.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Thanks everybody, Merry Christmas. I will be back later.