Hey all,
I'm using an Arduino Mega to send sensor data, an integer, to three Arduinos via serial using the Mega's multi serial function.
I wrote the code for the Nano's and ran it using the console in the IDE for testing. When I did this, the OLED the Nano was outputting to was displaying the correct integers but when I tied the Nano's to the serial pins on the Mega and set the Mega to just output a constant integer on all serial ports except for the one the USB is using, the displays on all of the Nano's displayed nothing but their TX lights were flashing. For debugging, I plugged in one of the Nano's into my computer and kept the rest on the power rail and had it output what it was receiving to the console. What it was writing was squares, which immediately lead me to think the baud rate was off. I tried a few baud rates but then realized that wasn't the issue. I also tried adding a 1 second delay to the serial read to allow for it more time to collect the integer I was sending over serial although this wouldn't work, the data needs to be as close to real-time as possible. That also didn't work. Below is the "Sender" code from the Mega, it's just a test script right now, in the future it will be real-time data from a sensor. Also down below is the "Reciever" code or the code running on all three Nanos. Code is sloppy right now but any support is appreciated!
Sender Code
int testval = 0;
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);
}
void loop() {
testval = "200";
Serial1.write(testval);
Serial2.write(testval);
Serial3.write(testval);
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
if (Serial2.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
if (Serial3.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
}
Receiver Code
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// OLED display TWI address
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(-1);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
int value;
int sensordata;
void setup() {
Serial.begin(9600);
// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
// display the header
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(31,2);
display.print("WATER LEVEL");
}
void loop() {
// put your main code here, to run repeatedly:
//Convert Serial Data
if( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
{
value = (value * 10) + (ch - '0'); // yes, accumulate the value
}
else if (ch == 10) // is the character the newline character
{
sensordata = value; // set var to the accumulated value
Serial.println(sensordata);
value = 0; // reset val to 0 ready for the next sequence of digits
}
}
display.setTextSize(4);
display.setTextColor(WHITE, BLACK);
if (sensordata <= 99) {
// display value
display.setCursor(16,20);
display.print(" ");
display.setCursor(33,20);
display.print(sensordata);
display.print("%");
} else if ( 99 < sensordata < 600) {
display.setCursor(28,20);
display.print(" ");
display.setCursor(16,20);
display.print(sensordata);
display.print("%");
}
if (sensordata <= 30) {
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(34,55);
display.print("LOW WATER");
} else {
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(34,55);
display.print(" ");
}
// update display with all of the above graphics
display.display();
}