Hello,
I currently have a project where I am trying to store sensor data sent from an external custom Atmega2560 board to an Arduino pro mini over the serial port. The Openlog is connected to the 5V,GND, A4, and A5 of the Arduino pro mini via a Qwiic connector daisy chain. I have two GPIO extension boards daisy chained before the the Openlog is connected to the Qwiic connector on the second GPIO extension board. The order of this could be the issue I’m not super familiar with Qwiic/I2C. I have attached my code as well. I am trying to append the file dataPB.text and write the string ‘dataLogger’ to this file on the SD card. I have tried lots of debugging and don’t know whats wrong. The receiveEvent and writeToSD are the functions relating to this. Could the issue also have to do with not using an Arduino UNO?
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_PCF8575.h>
#include <SparkFun_Qwiic_OpenLog_Arduino_Library.h>
const String version = "0.0.1";
const int LEDArduinoPins[17] = { -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3 }; //-1 is placeholder for indexing
const int expansionpins[25] = { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
int ledArray[41] = { -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
//----- gpio expansion variables -----
const uint8_t PCF8575_ADDRESS = 0x20;
const int NUM_LEDS = 24;
Adafruit_PCF8575 pcf8575;
OpenLog myLog; //Create instance
const byte OpenLogAddress = 42; //Default Qwiic OpenLog I2C address
//Serial communication variables
char command;
int state;
int blink;
int ledNumber;
//SD Card logger variables
String dataLogger;
//unsigned long startTime = millis(); // Record the start time
int ledPin = LED_BUILTIN; //Status LED connected to digital pin 13
void setup() {
Wire.begin(); //Initialize I2C
myLog.begin(); //Open connection to OpenLog
Serial.begin(9600);
while (!Serial) {}; //wait for com to initialize
// Initialize LED states
for (int i = 1; i <= 16; i++) {
pinMode(LEDArduinoPins[i], OUTPUT);
}
for (int i = 1; i <= 24; i++) {
pinMode(expansionpins[i], OUTPUT);
}
pinMode(ledPin, OUTPUT);
setLEDnum();
}
void setAllLEDs(int state, int blink) {
if (state == 1) {
if (blink == 1) {
for (int i = 1; i <= 40; i++) {
digitalWrite(ledArray[i], HIGH);
}
delay(100);
for (int i = 1; i <= 40; i++)
{
digitalWrite(ledArray[i], LOW);
}
delay(100);
} else if (blink == 0) {
for (int i = 1; i <= 40; i++) {
digitalWrite(ledArray[i], HIGH);
}
}
} else {
for (int i = 1; i <= 40; i++) {
digitalWrite(ledArray[i], LOW);
}
}
}
void setLED(int state, int blink, int ledNumber) {
if (state == 1) {
if (blink == 1) {
//Fix blinking later
//while(millis() - startTime < 15000){
digitalWrite(ledArray[ledNumber], HIGH);
//delay(500);
//digitalWrite(ledArray[ledNumber], LOW);
//delay(500);
//}
} else if (blink == 0) {
digitalWrite(ledArray[ledNumber], HIGH);
}
} else {
digitalWrite(ledArray[ledNumber], LOW);
}
}
void receiveEvent() {
while (Serial.available()) {
command = Serial.read(); // Read the incoming command
if (command == 'A') { // All LEDs command
state = (int)Serial.parseInt();
delay(2);
blink = (int)Serial.parseInt(); // Read the state (true for ON, false for OFF)
delay(2);
setAllLEDs(state, blink);
} else if (command == 'I') { // Individual LED command
state = (int)Serial.parseInt(); // Read the state (true for ON, false for OFF)
delay(2);
blink = (int)Serial.parseInt();
delay(2);
ledNumber = (int)Serial.parseInt(); // Read LED number (1 to 40)
delay(2);
setLED(state, blink, ledNumber);
} else if (command == 'D'){
dataLogger = Serial.readString();
writeToSD();
delay(2);
}
}
}
void writeToSD() {
//Record something to the default log
myLog.append("dataPBI.txt");
myLog.println(dataLogger);
myLog.println("your string is bad"); //Debug
Serial.println(dataLogger);
//Blink the Status LED because we're done!
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
void loop() {
while (Serial.available()) {
receiveEvent();
}
delay(1000);
}