How to call function on foreign UNO

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
}

why would it ?

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

Gee, thanks for your help. How would you do it?

Hope never dies.

@feraltek in cases like this, you cannot just send along things like that and expect the other side to know what to do.

In fact, by the time the code is running, all those nice names you used have been discarded, as they are no longer necessary.

You must design and exploit a system of communication where a message is sent, and code on the other side interprets that code and does something

  was that an 'A' : ring the bell
  was that a 'B' : spin the cat feeder auger for 0.6 seconds

There are details, see

and do not code any more until you can say that you get the idea.

HTH

a7

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


the magic of computers and AI I guess...

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 -

mySerial.println("ledOn();");

Also thanks for the hint about the two UARTs.

that's one way to go indeed

void loop() {
  switch(mySerial.read()) { // returns -1 if nothing to read
    case 'A': functionA(); break;
    case 'B': functionB(); break;
    case 'C': functionC(); break;
    case 'D': functionD(); break;
    default: break;
  }
}

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.

well - the method has merit. you actually send something to the other arduino

expecting it to trigger the function is wishful thinking and shows indeed poor understanding of what C++ does :slight_smile:

It's

    case 'B': feedTheCat(600); break;

'K?

a7

of course, but the parameter should be 42 not 600

1 Like

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.

Thanks. UKHeliBob's reply had all the clues I need.

1 Like

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.

it grows on you :wink: give it time.