Noob cant figure out char at all

I've been messing with a massive max7219 world clock I wanted to do for at least 3 months now and I'm stumped, as soon as I have more than 4 char's, it will only store like 2 variables. tried an ESP32, a Wemos D1 Mini (ESP8266), and 2 different Arduino Nanos with a ESP01 module. First block is my serial monitor output, second block is my REALLY cut down code omitting literally everything other than ArduinoJson. If anybody could tell me where I'm dumb, that'd make my day, thank you.

dotW: 
dotM: 10-09
tz1:
tz2:
tz3:
tz4:
tz5:
tz6: 13:16
#include <ArduinoJson.h>

char  szTimeL[7];
char dotM[6];
char dotW[4];
char tz1[5];
char tz2[5];
char tz3[5];
char tz4[5];
char tz5[5];
char tz6[5];

int sub3;
int sub4;

long localEpoc = 0;
long localMillisAtUpdate = 0;


// void createHString(char*, char*); // PlatformIO Integration
// void getTime(char*, bool); // PlatformIO Integration

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

}

void getTime(char *psz, bool f = true)
{   
  
  const char* client = "{\"maintime\": \"Sun 10-09 09:16 PM\",\"sub1\": \"Sun\",\"sub2\": \"10-09\",\"sub3\": \"09\",\"sub4\": \"16\",\"sub5\": \"PM\",\"timeL1\": \"21:16\",\"timeL2\": \"23:16\",\"timeL3\": \"00:16\",\"timeR1\": \"04:16\",\"timeR2\": \"12:16\",\"timeR3\": \"13:16\"}";
  DynamicJsonDocument doc(512);


  DeserializationError error = deserializeJson(doc, client);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  sprintf(dotW, doc["sub1"]);
  sprintf(dotM, doc["sub2"]);
  sub3 = doc["sub3"];#include <ArduinoJson.h>

char  szTimeL[7];
char dotM[6];
char dotW[4];
char tz1[5];
char tz2[5];
char tz3[5];
char tz4[5];
char tz5[5];
char tz6[5];

int sub3;
int sub4;

long localEpoc = 0;
long localMillisAtUpdate = 0;


// void createHString(char*, char*); // PlatformIO Integration
// void getTime(char*, bool); // PlatformIO Integration

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

}

void getTime(char *psz, bool f = true)
{   
  
  const char* client = "{\"maintime\": \"Sun 10-09 09:16 PM\",\"sub1\": \"Sun\",\"sub2\": \"10-09\",\"sub3\": \"09\",\"sub4\": \"16\",\"sub5\": \"PM\",\"timeL1\": \"21:16\",\"timeL2\": \"23:16\",\"timeL3\": \"00:16\",\"timeR1\": \"04:16\",\"timeR2\": \"12:16\",\"timeR3\": \"13:16\"}";
  DynamicJsonDocument doc(350 );


  DeserializationError error = deserializeJson(doc, client);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  sprintf(dotW, doc["sub1"]);
  sprintf(dotM, doc["sub2"]);
  sub3 = doc["sub3"];
  sub4 = doc["sub4"];
  sprintf(tz1, doc["timeL1"]);
  sprintf(tz2, doc["timeL2"]);
  sprintf(tz3, doc["timeL3"]);
  sprintf(tz4, doc["timeR1"]);
  sprintf(tz5, doc["timeR2"]);
  sprintf(tz6, doc["timeR3"]);

  localMillisAtUpdate = millis();
  sprintf(psz, "%2d%c%02d", sub3, (f ? ':' : ' '), sub4);

  Serial.print("psz: ");
  Serial.println(psz);
  Serial.print("dotW: ");
  Serial.println(dotW);
  Serial.print("dotM: ");
  Serial.println(dotM);
  Serial.print("tz1: ");
  Serial.println(tz1);
  Serial.print("tz2: ");
  Serial.println(tz2);
  Serial.print("tz3: ");
  Serial.println(tz3);
  Serial.print("tz4: ");
  Serial.println(tz4);
  Serial.print("tz5: ");
  Serial.println(tz5);
  Serial.print("tz6: ");
  Serial.println(tz6);
  
}

void loop()
{
  static uint32_t  lastTime = 0;
  static bool flasher = false;

  if (millis() - lastTime >= 1000)
  {
    lastTime = millis();
    getTime(szTimeL, flasher);
    flasher = !flasher;
  }
}

What happens if instead of:

  char dotW[4];
...
  sprintf(dotW, doc["sub1"]);

you just get the string pointer and use it instead:

const char* dotW;
...
dotW = doc["sub1"];

Later you could strcpy() this into a character buffer if you wanted to modify it.

Hello

Your arrays

char tz1[5];
char tz2[5];
char tz3[5];
char tz4[5];
char tz5[5];
char tz6[5];

are not big enough to store strings like "13:16", which is 6 characters, because a string ends with a null character

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.