Hi everyone.
I've been trying to read data from a joystick and send it to another arduino using serial communication. The range is supposed to be from 0 to 255 but for some reason I only get 1. Please help!
sender code:
uint8_t val[1];
int xpin = A0;
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
val[1] = map(analogRead(xpin), 0, 1023, 0, 255);
Serial.write(val, sizeof(val)); //Write the serial data
delay(100);
}
receiver code:
uint8_t val[1];
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
int readval = Serial.readBytes(val, sizeof(val));
Serial.println(readval);
delay(100);
}
1. You have connected the Arduinos using hardware UART Port (UART Port) which is not recommended as the UART Ports are engaged with PC/IDE/SM.
2. Connect the Arduinos using software UART Port (SUART Port) as per Fig-1.
Figure-1:
3. Connect wiper pint of Pot with A1 instead of A0 as my diagram shows A1. 4. Upload the following sketch in UNO-1 (Sender). (Your one with slight modification.)
#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6);////SRX = 5, STX = 6
uint16_t val;
int xpin = A1;
void setup()
{
// Begin the Serial at 9600 Baud
Serial.begin(9600);
SUART.begin(9600);
}
void loop()
{
val = analogRead(xpin); //val =0 to 0x03FF in hex base = 0 to 1023 in decimal base
SUART.write(highByte(val)); //sending upper byte of the hex value of val
SUART.write(lowByte(val)); //sending lower byte of the hex value of val
delay(1000); //test interval
}
5. Upload the following sketch in UNO-2.
#include<SoftwareSerial.h>
SoftwareSerial SUART(5, 6);////SRX = 5, STX = 6
uint16_t val;
void setup()
{
// Begin the Serial at 9600 Baud
Serial.begin(9600);
SUART.begin(9600);
}
void loop()
{
byte n = SUART.available(); //check that at least1 1 char/byte in buffer
if(n == 2) //buffer contains 2-byte data that has come from Sender
{
byte yH = SUART.read(); //getting upper byte from buffer
byte yL = SUART.read(); //getting lower byte from buffer
val = yH << 8 | yL //forming 16-bit val ; yH00000000 + 00000000yL = yHyL
Serial.println(val, DEC); // shows 0 to 1023 as Pot is rotated at Sender side
}
}
6. Open SM for Receiver at Bd = 9600. 7. Slowly vary Pot at the Sender side. 8. Check that the SM2 shows decimal values from 0 to 1023.
Wow! Thank you so much it worked!
I'm trying to get it to work wirelessly on a xbee module. Would this code work with that?
Also, since I'm probably the world's biggest noob when it comes to this, I'd appreciate it if you could give me a code explanation for this.
Thanks so much!
I'm sorry. What I meant was, would I send the data received through the analog pins to the other uno using serial communication? In the last example we only sent data from one pin. I need to send two pieces of data, each from different analog pins separately.
Your question suggests you should have helped us earlier by answering the question in reply #2, "why is val[] an array?". Because, only now are you answering it. Thus you waited much longer for advice.
Have you practiced the sketch of of post #12 to see how two data items (each 16-bit) were sent from Sender to Receiver using SUART Port? Follow that post #12 to send any number of data items. Besides this approach, there are many approaches to find which do a google search.
No, this is a wrong way. As @UKHeliBob said, we need some packet structure for sending data, else OP will very quickly runs into the inability to distinguish between the first and second value.
and the result will be a complete mess in the receiver
The wrong way ultimately takes to the right point. Let the OP begin with something that works when he has no idea about data transmission.
Let the OP encounter the situation and then will be discovering that -- 1. To send known number of binary coded data, he needs to send first 2/3-byte preamble bytes (the sync pattern). 2. Send the data items. 3. Send the checksum. 4. At the receiver, detect the sync pattern and then begin to collect data from buffer until known number of bytes are received + checksum byte. 5. Check the validity check of the the received frame. 6. Extract individual data item by combining successive two bytes of the received frame.
When I ran the program, the values from the x and y axis of the joystick showed up fine on the SM, but the value from the push button was constantly 1.