im sending the value 1500 from another MCU with
fdserial_txChar(transmit, send >> 7); // send Higher 7 bits
fdserial_txChar(transmit, send & 0x7F); // send Lower 7 bits
and was receiving it with code that looked like this
servo1 = fdserial_rxChar(receive) << 7; // get Higher 7 bits
servo1 += fdserial_rxChar(receive) & 0x7F; // get and add Lower 7 bits
how do i rewrite the receiving code so arduino will compile it?
Thank you.
here is the code im trying to get working
void setup() {
// physical pin 2=TX 3=RX
Serial.begin(9600);
pinMode(3, OUTPUT); // set LED pin as output
digitalWrite(3, LOW); // switch off LED pin
}
void loop() {
if (Serial.available()) {
// char data_rcvd = Serial.read(); // read one byte from serial buffer and save to data_rcvd
int data_rcvd = Serial.read() << 7; //get Higher 7 bits
int data_rcvd += Serial.read() & 0x7F; //get and add Lower 7 bits
if (data_rcvd == 1500) digitalWrite(3, HIGH); // switch LED On
if (data_rcvd == '0') digitalWrite(3, LOW); // switch LED Off
}
// if (digitalRead(8) == HIGH) Serial.write('0'); // send the char '0' to serial if button is not pressed.
// else Serial.write('1'); // send the char '1' to serial if button is pressed.
}
2 Answers 2
the answer to my question of how to write
servo1 = fdserial_rxChar(receive) << 7; // get Higher 7 bits
servo1 += fdserial_rxChar(receive) & 0x7F; // get and add Lower 7 bits
so Arduino would compile it is....
combined = (val1 << 7) | (val2);
You have a few obvious problems here.
if (Serial.available()) { int data_rcvd = Serial.read() << 7; //get Higher 7 bits int data_rcvd += Serial.read() & 0x7F; //get and add Lower 7 bits
You are testing that one byte is available on the serial port and then reading two bytes. Too soon. Test for two bytes if you are planning to read two.
if (data_rcvd == '0') digitalWrite(3, LOW); // switch LED Off
Why the quotes? Why not: if (data_rcvd == 0)
?
How are you intending to synchronize the sending and receiving?
Your sender is sending HLHLHLHL
- what if the receiver starts while the low byte is being sent? You will receive LHLHLH
which means your received data will be meaningless.
You are better off sending "readable" data, like "1500" rather than 1500, terminating with a newline, and structuring your receiving code to read until it hits a newline. That will synchronize the receiver (after the first reading, anyway).
For more tips about reading serial data see: How does serial communications work on the Arduino?
Also my post about serial data on my forum has code for buffering incoming serial data and waiting for a newline.
-
thanks for your Help Nick, you steered me towards finding my answer. is there a way to get arduino to pause/wait until it receives a specific value such as 129/0x81 ? kinda like a way to sync things up?benjamin nikkel– benjamin nikkel2023年03月14日 19:23:05 +00:00Commented Mar 14, 2023 at 19:23
-
@benjaminnikkel The simplest solution is what I suggested in my answer. Send a "line" of data including a newline at the end. The newline will separate each reading from the next. Instead of transmitting the low-order and high-order bytes, just do a
sprintf
into a buffer, turning the number into ASCII characters, and add a newline to the end. The Arduino can read a line of data. For tips see my post about reading serial data. However the link on my own forum has better information about reading lines of data.2023年03月15日 04:06:52 +00:00Commented Mar 15, 2023 at 4:06
fdserial_rxChar()
or similar. So what is your actual problem? You eon't get good answers if you don't ask an understandable and complete questionim sending the value 1500
... what does that mean? ... are you sending a integer? ... are you sending text? ... something other?