Deleting Zero digit

Greetings from Birmingham
I'm hoping for a little help . .. the code is a snippet of a clock I built for my daughter in law 5 years ago .. It was zapped by a Thunderstorm which took out various bits which I've replaced and all works OK again.
However, she has asked that the time should only show the 'time' .. that is 5.30am is shown a 5:30, not 05:30.
I've had various attempts but I can't figure it .. .. .
Any pearly wisdom around please ??

Thanks

MM

void displayTime() {

  if (time_str[0] = 0 ) {

    lc.setChar(1, 0, "  " , true);
  }
    else
    lc.setChar(1, 0, (time_str[0]), true);
  
  lc.setChar(1, 1, (time_str[1]), true);
  lc.setChar(1, 2, (time_str[3]), true);
  lc.setChar(1, 3, (time_str[4]), true);

  lc.setChar(0, 0, (date_str[0]), true);
  lc.setChar(0, 1, (date_str[1]), true);
  lc.setLed(0, 2, 7, true);
  lc.setChar(0, 3, (date_str[3]), true);
  lc.setChar(0, 4, (date_str[4]), true);
  lc.setLed(0, 5, 7, true);
  lc.setChar(0, 6, (date_str[8]), true);
  lc.setChar(0, 7, (date_str[9]), true);

  return;
}
 if (time_str[0] = 0 )

That sets time_str[0] to zero rather than comparing it to zero

= for assignment of a value
== for comparison of two values

1 Like

Hi Bob

Thanks for such a prompt response .. .. . I've tried the = sign both ways, I do understand the difference, but it makes no change so I've made a fundamental mistake elsewhere.

MM

Could you show a full code?

