davekw7x
Thank you for the quick reply. Initially, my sketch wasn’t doing the self-check, by adding “Sd2Card card” it at least performs the self-check and finds the sd card. Still not writing to sd card though. I have used multiple XIAO, XIAO expansion boards, and sd cards to no avail. I’ve attached my code for your review; I’m still fairly new to this so hopefully I have made an obvious (to you) mistake. BTW: I should mention that in spite of my love for this little controller, I find it somewhat flukey and very different than Nano and a couple other controllers, especially when it isn’t mounted on an expansion board. My original sketch was working fine on the Stroud Water Research Mayfly and the Seeeduino Stalker. The project is an ultrasonic-based Water Level Indicator for a stream flooding project my not-for-profit is undertaking.
type or paste code here
#define sensorPin 0
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include <Adafruit_SSD1306.h>
#include <Time.h>
#include "RTClib.h"
Sd2Card card;
//SdVolume volume;
//SdFile root;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define ds3231_I2C_ADDRESS 0x68
#define TIME_MSG_LEN 11
#define TIME_HEADER 'T'
#define sensorPin 0
#define PCF8563address 0x51
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
long distance = 0;
File myFile;
char fileName[] = "WLI_001.txt";
const int chipSelect = 2;
char charRead;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
byte bcdToDec(byte value)
{
return ((value / 16) * 10 + value % 16);
}
byte decToBcd(byte value) {
return (value / 10 * 16 + value % 10);
}
void setPCF8563()
// this sets the time and date to the PCF8563
{
Wire.beginTransmission(PCF8563address);
Wire.write(0x02);
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void readPCF8563()
// this gets the time and date from the PCF8563
{
Wire.beginTransmission(PCF8563address);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(PCF8563address, 7);
second = bcdToDec(Wire.read() & B01111111); // remove VL error bit
minute = bcdToDec(Wire.read() & B01111111); // remove unwanted bits from MSB
hour = bcdToDec(Wire.read() & B00111111);
dayOfMonth = bcdToDec(Wire.read() & B00111111);
dayOfWeek = bcdToDec(Wire.read() & B00000111);
month = bcdToDec(Wire.read() & B00011111); // remove century bit, 1999 is over
year = bcdToDec(Wire.read());
}
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
Wire.begin();
pinMode (2, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("\nInitializing SD card...");
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
while (1);
} else {
Serial.println("Wiring is correct and a card is present.");
}
}
void read_sensor() {
distance = analogRead(sensorPin) * 5;
}
void print_data() {
}
void loop()
{
Serial.print(distance);
Serial.print("mm");
Serial.print('\t');
readPCF8563();
Serial.print(" ");
{ if (month < 10)
Serial.print("0");
}
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/20");
Serial.print(year, DEC);
Serial.print('\t');
{ if (hour < 10)
Serial.print("0");
}
Serial.print(hour, DEC);
Serial.print(":");
if (minute < 10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second < 10)
{
Serial.print("0");
}
Serial.println(second, DEC);
Serial.print('\t');
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
{ if (month < 10)
display.print("0");
}
display.print(month, DEC);
display.print("/");
{ if (dayOfMonth < 10)
display.print("0");
}
display.print(dayOfMonth, DEC);
display.print("/20");
display.print(year, DEC);
display.setTextColor(WHITE);
display.setCursor(0, 18);
{ if (hour < 10)
display.print("0");
}
display.print(hour, DEC);
display.print(":");
{ if (minute < 10)
display.print("0");
}
display.print(minute, DEC);
display.print(":");
{ if (second < 10)
display.print("0");
}
display.print(second, DEC);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 38);
display.print(distance);
display.print("mm");
display.display();
myFile = SD.open("WLI_001.txt", FILE_WRITE);
{ if (month < 10)
myFile.print("0");
}
myFile.print(month, DEC);
myFile.print('/');
{ if (dayOfMonth < 10)
myFile.print("0");
}
myFile.print(dayOfMonth, DEC);
myFile.print('/');
myFile.print(year, DEC);
myFile.print('\t');
{ if (hour < 10)
myFile.print("0");
}
myFile.print(hour, DEC);
myFile.print(':');
{ if (minute < 10)
myFile.print("0");
}
myFile.print(minute, DEC);
myFile.print(':');
{ if (second < 10)
myFile.print("0");
}
myFile.print(second, DEC);
myFile.print('\t');
myFile.print(distance);
myFile.print("mm");
myFile.close();
read_sensor();
print_data();
delay(2000);
}