Trying unsuccessfully to call a function on one UNO via SoftSerial from another UNO.
Running SoftSerial on both an Arduino UNO R3 and UNO R4 Wifi. I want to call a function on the R3 from the R4.
Simple test function on the R3
void ledOn() {
digitalWrite(13, HIGH); // code to be executed
}
On the R4 -
mySerial.println("ledOn(););
It doesn't turn on the led on the R3.
Sketch on R4 UNO. The sketch on the R3 Uno is the same except change "R4 Wifi" to "R3".
/*
Software serial test
Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.
The circuit:
* RX is digital pin 2 (connect to TX of other device)
* TX is digital pin 3 (connect to RX of other device)
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // Rx, Tx
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.println("SoftSerial on UNO R4 Wifi");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello from UNO R4 Wifi");
mySerial.println("ledOn();");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
void ledOn() {
digitalWrite(13, HIGH); // code to be executed
}
Start by not using SoftwareSerial on the R4. You don't need to because it has 2 hardware UARTs and you can use Serial1 on pins 0 and 1 unlike the classic Uno
As to your sketch as posted, where in the sketches do you actually call the ledOn() function ? It is all very well receiving a message, which I assume you see printed, but then you ignore what has been received
Try sending just a single character such as an 'L' from one board to the other. On the receiver, read the character into a char variable and test whether it is an 'L'. If it is then explicitly call the ledOn() function
I would capture the incoming command and then do a string (or character if it's a one character command) comparison to see what function needs to be called and call the function
Thanks. I get the idea, like sending a flag for an if..then routine. For the final project that will have a few functions I can test characters in a case structure to determine which to run.
My failed attempt is the final line of the Setup -
This method to call function on one board from another is absolutely nonsense regarding the C++ language.
The name of the function, like ledOn() - exists in source code only and stripped from the binary firmware after compilation. On the machine code all functions are just a memory addresses and can't be called by its sourcecode names.
On a couple of embedded microprocessors running Forth, sending parameters and a function name can directly run that function on the second board. I hoped it would be similar on these UNOs. Now I know it isn't, and UKHeliBob's reply gave me all the clues I need to implement what I want to do.
Forth is typically executed through an interpreter that processes Forth source code interactively. When you enter a Forth command, the interpreter immediately executes it.
That's not how C++ works (fully compiled - you need to code what you want the program to do)
I'm an infant in C++ and don't really like it. Been using Forth since the mid 1980s, but not much for the last dozen years or so. I've got some chips with FlashForth for the UNO R3, and have developed routines for I/O, but haven't yet for comms.