I'd bet you intend something like:

  if (time_str[0] == '0' ) {

because '0' is a character you are looking to replace and 0 is a null.

1 Like

seems that time_str is a String and comparing with integer will not work, at leas how so think, it must be char '0' or integer/byte 48

We can assume anything, it would be more correct for OP to post the code.

1 Like

I’m away from my bench now, I’ll try and send code later this evening
MM

Well I don't understand how viewing the full code is going to help any but .. ..



/* Clock Main Time Frame 3 x Max 7219 & 7 Segments
   August 2021
*/


#include "LedControl.h"
LedControl lc = LedControl(12, 11, 10, 3);

//const int clock = 10;
//const int latch = 11;
//const int data = 12;

#include <DS3231.h>
#include <RTClib.h>

DS3231 rtc(SDA, SCL);

#include <Wire.h>
#include <SPI.h>


int getKey(void);
bool cond = true;
bool timeDirty = false;
Time t;

String time_str = "";
String prev_time_str = "";
String hr_1_str = "";
String hr_2_str = "";
String min_1_str = "";
String min_2_str = "";
String sec_1_str = "";
int hr_1 = 0;
int hr_2 = 0;
int min_1 = 0;
int min_2 = 0;
int sec_1 = 0;

String date_str = "";
String prev_date_str = "";
String day1_str = "";
String day2_str = "";
String mon1_str = "";
String mon2_str = "";
String year1_str = "";
String year2_str = "";

int day_1 = 0;
int day_2 = 0;
int mon_1 = 0;
int mon_2 = 0;
int year_1 = 0;
int year_2 = 0;

unsigned long delaytime = 10;

int NH = 0;
int NM = 0;
int NH1 = 0;
int NM1 = 0;
int ND = 0;
int NY = 0;
int ND1 = 0;
int NY1 = 0;

int ledSecs = 0, oldledSecs = 0;

void setup() {

  // Serial.begin(9600);

  //************************************ RTC **********************

  Serial.begin(115200);
  rtc.begin();

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
 
  pinMode(12, OUTPUT);  

  //  rtc.setDOW(TUESDAY);      // Set Day-of-Week to SUNDAY
   // rtc.setTime(19, 45, 0);    // Set the time to 12:00:00 (24hr format)
  //  rtc.setDate(16,7, 2024);  // Set the date to January 1st, 2014

  //we have already set the number of devices when we created the LedControl
  int devices = lc.getDeviceCount();
  //we have to init all devices in a loop
  for (int address = 0; address < devices; address++) {

    lc.shutdown(address, false);
    lc.setIntensity(address, 8);
    lc.clearDisplay(address);
  }
  t = rtc.getTime();
  Serial.println("Started");
}

void loop() {

  //read the number cascaded devices
  //int devices = lc.getDeviceCount(); //<-- you aren't using this
  t = rtc.getTime();  // Time structure holds current seconds
  date_str = rtc.getDateStr();
  time_str = rtc.getTimeStr();
  ledSecs = t.sec;

  if (ledSecs != oldledSecs) {       // only execute on change
    if (ledSecs == 0) cond = !cond;  // LED on or off only when seconds are 0
    oldledSecs = ledSecs;            // ready for next change
    int b = ledSecs / 8;             // matrix row..  8 leds per row
    int c = ledSecs - (b * 8);       // matrix column..  remainder is column
   
    lc.setLed(2, b, c, cond);
    displayTime();  // Once every second
    Serial.print(time_str);
    Serial.print("\n\r");
  }
  int keypress = getKey();
  if (keypress == 4)
    adjustTime();
  if (keypress == 8)
    adjustDate();

}  //   <<<<<<<<<<<< the END of Loop();

void displayTime() {

  if (time_str[0] == 0 ) {

    lc.setChar(1, 0, "  " , true);
  }
    else 
    lc.setChar(1, 0, (time_str[0]), true);
  
  lc.setChar(1, 1, (time_str[1]), true);
  lc.setChar(1, 2, (time_str[3]), true);
  lc.setChar(1, 3, (time_str[4]), true);

  lc.setChar(0, 0, (date_str[0]), true);
  lc.setChar(0, 1, (date_str[1]), true);
  lc.setLed(0, 2, 7, true);
  lc.setChar(0, 3, (date_str[3]), true);
  lc.setChar(0, 4, (date_str[4]), true);
  lc.setLed(0, 5, 7, true);
  lc.setChar(0, 6, (date_str[8]), true);
  lc.setChar(0, 7, (date_str[9]), true);

  return;
}


void adjustTime(void) {
  // hr_1 = NH;
  // min_1 = NM;
  int keypress = 0;
  while (getKey() == 4)
    ;
  delay(250);
  while (keypress != 4) {  // this can be modified and you can double this function
    keypress = getKey();
    if (keypress == 1) NH++;
    if (keypress == 2) NM++;
    if (NM > 9) {
      NM1++;
      if (NM1 > 5) NM1 = 0;
      NM = 0;
    }
    if (NH > 9) {
      NH1++;
      if (NH1 > 5) NH1 = 0;
      NH = 0;
    }
    time_str[4] = NM;
    time_str[1] = NH;
    time_str[3] = NM1;
    time_str[0] = NH1;
    displayTime();
    timeDirty = true;
  }
  NM = NM1 * 10 + NM;
  NH = NH1 * 10 + NH;
  rtc.setTime(NH, NM, 0);
  delay(250);
  return;
}

void adjustDate(void) {
  // hr_1 = NH;
  // min_1 = NM;
  ND = NM = NY = 1;
  int keypress = 0;
  while (getKey() == 8)
    ;
  delay(250);
  while (keypress != 8) {  // this can be modified and you can double this function
    keypress = getKey();
    if (keypress == 1) ND++;
    if (keypress == 2) NM++;
    if (keypress == 16) NY++;

    if (ND > 9) {
      ND1++;
      if (ND1 > 3) ND1 = 0;
      ND = 0;
    }
    if (NM > 9) {
      NM1++;
      if (NM1 > 1) NM1 = 0;
      NM = 0;
    }
    if (NY > 9) {
      NY1++;
      if (NY1 > 3) NY1 = 0;
      NY = 0;
    }
    date_str[1] = ND;
    date_str[0] = ND1;
    date_str[4] = NM;
    date_str[3] = NM1;
    date_str[9] = NY;
    date_str[8] = NY1;
    displayTime();
  }
  ND = ND1 * 10 + ND;
  NM = NM1 * 10 + NM;
  NY = 2000 + (NY1 * 10 + NY);
  rtc.setDate(ND, NM, NY);
  delay(250);
  return;
}
int getKey() {

  int retkey = 0;
  if (digitalRead(2) == LOW) retkey += 1;
  if (digitalRead(3) == LOW) retkey += 2;
  if (digitalRead(4) == LOW) retkey += 2;
  if (digitalRead(6) == LOW) retkey += 4;
  if (digitalRead(7) == LOW) retkey += 8;
  if (digitalRead(5) == LOW) retkey += 16;
  if (retkey) delay(500);
  return retkey;
}

MM

It provides an example that could possibly compile, while also identifying the libraries used, and also the data types of things like time_str[]

Still these look wrong:

I think you want to compare with the character '0', not the number 0, and I'm not at all sure what lc.setChar would do when given a c-string rather than a character.

 * Params:
 *   addr  address of the display
 *   digit the position of the character on the display (0..7)
 *   value the character to be displayed.
 *   dp    sets the decimal point.  
*/ 
void setChar(int addr, int digit, char value, boolean dp);
/* Clock Main Time Frame 3 x Max 7219 & 7 Segments
   August 2021
*/

#include <Wire.h>
#include <SPI.h>
#include "RTClib.h" //https://github.com/adafruit/RTClib
#include "LedControl.h"
RTC_DS3231 rtc;
LedControl lc(12, 11, 10, 3);//data,latch,clock,modules

void setup() {
  Serial.begin(115200);
  rtc.begin();

PORTD|=B11111100;

  pinMode(12, OUTPUT);

  //we have already set the number of devices when we created the LedControl
  int devices = lc.getDeviceCount();

  //we have to init all devices in a loop
  for (int address = 0; address < devices; address++) {
    lc.shutdown(address, false);
    lc.setIntensity(address, 8);
    lc.clearDisplay(address);
  }
  Serial.println("Started");
  lc.setLed(0, 2, 7, true);
  lc.setLed(0, 5, 7, true);
}

void loop() {
  static byte oldledSecs = 0;

  DateTime now = rtc.now();

  if (now.seconds() != oldledSecs) {       // only execute on change
    oldledSecs = now.seconds();            // ready for next change

    if (now.hour() < 10 )lc.setChar(1, 0, ' ' , true);
    else lc.setDigit(1, 0, now.hour() / 10, true);

    lc.setDigit(1, 1, now.hour() % 10, true);
    lc.setDigit(1, 2, now.minute() / 10, true);
    lc.setDigit(1, 3, now.minute() % 10, true);

    lc.setDigit(0, 0, now.day() / 10, true);
    lc.setDigit(0, 1, now.day() % 10, true);
    lc.setDigit(0, 3, now.month() / 10, true);
    lc.setDigit(0, 4, now.month() % 10, true);
    lc.setDigit(0, 6, now.year() / 10, true);
    lc.setDigit(0, 7, now.year() % 10, true);

    char dataString[30];
    sprintf(dataString, "%02d.%02d.%02d %02d:%02d:%02d\r\n",  now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
    Serial.print(dataString);
  }
  int keypress = getKey();
  if (keypress == 4)    adjustTime();
  if (keypress == 8)    adjustDate();
  delay(20);
}

void adjustTime() {
  int NH = 0;
  int NM = 0;
  int keypress = 0;
  while (getKey() == 4);
  delay(25);
  while (keypress != 4) {
    keypress = getKey();
    if (keypress == 1) {
      NH++;
      lc.setDigit(1, 0, NH / 10, true);
      lc.setDigit(1, 1, NH % 10, true);
    }
    if (keypress == 2) {
      NM++;
      lc.setDigit(1, 2, NM / 10, true);
      lc.setDigit(1, 3, NM % 10, true);
    }
  }
  rtc.setTime(NH, NM, 0);
  return;
}

void adjustDate() {
  int ND = NM = NY = 1;
  int keypress = 0;
  while (getKey() == 8);
  delay(25);
  while (keypress != 8) {
    keypress = getKey();
    if (keypress == 1) {
      ND++;
      lc.setDigit(0, 0, ND / 10, true);
      lc.setDigit(0, 1, ND % 10, true);
    }
    if (keypress == 2) {
      NM++;
      lc.setDigit(0, 3, NM / 10, true);
      lc.setDigit(0, 4, NM % 10, true);
    }
    if (keypress == 16) {
      NY++;
      lc.setDigit(0, 6, NY / 10, true);
      lc.setDigit(0, 7, NY % 10, true);
    }
  }
  NY = 2000 +  NY;
  rtc.setDate(ND, NM, NY);
  return;
}

int getKey() {
  int retkey = 0;
  if (digitalRead(2) == LOW) retkey += 1;
  if (digitalRead(3) == LOW) retkey += 2; // if hold keys on pin2 and pin3 "retkey" will be 4
  if (digitalRead(4) == LOW) retkey += 2;
  if (digitalRead(6) == LOW) retkey += 4;
  if (digitalRead(7) == LOW) retkey += 8;
  if (digitalRead(5) == LOW) retkey += 16;
  if (retkey) delay(500);
  return retkey;
}

have another, better sketch, look down below

1 Like

Good Morning Kolaha

WOW ! Thank you very much for that .. I appreciate the effort.

Unfortunately, the sketch throws up a series of compilation errors which I need to work through. I'm aware that there are two DS3231 libraries around and I think that may be a big part of the problem .. .. .. .

Again, many Thanks for your help

MM

That looks like it has a couple of things wrong with it.
Try changing it to:

  if (time_str[0] == '0' ) {

    lc.setChar(1, 0, ' ' , true);
  }

corrected:

/* Clock Main Time Frame 3 x Max 7219 & 7 Segments
   August 2021
*/

#include <Wire.h>
#include <SPI.h>
#include "RTClib.h" //https://github.com/adafruit/RTClib
#include "LedControl.h"
RTC_DS3231 rtc;
LedControl lc(12, 11, 10, 3);//data,latch,clock,modules

void setup() {
  Serial.begin(115200);
  rtc.begin();

  PORTD |= B11111100;

  pinMode(12, OUTPUT);

  //we have already set the number of devices when we created the LedControl
  int devices = lc.getDeviceCount();

  //we have to init all devices in a loop
  for (int address = 0; address < devices; address++) {
    lc.shutdown(address, false);
    lc.setIntensity(address, 8);
    lc.clearDisplay(address);
  }
  Serial.println("Started");
  lc.setLed(0, 2, 7, true);
  lc.setLed(0, 5, 7, true);
}

void loop() {
  static byte oldledSecs = 0;

  DateTime now = rtc.now();

  if (now.second() != oldledSecs) {       // only execute on change
    oldledSecs = now.second();            // ready for next change

    if (now.hour() < 10 )lc.setChar(1, 0, ' ' , true);
    else lc.setDigit(1, 0, now.hour() / 10, true);

    lc.setDigit(1, 1, now.hour() % 10, true);
    lc.setDigit(1, 2, now.minute() / 10, true);
    lc.setDigit(1, 3, now.minute() % 10, true);

    lc.setDigit(0, 0, now.day() / 10, true);
    lc.setDigit(0, 1, now.day() % 10, true);
    lc.setDigit(0, 3, now.month() / 10, true);
    lc.setDigit(0, 4, now.month() % 10, true);
    lc.setDigit(0, 6, now.year() / 10, true);
    lc.setDigit(0, 7, now.year() % 10, true);

    char dataString[30];
    sprintf(dataString, "%02d.%02d.%02d %02d:%02d:%02d\r\n",  now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
    Serial.print(dataString);
  }
  int keypress = getKey();
  if (keypress == 4)   {
    int NH = now.hour();
    int NM = now.minute();
    int keypress = 0;
    while (getKey() == 4);
    delay(25);
    while (keypress != 4) {
      keypress = getKey();
      if (keypress == 1) {
        NH++;
        lc.setDigit(1, 0, NH / 10, true);
        lc.setDigit(1, 1, NH % 10, true);
      }
      if (keypress == 2) {
        NM++;
        lc.setDigit(1, 2, NM / 10, true);
        lc.setDigit(1, 3, NM % 10, true);
      }
    }
    rtc.adjust(DateTime(now.year(), now.month(), now.day(), NH, NM, 0));
  }
  if (keypress == 8)    {
    byte ND = now.day();
   byte NM = now.month();
   byte NY = now.year();
    byte keypress = 0;
    while (getKey() == 8);
    delay(25);
    while (keypress != 8) {
      keypress = getKey();
      if (keypress == 1) {
        ND++;
        lc.setDigit(0, 0, ND / 10, true);
        lc.setDigit(0, 1, ND % 10, true);
      }
      if (keypress == 2) {
        NM++;
        lc.setDigit(0, 3, NM / 10, true);
        lc.setDigit(0, 4, NM % 10, true);
      }
      if (keypress == 16) {
        NY++;
        lc.setDigit(0, 6, NY / 10, true);
        lc.setDigit(0, 7, NY % 10, true);
      }
    }
    NY = 2000 +  NY;
    rtc.adjust(DateTime(NY, NM, ND, now.hour(), now.minute(), 0));
  }
  delay(20);
}

int getKey() {
  int retkey = 0;
  while (PIND & B11111100); // let buttons loose
  if (digitalRead(2) == LOW) retkey += 1;
  if (digitalRead(3) == LOW) retkey += 2; 
  if (digitalRead(4) == LOW) retkey += 2;
  if (digitalRead(6) == LOW) retkey += 4;
  if (digitalRead(7) == LOW) retkey += 8;
  if (digitalRead(5) == LOW) retkey += 16;
  if (retkey) delay(500);
  return retkey;
}