So I have one integer reading just fine with these two codes:
TX:
/*
AnalogToSerial
Reads an analog input on pin 0, outputs to TX.
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(19,18); // RX, TX
int data [2];
int data1 [4];
int potPin = 0; // analog pin used to connect the potentiometer
int pot0; // variable to read the pot0ue from the analog pin
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(19, INPUT);
pinMode(18, OUTPUT);
}
void loop() {
pot0 = analogRead(potPin); // reads the pot0ue of the potentiometer (pot0ue between 0 and 1023)
data[0] = pot0 & 0xFF; //least significant 8 bit byte
data[1] = (pot0 >> 8); //most significant 2 bits
// Serial.print(data[1], HEX);
// Serial.print(" ");
// Serial.print(data[0], HEX);
// Serial.print(" ");
// Serial.println(data[1]<<8 | data[0]);
mySerial.write(data[0]);
mySerial.write(data[1]); //bytes sent
delay(50); // delay in between reads for stability
}
RX:
/*
Analog Receive
Reads a 10 bit serial stream in two bytes
reassembles and converts to binary display using 8 LEDs
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,18); // RX, TX
int val[4]; // variable to read the value from the analog pin
int serialBytes[2];
int motorVal;
int coolVal; // 0-1024 from serial
int directionPin = 8;
int PWM_out_pin = 9; // works on pins 2 - 13 and 44 - 46
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
// mySerial.begin(9600);
pinMode(10, INPUT);
pinMode(18, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
if (mySerial.available()>1) {
val[0]=mySerial.read(); // least significant 8 bits
val[1]=mySerial.read(); // most significant 2 bits
val[2] = val[1]<<8 | val[0]; // reassebled 10 bit value, sore in val[2]
delay(10);
}
Serial.print(val[1],HEX); Serial.print(" ");
Serial.print(val[0],HEX); Serial.print(" ");
Serial.print(val[2], HEX); Serial.print (" ");
coolVal = val[2];
Serial.println(coolVal);
if (coolVal > 550) {
digitalWrite (directionPin, HIGH);
motorVal = map(coolVal, 550, 1023, 0, 1023);
analogWrite (PWM_out_pin, motorVal / 4); }
if ((coolVal < 550) && (coolVal > 500)) {
analogWrite (directionPin, HIGH);
analogWrite (PWM_out_pin, 0); }
if (coolVal < 500) {
digitalWrite (directionPin, LOW);
motorVal = map(coolVal, 0, 500, 1023, 0);
analogWrite (PWM_out_pin, motorVal / 4); }
}
I tried adding another integer to send over serial the same way, by inserting a data1[0] and data1[1] to the TX and a val1[0] and val1[1] to receive it and it jumbled both of my values. How am I supposed to achieve this?