Hey Guys, could use a second set of eyes to see if I've broken my code. My project is a clock (bar graph style) using a DS3231 RTC & SX1509 LED driver. Arduino is a Nano. This is my first time playing with I2C and all was working well yesterday on the breadboard. LED routines working, able to set time of day with pushbuttons, etc. Coincidence or not, things stopped working while I was "cleaning up" setup code last night.
Additional info:
Voltage levels for hardware were checked and are OK (3.3 & 5V).
Swapped Nano and no change.
Running a library-provided 'time of day' example sketch for the RTC works fine.
Running a library-provided LED flashing routine for the SX1509 works fine.
Code below omits my LED driving and such; just trying to get TOD back over serial.
If I run the code below, I get some garbage, or no response at all in the serial monitor, UNLESS I comment out Wire.begin and the Pin Definitions block. Then time of day reports properly over serial. If either are included back, things break.
Thoughts?
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <RTClib.h>
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include <SparkFunSX1509.h>
#define DS3231_I2C_ADDRESS 0x68
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
SX1509 io;
RTC_DS3231 rtc;
int hour; // holds hour variable
int minn; // holds minute variable
int minn1; // holds truncated 1x minute
int minn2; // holds truncated 10x minute
int sec; // holds seconds
int j; // used as interlock for main loop and time set
int L; // Left switch input
int R; // right switch input
int X = 1; //Stores state of left switch
int Y = 0; //Stores state of right switch
int HrSet; //hr variable for setting routine
int TMSet; // 10x min variable for setting routine
int MSet; // 1x min variable for setting routine
bool bellset = 0; //if toggled to 1, bell will sound on the hour
// Convert Decimal to BCD
byte decToBcd(byte val) {
return ((val / 10 * 16) + (val % 10));
}
// Convert BCD to Decimal
byte bcdToDec(byte val) {
return ((val / 16 * 10) + (val % 16));
}
void setup () {
Wire.begin();
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
// This line sets the RTC with an explicit date & time, for example to set
// YEAR : MONTH : DAY : HOUR : MINUTE : SECOND
rtc.adjust(DateTime(2024, 1, 1, 12, 0, 0));
}
// Set LED clock to 500kHz (2MHz / (2^(3-1)):
io.clock(INTERNAL_CLOCK_2MHZ, 3);
// Pin Definitions Block
io.pinMode(15, ANALOG_OUTPUT); // 1x min breathable
io.pinMode(14, ANALOG_OUTPUT); // 10x min breathable
io.pinMode(13, ANALOG_OUTPUT); // Hrs breathable
io.pinMode(10, ANALOG_OUTPUT); // Time Digit Indicators
io.pinMode(9, ANALOG_OUTPUT);
io.pinMode(8, ANALOG_OUTPUT);
io.pinMode(7, ANALOG_OUTPUT);
io.pinMode(6, ANALOG_OUTPUT);
io.pinMode(5, ANALOG_OUTPUT);
io.pinMode(4, ANALOG_OUTPUT);
io.pinMode(3, ANALOG_OUTPUT);
io.pinMode(2, ANALOG_OUTPUT);
io.pinMode(1, ANALOG_OUTPUT);
pinMode(9, OUTPUT); // BELL
pinMode(5, INPUT_PULLUP); // left button
pinMode(6, INPUT_PULLUP); // right button
io.analogWrite(15, 0); // Startup with all LEDs OFF
io.analogWrite(14, 0);
io.analogWrite(13, 0);
io.analogWrite(10, 0);
io.analogWrite(9, 0);
io.analogWrite(8, 0);
io.analogWrite(7, 0);
io.analogWrite(6, 0);
io.analogWrite(5, 0);
io.analogWrite(4, 0);
io.analogWrite(3, 0);
io.analogWrite(2, 0);
io.analogWrite(1, 0);
// End of Pin Definitions Block
}
void loop () {
DateTime now = rtc.now();
hour = (now.hour());
minn = (now.minute());
sec = (now.second());
if (hour > 12) {hour = hour - 12;}
Serial.print(hour);
Serial.print(':');
if (minn < 10) Serial.print("0");
Serial.print(minn);
Serial.print(':');
if (sec < 10) Serial.print("0");
Serial.print(sec);
Serial.println();
}