Here's the first.
When I go to upload a file (via USB, of course), I often have to upload it several times before the seconds start ticking. Sometimes I`ll see the xmit LED on the arduino ticking, sometimes absolutely nothing.
If I want to run the serial monitor, I have to close it, reupload the file, reopen the serial monitor, and cross my fingers.
I built the first prototype on a battery pack. (refer to weird problem number two, in my next post). I had the arduino plugged in and powered via USB. I would plug in the battery back, unplug the USB, and everything was great.
Until the batteries died. Or I tried to plug it into the wall. If it lost power for even a split second the program would stop. I'd hit the reset button (thinking this would re-run the program) but I could never get the clock to start working again without plugging it into the USB and trying a couple times until the program stuck.
These two things, the power & the reset button, feel like the same problem to me, but I`m not sure.
The code is only 8 megs of the 32 available, so I don't think that's the problem.
I`m not sure if this is a general problem or directly related to my code, so here it is in it's entirety:
Code: Select all
#include <Wire.h>
#include "RTClib.h"
int FaderPin = 6; //To control brightness of LEDs
int latchPin = 7; //Turn on and off shift registers
int clockPin = 8; //Shift register clock
int dataPin = 9; //To send data to shift registers
int potPin = A0; //Read potentiometer
int HourButton = 10;
int MinButton = 11;
byte secLEDs = 0; //Bytes to send second data
byte minLEDs = 0; //Bytes to send min data
byte hourLEDs = 0; //Bytes to send hour data
int Hour1; //Checks for hour button press
int Hour2; //Checks for hour button bounce
int HourCheck; //Reads hour button
int Min1; //Checks for minute button press
int Min2; //Checks for minute button bounce
int MinCheck; //Reads minute button
int s1 = 0;
int s10 = 0;
int m1 = 0;
int m10 = 0;
int hours = 1;
int h1 = 0;
int h10 = 0;
int timecheck = 0;
int AMPM = 0;
int pot = 0;
RTC_DS1307 RTC;
void setup() {
Serial.begin(9600); //Set up serial communication at 9600bps
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__)); // following line sets the RTC to the date & time this sketch was compiled
DateTime now = RTC.now();
timecheck = now.second();
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(HourButton,INPUT );
pinMode(MinButton, INPUT );
pinMode(A2,OUTPUT);
digitalWrite(A2, LOW);
pinMode(A3, OUTPUT);
digitalWrite(A3, HIGH);
HourCheck = digitalRead(HourButton); // read the initial state
MinCheck = digitalRead(MinButton) ; }
void loop(){
DateTime now = RTC.now();
////////// time keeping //////////
if (timecheck != now.second()) {
s1++;
secLEDs=0;
timecheck = now.second();
if (s1 == 10) {
s1=0;
s10++; }
if (s1 <0 || s1>10) {
s1=0; }
if (s10 == 6 ) {
s10 = 0;
m1++; }
if (s10 <0 || s10>6) {
s10 = 0; }
if (m1 == 10) {
m1 = 0;
m10++; }
if (m1 < 0 || m1 > 10) {
m1=0; }
if(m10 == 6) {
m10 = 0;
hours++; }
if(m10<0 || m10>6 ) {
m10=0; }
if(hours>=1 && hours<=9) {
h1 = hours;
h10 = 0;
AMPM=0; }
if(hours>=10 && hours<=11) {
h1 = hours-10;
h10 = 1;
AMPM = 0; }
if(hours==12) {
h1 = hours-10;
h10 = 1;
AMPM = 1; }
if(hours>=13 && hours<=21) {
h1 = hours-12;
h10 = 0;
AMPM = 1; }
if(hours>=22 && hours<=23) {
h1 = hours-22;
h10 = 1;
AMPM = 1; }
if(hours==24) {
h1 = hours-22;
h10=1;
AMPM=0; }
if(hours<1 || hours >24) {
hours=1; }
Serial.print(h10);
Serial.print(h1);
Serial.print(":");
Serial.print(m10);
Serial.print(m1);
Serial.print(":");
Serial.print(s10);
Serial.print(s1);
Serial.print(" ");
if (AMPM==0) {
Serial.println("AM"); }
else {
Serial.println("PM"); }
////////// LED Display //////////
secLEDs = 0;
minLEDs = 0;
hourLEDs = 0;
updateShiftRegister();
if (s1 == 1 || s1 == 3 || s1 == 5 || s1 == 7 || s1 == 9) {
bitSet(secLEDs,1); }
if (s1 == 2 || s1 == 3 || s1 == 6 || s1 == 7) {
bitSet(secLEDs,2); }
if (s1 == 4 || s1 == 5 || s1 == 6 || s1 == 7) {
bitSet(secLEDs,3); }
if (s1 == 8 || s1 == 9) {
bitSet(secLEDs,4); }
if (s10 == 1 || s10 == 3 || s10 == 5) {
bitSet(secLEDs,5); }
if (s10 == 2 || s10 == 3) {
bitSet(secLEDs,6); }
if (s10 == 4 || s10 == 5) {
bitSet(secLEDs,7); }
if (m1 == 1 || m1 == 3 || m1 == 5 || m1 == 7 || m1 == 9) {
bitSet(minLEDs,1); }
if (m1 == 2 || m1 == 3 || m1 == 6 || m1 == 7) {
bitSet(minLEDs,2); }
if (m1 == 4 || m1 == 5 || m1 == 6 || m1 == 7) {
bitSet(minLEDs,3); }
if (m1 == 8 || m1 == 9) {
bitSet(minLEDs,4); }
if (m10 == 1 || m10 == 3 || m10 == 5) {
bitSet(minLEDs,5); }
if (m10 == 2 || m10 == 3) {
bitSet(minLEDs,6); }
if (m10 == 4 || m10 == 5) {
bitSet(minLEDs,7); }
if (h1 == 1 || h1 == 3 || h1 == 5 || h1 == 7 || h1 == 9) {
bitSet(hourLEDs,1); }
if (h1 == 2 || h1 == 3 || h1 == 6 || h1 == 7) {
bitSet(hourLEDs,2); }
if (h1 == 4 || h1 == 5 || h1 == 6 || h1 == 7) {
bitSet(hourLEDs,3); }
if (h1 == 8 || h1 == 9) {
bitSet(hourLEDs,4); }
if (h10 == 1) {
bitSet(hourLEDs,5); }
if (AMPM == 1) {
bitSet(hourLEDs,6); }
updateShiftRegister(); }
////////// LED Fader //////////
pot = 255-analogRead(potPin)/4;
analogWrite(FaderPin, pot);
////////// Hours Button //////////
Hour1 = digitalRead(HourButton); // read input value and store it in val
delay(10);
Hour2 = digitalRead(HourButton);
if (Hour1 == Hour2) {
if (Hour1 != HourCheck) { // the button state has changed!
if (Hour1 == HIGH) { // check if the button is pressed
hours++;
s1=0;
s10=0; } }
HourCheck = Hour1; } // save the new state in our variable
////////// Minutes Button //////////
Min1 = digitalRead(MinButton); // read input value and store it in val
delay(10);
Min2 = digitalRead(MinButton);
if (Min1 == Min2) {
if (Min1 != MinCheck) { // the button state has changed!
if (Min1 == HIGH) { // check if the button is pressed
m1++;
s1=0;
s10=0; } }
MinCheck = Min1; } // save the new state in our variable
}
void updateShiftRegister() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, hourLEDs);
shiftOut(dataPin, clockPin, MSBFIRST, minLEDs);
shiftOut(dataPin, clockPin, MSBFIRST, secLEDs);
digitalWrite(latchPin, HIGH); }
Thanks in advance.