For the last few weeks I've being working on and off to get a PCF8523 RTC breakout to work with the Trinket 5V. I think I have boiled the problem down to a commutation problem between the Trinket and the RTC or I might be running out of RAM. I am pretty sure that its not my code because I have run the same code on a Sparkfun Redboard(Arduino Uno) and it works perfectly. Here is the code if there's something I haven't spotted that would affect the trinket that isn't affecting the Redboard.
Code: Select all
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "RTClib.h"
#ifdef __AVR__
#include <avr/power.h>
#endif
RTC_PCF8523 rtc;
#define PIN 2 //CHANGE
Adafruit_NeoPixel strip = Adafruit_NeoPixel(21, PIN, NEO_GRB + NEO_KHZ800);
boolean pixelval[21];
int lastHr;
int lastMin;
int lastSec;
SoftwareSerial mySerial(4, 3); // RX, TX
void setup() {
//Serial.begin(57600);
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
if (! rtc.begin()) {
delay(10);
mySerial.println("There was a problem");
while (1);
}
if (! rtc.initialized()) {
// 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));
delay(5);
mySerial.println("Time Set");
}
strip.begin();
strip.setBrightness(50);
strip.show(); // Initialize all pixels to 'off'
mySerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
if (now.hour() != lastHr) {
for (int i = 0; i < 5; i++) {
pixelval[i] = bitRead(now.hour(), i);
lastHr = now.hour();
mySerial.print(lastHr);
}
}
if (now.minute() != lastMin) {
for (int i = 0; i < 8; i++) {
pixelval[i + 13] = bitRead(now.minute(), i);
lastMin = now.minute();
mySerial.print(lastMin);
}
}
if (now.second() != lastSec) {
for (int i = 0; i < 8; i++) {
pixelval[i + 5] = bitRead(now.second(), i);
lastSec = now.second();
mySerial.print(lastSec);
}
}
for (int i = 0; i < 21; i++) {
if (pixelval[i] == true) {
if (i < 5) {
strip.setPixelColor(i, 0, 255, 0);
}
else if (i > 4 && i < 13) {
strip.setPixelColor(i, 0, 0, 255);
}
else if (i > 12 && i < 21) {
strip.setPixelColor(i, 255, 0, 0);
}
}
else {
strip.setPixelColor(i, 0, 0, 0);
}
}
strip.show();
mySerial.print(now.hour(), DEC);
mySerial.print(':');
mySerial.print(now.minute(), DEC);
mySerial.print(':');
mySerial.print(now.second(), DEC);
mySerial.println();
}
Sorry if this is a little confusing, I tried my best to make it as clear as possible.
Thanks,
Ben