I have an arduino mega and an uno,
The mega is running a sketch that sends and receives text strings between itself and the computer over the USB port, so i believe the serial monitor can't be used on the mega whilst USB comms is going on.
I would like to read the strings being sent between the computer and mega.
So i am using the uno to read what is being sent from and to the arduino and computer and show it on the uno's serial monitor screen on the computer.
I connected the uno's RX pin 0 to the mega's TX0 pin 1, and the uno's TX pin 1 to the mega's RX0 pin 0.
With the following sketch on the uno i can read the data that is sent from the mega to the computer, but not the other way around:
String readString;
void setup() {
Serial.begin(115200);
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(3); //slow looping to allow buffer to fill with next character
}
if (readString.length() > 0) {
Serial.println(readString); //so you can see the captured String
readString = "";
}
}
I believe the above code reading just the TX part of the serial data from the mega to the computer?
If so, how do i make it read the RX part from the computer to the mega too?
:
The code i am running on the mega sends and receives text strings between a bus driving simulator on the computer and a real dashboard on a driving rig.
The few comments in the below code are mine, part of the reason i want to read the incoming data is to figure a few things out.
Bus Board Interface 2 code
float varlist[10];
bool prevtriglist[10];
unsigned long buff1;
unsigned long buff2;
float floatroz;
void setup() {
Serial.begin(115200);
Serial.setTimeout(25);
//Lights
pinMode(47, OUTPUT);
pinMode(48, OUTPUT);
pinMode(49, OUTPUT);
pinMode(50, OUTPUT);
pinMode(51, OUTPUT);
pinMode(52, OUTPUT);
pinMode(53, OUTPUT);
//Switches
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
delay(1000);
Serial.write("REFRESH");
readstr();
}
void sendrequest() {
//Serial.print("SL:0");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - buff1 > 2000) {
buff1 = currentMillis;
sendrequest();
}
while (Serial.available() > 0) {
readstr();
}
analogWrite(47, varlist[0] * 255);
analogWrite(48, varlist[1] * 255);
analogWrite(49, varlist[2] * 255);
analogWrite(50, varlist[3] * 255);
analogWrite(51, varlist[4] * 255);
analogWrite(52, varlist[5] * 255);
analogWrite(53, varlist[6] * 255);
if (currentMillis - buff2 > 500) {
buff2 = currentMillis;
sendfloatauto();
}
// Buttons / Switches:
if (prevtriglist[0] != !digitalRead(7)) { // Station brake
prevtriglist[0] = !digitalRead(7);
sendtrig(0, prevtriglist[0]);
}
if (prevtriglist[1] != !digitalRead(8)) { // Door 0
prevtriglist[1] = !digitalRead(8);
sendtrig(1, prevtriglist[1]);
}
if (prevtriglist[2] != !digitalRead(9)) { // Door 1
prevtriglist[2] = !digitalRead(9);
//sendtrig(2,1);
//delay(60);
//sendtrig(2,0);
sendtrig(2, prevtriglist[2]);
}
if (prevtriglist[3] != !digitalRead(10)) { // High Beam
prevtriglist[3] = !digitalRead(10);
sendtrig(3, prevtriglist[3]);
}
if (prevtriglist[4] != !digitalRead(11)) { // Kinderwagen Switch
prevtriglist[4] = !digitalRead(11);
sendtrig(4, prevtriglist[4]);
}
if (prevtriglist[5] != !digitalRead(12)) { // Horn
prevtriglist[5] = !digitalRead(12);
sendtrig(5, prevtriglist[5]);
}
}
void readstr() {
String string1 = Serial.readStringUntil('\n');
if (string1.substring(0, 2) == "LV") {
int varpos = string1.indexOf(":", 3);
int varindex = atoi(string1.substring(3, varpos).c_str());
float varstate = atof(string1.substring(varpos + 1).c_str());
varlist[varindex] = varstate;
}
if (string1.substring(0, 2) == "SV") {
int varpos = string1.indexOf(":", 3);
int varindex = atoi(string1.substring(3, varpos).c_str());
float varstate = atof(string1.substring(varpos + 1).c_str());
if (varindex == 0) {
if (varstate == 1) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
}
}
void sendtrig(int index, float var) {
Serial.print("TV:" + String(index) + ":" + String(var));
}
void sendvar(int index, float var) {
Serial.print("LV:" + String(index) + ":" + String(var));
}
void sendfloatauto() {
float temp = analogRead(A0);
temp = temp / 1024;
temp = min(temp, 0.99);
if (abs(temp - floatroz) > 0.05) {
floatroz = temp;
sendvar(5, temp);
}
}