Hello, I'm working on a project with two Arduino UNOs connected via the RX and TX (pins 1 and 0). The sender Arduino is supposed to send a message to the receiver. When the receiver Arduino reads the message, it should turn on an LED. However, the LED isn’t turning on. I’m certain it’s not a hardware issue; the LED and wiring are fine, and I know the receiver is getting the correct message because I connected an I2C OLED and printed the message to it. Below is the code for both the sender and receiver—just wondering if I'm missing something or incorrectly reading the message in the reciever.
Sender:
#include "I2Cdev.h"
#include "MPU6050.h"
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
File myFile;
RTC_DS3231 rtc;
MPU6050 accelgyro(0x69); //AD0 Pin gapped
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define OUTPUT_READABLE_ACCELGYRO
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
unsigned long previousMillis = 0;
int previousSecond = -1;
void setup () {
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
Serial.begin(38400);
accelgyro.initialize();
#ifndef ESP8266
while (!Serial);
#endif
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
SD.begin(10);
delay(2000);
myFile = SD.open("data.txt", FILE_WRITE);
if (!myFile) {
Serial.println("Error opening file!");
return;
}
myFile.println("Time(ms), Acceleration X, Y, Z, Gyro X, Y, Z");
myFile.close();
}
void loop () {
bool mpuStatus = accelgyro.testConnection();
bool sdStatus = SD.exists("data.txt");
Serial.println("MPU OK");
DateTime now = rtc.now();
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
unsigned long currentMillis = millis();
if (now.second() != previousSecond) {
previousMillis = currentMillis;
previousSecond = now.second();
}
unsigned long ms = currentMillis - previousMillis;
// Log data to the SD card
myFile = SD.open("data.txt", FILE_WRITE);
if (myFile) {
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(" ");
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print('.');
myFile.print(ms);
myFile.print(", ax:");
myFile.print(ax);
myFile.print(", ay:");
myFile.print(ay);
myFile.print(", az:");
myFile.print(az);
myFile.print(", gx:");
myFile.print(gx);
myFile.print(", gy:");
myFile.print(gy);
myFile.print(", gz:");
myFile.println(gz);
myFile.close();
} else {
//Serial.println("Error opening file!");
}
delay(10);
}
Reciever:
int mpuLedPin = 5; // LED for MPU6050 status
int sdLedPin = 7; // LED for SD card status
void setup() {
Serial.begin(38400);
pinMode(mpuLedPin, OUTPUT);
pinMode(sdLedPin, OUTPUT);
digitalWrite(mpuLedPin, HIGH);
digitalWrite(sdLedPin, HIGH);
delay(1000);
digitalWrite(mpuLedPin, LOW);
digitalWrite(sdLedPin, LOW);
}
void loop() {
if (Serial.available() > 0) {
// Light up SD LED briefly whenever data is received
digitalWrite(sdLedPin, HIGH);
delay(100);
digitalWrite(sdLedPin, LOW);
String message = Serial.readStringUntil('\n');
if (message == "MPU OK") {
digitalWrite(mpuLedPin, HIGH); // Turn on LED if MPU is OK
} else if (message == "MPU FAIL") {
digitalWrite(mpuLedPin, LOW); // Turn off LED if MPU fails
}
//if (message == "SD OK") {
// digitalWrite(sdLedPin, HIGH); // Turn on LED if SD is OK
//} else if (message == "SD FAIL") {
// digitalWrite(sdLedPin, LOW); // Turn off LED if SD fails
//}
}