-1

I wrote code for my transmitter Arduino. I want to send the data to the receiver Arduino through Tx and Rx serial.

The issue is that I could not discard the char "T" and it appears as output value on pin 9 or 10 or 3.

This is the transmitter and receiver code. I only need my receiver code to be modified.

Transmitter code :

char HEADER_BYTE = 'T';  // Character header

#define potPin1 A1  // Potentiometer 1 pin
#define potPin2 A2  // Potentiometer 2 pin
#define potPin3 A4  // Potentiometer 3 pin

void setup() {
  Serial.begin(9600); // Set baud rate for serial communication
  Serial1.begin(9600);
}

void loop() {
  int servo1Angle = map(analogRead(potPin1), 0, 1023, 0, 180); // Map potentiometer reading to servo angle range
  int servo2Angle = map(analogRead(potPin2), 0, 1023, 0, 180); // Map potentiometer reading to servo angle range
  int motorSpeed = map(analogRead(potPin3), 0, 1023, 0, 250); // Map potentiometer reading to motor speed range (adjust based on driver)

  // delay(1);
  Serial1.write(HEADER_BYTE);  // Send header character
  Serial.println(HEADER_BYTE);
  Serial1.write(servo1Angle); // Send servo 1 angle
  Serial.println(servo1Angle);
  Serial1.write(servo2Angle); // Send servo 2 angle
  Serial.println(servo2Angle);
  Serial1.write(motorSpeed);  // Send motor speed
  Serial.println(motorSpeed);

  delay(10);
}

Receiver code:

#include <Servo.h>

Servo myservo1;
Servo myservo2;
int pot_pin = 3;

void setup() {
  Serial.begin(9600);  // Start serial communication
  Serial1.begin(9600);

  pinMode(pot_pin,OUTPUT);
  myservo1.attach(9);
  myservo2.attach(10);
  // myservo.write(90);  // set servo to mid-point
}

void loop() {
  if (Serial1.available() >= 4) {
    if (Serial1.peek() == 'T') {
      Serial1.read();  // Discard the 'T' character
    }
    int incomingByte = Serial1.read();

    // switch (incomingByte) {
    //  case 84:
    int pot1 = Serial1.read();
    int pot2 = Serial1.read();
    int pot3 = Serial1.read();

    myservo1.write(pot1);
    Serial.print(pot1);
    Serial.print("   ");
    myservo2.write(pot2);
    Serial.print(pot2);
    Serial.print("   ");
    analogWrite(pot3,3);
    Serial.print(pot3);
    Serial.println("  ");
    //  break;
    //}
  }
}
1
  • you don't wait for a T, you just assume that it will be the first character
    – jsotola
    Apr 1 at 16:51

1 Answer 1

1

I see two issues here.

First, T is not a very good choice for a header. You are using a binary protocol (because of Serial.write()), sending bytes with numeric values between 0 and 250. The constant 'T' actually means 84 (the ASCII code for the uppercase letter T), and this value can appear within the actual data. If you want to frame your messages with a header byte, I suggest you choose a byte that cannot be part of the body of the message. E.g. 255.

Second, your logic for reading the message is wrong:

if (Serial1.peek() == 'T') {
    Serial1.read();  // Discard the 'T' character
}

You should ask yourself: what would this code do if the byte at the top of the buffer is not 'T'? What should it do instead? I think the right thing to do would be to discard all incoming bytes until it finds the expected header.

int incomingByte = Serial1.read();

Why discard this byte? The header byte has already been discarded (if present). Why discard the following byte? Doing so will reliably mess your communication.

I suggest something along these lines:

void loop() {
  if (Serial1.available() >= 4) {
    if (Serial.read() == HEADER_BYTE) {
      int pot1 = Serial1.read();
      int pot2 = Serial1.read();
      int pot3 = Serial1.read();

      // ...
    }
  }
}

Here, if we expect the header byte and we get anything else, we just discard the byte we got and move to the next loop() iteration. Next time we have four byte in the serial input buffer, we try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.