I suspect that I might be using too many resources, because of the strings, but I am new to Arduino, so not sure where I am going wrong. Any help would be appreciated.
What it should do when complete: "Birthday Reminder"
- Read the SD card for a "birthday" file ("MM-DD.txt" ) that matches "today".
- The file contains the Name of the person and year of birth.
- Flash LED's and display the person's name and age on the OLED.
- The unit will not displayed anything until someone's birthday.
I am using the SD card with a simple format, to easily add new birthday's.
Output:
⸮Initializing SD card...
card initialized.
25
111
opening: 04-25.txt
2020"Test Person"
bYearStr: 20
The sd card file:
Name: MM-DD.txt
Example: 04-25.txt
Contents: Birthyear, Name
Example: 2020"Test Person"
The Sketch
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
RTC_DS3231 rtc;
int age;
int led;
int dly;
String bName;
String bYearStr;
int bYear;
int currentDay;
int tDy;
int tMth;
int tYr;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
delay(3000); // wait for console opening
// Setup Candle pins
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display(); // this command will display all the data which is in buffer
display.setTextColor(WHITE, BLACK);
const int SdCardPin = 10;
Serial.println("Initializing SD card..."); // Verifying SD Card.
// if (!SD.begin(SdCardPin)) {
if (!SD.begin(10)) {
Serial.println("Card failed, or not present"); // don't do anything more:
while (1);
}
Serial.println("card initialized.");
currentDay = 111;
}
void loop() {
age = 0;
DateTime now = rtc.now();
tDy = now.day();
tMth = now.month();
tYr = now.year();
Serial.println(tDy);
Serial.println(currentDay);
if (currentDay != tDy) {
String fileName;
String m = String(tMth);
String d = String(tDy);
if (tMth < 10) {
String mm = m;
m = "0" + mm;
}
if (tDy < 10) {
String dd = d;
d = "0" + dd;
}
fileName = m + "-" + d + ".txt";
//String fileName = "01-01.txt";
Serial.println("opening: " + fileName);
File f = SD.open(fileName);
delay(3000);
if (!f) {
Serial.println("open failed");
} else {
while (f.available()) {
String line = f.readStringUntil('\n');
Serial.println(line);
bYearStr = line.substring(0, 4);
bName = line.substring(4);
delay(1000);
currentDay = tDy;
break;
}
f.close();
}
Serial.print("bYearStr: ");
Serial.println(bYearStr);
Serial.print("bName: ");
Serial.println(bName);
int bYear = bYearStr.toInt();
age = tYr - bYear;
Serial.print("Age: ");
Serial.println(age);
} else { // Start current day equal
display.setTextSize(1);
display.setCursor(38, 10);
display.print("Birthday!");
display.println();
display.setTextSize(1);
display.setCursor(0, 30);
display.print("Name: ");
display.print(bName);
display.setCursor(0, 40);
display.print("Age: ");
display.print(age );
display.println();
led = random(3, 6);
dly = random(80, 250);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dly); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
} // End current day
display.display();
} // End void Loop