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;
//}
}
}
T, you just assume that it will be the first character