I have one integer value which I should pass through Serial1, Serial2, Serial3, and Serial4 after performing some algebraic operation on this integer. For this algebraic operation, I made a function and input arguments is an integer and Serial number. Simple code is given below: (I deleted every extra part of my code to make easy to understand)
String stringG; int d1; int d;
#include<SoftwareSerial.h>
//Software Serial #1: Rx = digital pin 10, Tx = digital pin 11
SoftwareSerial Serial4(10, 11);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Serial4.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
String XX = Serial.readString();
int d1 = XX.toInt();
Serial.println(d1);
int yy;
yy = compareFunction (d1, Serial4);
Serial.println("Now the final values are:");
Serial.println(d1);
Serial.println(yy);
}
}
int compareFunction (int d, String Serialx)
{
int ZZ = Serialx.readString(); // I will receive some values from motor using serial4
//to avoid long code I deleted unnecessary parts
// I will use ZZ and d values and output will be yy
return yy;
}
As you see in compareFunction
I receive value using Serial (it may be 1,2,3 or 4) So I have to pass Serial as an input argument
in this function. If I can't pass Serial
then I have to write 4 functions each for Serial1, 2, 3 and 4 which I want to avoid. Is it possible? How can I do it? when I tried with above code it shows error
exit status 1 could not convert 'Serial4' from 'SoftwareSerial' to 'String'
String
. Did you try to find out what type it actually is?