When I connect a piezo (to pin 8), it causes the output of my RTC to go haywire and throw 165
errors into the results.
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 165:37:4 // Connected piezo
2016/3/25 13:36:4
2165/165/45 13:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2165/165/45 13:36:4
2016/3/25 13:36:4
2165/165/45 13:36:4
2165/165/165 165:165:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 37:36:4
2016/3/25 13:36:4
2165/165/27 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4
Here's my current layout...
Worth noting:
- Disconnecting the piezo instantly solves the problem.
- Connecting to any other pins also caused the issue.
Here is the Fritzing diagram
and schematic
Here's the full code...
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
#define PIN 6
#define TONE 8
const int buttonPin = 7;
long fadeTime = 1 * 60000L; // X minutes
int colorStops = 256;
int delaySpeed = fadeTime / colorStops;
int notes[] = {262,294,330,349};
int switchState = 1; // variable for reading the pushbutton status
int timeHour = 13;
int timeMinute = 20;
uint32_t alarmLength = 5 * 60000L; // 5 minutes
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_RGB);
void setup() {
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input
strip.begin();
strip.show();
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
if (now.hour() == timeHour && now.minute() == timeMinute && now.second() == 00){
// Fade in light
for(int i = 1; i<colorStops; i++){
if (kill() == true){ break; }
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(i,i,0));
}
strip.show();
delay(delaySpeed);
}
// Play tone after light fully bright
for( uint32_t tStart = millis(); (millis()-tStart) < alarmLength; ){
if (kill() == true){ break; }
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(random(100,255),random(100,255),random(100,255)));
}
strip.show();
tone(TONE, notes[random(0,3)]);
delay(100);
}
noTone(TONE);
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(0,0,0));
}
strip.show();
}
}
bool kill() {
if (digitalRead(buttonPin) == LOW) {
noTone(TONE);
for(int np = 0;np<strip.numPixels(); np++){
strip.setPixelColor(np, strip.Color(0,0,0));
}
strip.show();
return true;
}
else {
return false;
}
}