Print2digits error

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);
    }
}

Hi,
Before you started to add the adjust edit, did the code compile and run correctly?

Thanks.. Tom.. :slight_smile:

Try autoformat on the code. You will see that you are missing a curly bracket. if you put every curly bracket on its own line, mismatches are easier to spot.

It has run before, with the adjust edits in there. What I changed that made it not run successfully is I updated the code with new library functions as before I was using this code

#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 Sw0PW = 1; // pins to power LEDs on switches
int Sw1PW = 2;
int Sw2PW = 3;

int hrPins[] = {30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52};
int minPins[] = {31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53};

void setup()
{
  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

  // Why on earth are you using digital pins to power LEDs that are always on? Just wire them directly to 5V already!
  digitalWrite(Sw0PW, HIGH);
  digitalWrite(Sw1PW, HIGH);
  digitalWrite(Sw2PW, HIGH);

  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop()
{
  tmElements_t tm;

  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 %= 12; // Take the time modulo 12
    for(int i = 0; i < 11; i++)
        digitalWrite(hrPins[i], tm.Hour > i); // LED i turns on whenever the hour is at least i+1
    digitalWrite(hrPins[11], tm.Hour == 0); // The last LED behaves differently from the others for some reason

    tm.Minute /= 5; // We only care about the time in 15-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
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}




// toggle the state on a pin
void toggle(int pinNum) 
{  
    int pinState = digitalRead(pinNum);
    pinState = !pinState;
    digitalWrite(pinNum, pinState); 
}

void set_time() {
    byte minutes = 0;
    byte hours = 0;

    while (!digitalRead(Sw0))  // set time switch must be released to exit
    {
        while (!digitalRead(Sw1)) // set minutes
        { 
            minutes++;          
            if ((minutes & 0x0f) > 9) minutes = minutes + 6;
            if (minutes > 0x59) minutes = 0;
            Serial.print("Minutes = ");
            if (minutes >= 9) Serial.print("0");
            Serial.println(minutes, HEX);

            delay(750);
        }

        while (!digitalRead(Sw2)) // set hours
        { 
            hours++;          
            if ((hours & 0x0f) > 9) hours =  hours + 6;
            if (hours > 0x23) hours = 0;
            Serial.print("Hours = ");
            if (hours <= 9) Serial.print("0");
            Serial.println(hours, HEX);

            delay(750);
        }

        // Note: you should use some function/library for this to make the code cleaner
        Wire.beginTransmission(0x68); // activate DS1307
        Wire.write(0); // where to begin
        Wire.write(0x00);          //seconds
        Wire.write(minutes);          //minutes
        Wire.write(0x80 | hours);    //hours (24hr time)
        Wire.write(0x06);  // Day 01-07
        Wire.write(0x16);  // Date 0-31
        Wire.write(0x11);  // month 0-12
        Wire.write(0x13);  // Year 00-99
        Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. 
        Wire.endTransmission();
    }    
}

and I needed to update sections where I was using a Wire.beginTransmission into using an actual RTC library

Your void print2digits is probably just stuck inside of some other function on account of curly bracket misuse.

Don't know if it's relevant to your setup, but I had to also

#include <TimeLib.h>

to the RTC examples included in the IDE for my RTC to work

I also needed to include TimeLib.h, however it was for the declaration of tmElements_t tm;

I still cant use print2digits.

Why not using Serial.print(tm.XXX)? This works for me.

I still cant use print2digits.

I can't juggle horses, either. I'm sure that it will be a lot easier for you to guess why I can't juggle horses than for me to guess why you can't use print2digits.

Why not using Serial.print(tm.XXX)? This works for me.

If you wish, for some reason, to print the hour as 2 digits, when it is 2:00 AM, then that will NOT work, because print() does not print leading 0s.