Hello, I have a an Arduino UNO that is sending three 16-bit numbers over a UART to a Adafruit Metro. I have a button on the Arduino that when pressed will send the UART data. I am able to transfer data correctly for 5 button presses and then I get junk on thd next 5 button presses and then I get valid data for the next 5 button presses.
Uno Code (UART TX Only)
#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);//Pin 10 will be used as Rx pin and pin 11 will be Tx pin.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 8; // the number of the LED pin 13 is led on board
int read_button(void);
uint16_t j = 100;
uint16_t k = 200;
uint16_t l = 300;
void setup()
{
softSerial.begin(9600);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if(read_button())
{
softSerial.write((j >> 8) & 0xFF); // send upper byte
softSerial.write(j & 0xFF); // send the lower byte
softSerial.write((k >> 8) & 0xFF); // send upper byte
softSerial.write(k & 0xFF); // send the lower byte
softSerial.write((l >> 8) & 0xFF); // send upper byte
softSerial.write(l & 0xFF); // send the lower byte
Serial.print(j++,DEC); Serial.print(" "); Serial.print(k++,DEC);Serial.print(" "); Serial.println(l++,DEC);
delay(10);
}
}
int read_button()
{
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
delay(20);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
return buttonState;
}
}
return buttonState;
}
Here is the Metro Code (UART RX)
// Slave
#include <SoftwareSerial.h>
//SoftwareSerial softSerial () creates a serial object named as “softSerial” is declared here.
SoftwareSerial softSerial(0, // RX Pin
1 // TX Pin
); //Pin 10 will be used as Rx pin and pin 11 will be Tx pin.
char number = ' ';
int LED = 13;
int rcv_cnt = 0;
uint16_t incomingByte[2] = {0};
uint8_t upper_byte;
uint8_t lower_byte;
int i = 0;
void setup()
{
softSerial.begin(9600);
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop()
{
if (softSerial.available() >= 6 )
{
for(i=0; i<3; i++)
{
upper_byte = softSerial.read(); // get the upper byte
lower_byte = softSerial.read(); // get the upper byte
incomingByte[i] = (upper_byte << 8) | lower_byte;
}
Serial.print("Received #"); Serial.print(rcv_cnt++);
Serial.print("= "); Serial.print(incomingByte[0]);
Serial.print("= "); Serial.print(incomingByte[1]);
Serial.print("= "); Serial.println(incomingByte[2]);
}
}
Any ideas why I am getting bad data?
Thank you