Hello! I'm writing a code to be used with an LED clock, and I'm having difficulty setting up the code to be able to adjust time. I keep getting an error that "print2digits was not declared in this scope". Can anyone help? My LEDs on the front of the box aren't lighting up anymore either. I'm using three pushbuttons to adjust time.
#include <Wire.h> // specify use of Wire.h library.
#include <Time.h>
#include <DS1307RTC.h>
#define ONE_HZ_SW 8 // one Hz square wave from Ds1307
#define blinkPin 13
#define Sw0 4
#define Sw1 5
#define Sw2 6
int hrPins[] = {52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30};
int minPins[] = {31, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 33};
void setup()
{
tmElements_t tm;
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600);
digitalWrite(blinkPin, 0);
pinMode(ONE_HZ_SW, INPUT_PULLUP);
pinMode(Sw0, INPUT_PULLUP); // for this use a slide switch
pinMode(Sw1, INPUT_PULLUP); // N.O. push button switch
pinMode(Sw2, INPUT_PULLUP); // N.O. push button switch
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}
void loop()
{
tmElements_t tm;
// wait or HIGH
while(!digitalRead(ONE_HZ_SW)) {
}
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
delay(1000);
if (!(digitalRead(Sw0))) set_time(); // hold the switch to set time
while(digitalRead(ONE_HZ_SW)){
}// wait for low
toggle(blinkPin);
}
tm.Hour = (tm.Hour + 11) % 12; // This maps 0 to 11 (all on), 1 to 0 (only first on), 11 to 10 (all but last on), 12 to 11 (all on), 13 to 0 (first on), 23 to 10 (all but last on)
for(int i = 0; i < 12; i++)
digitalWrite(hrPins[i], tm.Hour >= i);
tm.Minute /= 5; // We only care about the time in 5-minute increments
for(int i = 1; i < 12; i++)
digitalWrite(minPins[i], tm.Minute >= i);
digitalWrite(minPins[0], tm.Minute == 0); // The first pin behaves differently: it only turns on during the first 5 minutes
}
// toggle the state on a pin
void toggle(int pinNum)
{
int pinState = digitalRead(pinNum);
pinState = !pinState;
digitalWrite(pinNum, pinState);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
void set_time() {
tmElements_t tm;
tm.Minute = 0;
tm.Hour = 0;
while (!digitalRead(Sw0)) // set time switch must be released to exit
{
while (!digitalRead(Sw1)) // set minutes
{
tm.Minute++;
if ((tm.Minute & 0x0f) > 9) tm.Minute = tm.Minute + 6;
if (tm.Minute > 0x59) tm.Minute = 0;
Serial.print("Minutes = ");
if (tm.Minute >= 9) Serial.print("0");
Serial.println(tm.Minute);
delay(750);
}
while (!digitalRead(Sw2)) // set hours
{
tm.Hour++;
if ((tm.Hour & 0x0f) > 9) tm.Hour = tm.Hour + 6;
if (tm.Hour > 0x23) tm.Hour = 0;
Serial.print("Hours = ");
if (tm.Hour <= 9) Serial.print("0");
Serial.println(tm.Hour);
delay(750);
}
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
delay(1000);
}
}