Problem with getting value for global variable

Hi, I have an Arduino Nano, three Max7219 8x32 dot matrix displays, a DS18B20 sensor, DS1307 RTC. I divided the clock board into 5 zones for display. Look at the attached image.

I have a problem with getting value for szTempP variable (plus or minus sign depending on temperature).

My global variables:
szTimeL - time /hh:mm
szTempC - temperature value before decimal point
szTempP - plus or minus sign depending on temperature
szTempS - temperature value after the decimal point

Example:
There is a temperature of +30.5 Celsius.

It should turn out like this:
szTempC="30"
szTempS="5"
szTempP="+"

But it turns out like this:
szTempC="30"
szTempS="5"
szTempP="+5"

The szTempP variable is passed not a single "+" sign, but the value "+5".
What is wrong with my code, can you help me please?

My code:

// Use the DS1307 clock module
#define USE_DS1307 1

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Wire.h>
#include <MD_DS1307.h>
#include "Font_Data.h"
#include <microDS18B20.h>

MicroDS18B20<2> sensor; 

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW

#define MAX_ZONES 3 //
#define ZONE_SIZE 4 // 

#define MAX_DEVICES (MAX_ZONES * ZONE_SIZE) //

#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10


MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
#define SPEED_TIME  75
#define PAUSE_TIME  0

#define MAX_MESG  6
#define  DEBUG  0

// Global variables
char  szTimeL[MAX_MESG];    // hh:mm\0
char  szTempC[MAX_MESG];
char  szTempP[1];
char  szTempS[1];

void getTime(char *psz, bool f = true)

{

  RTC.readTime();
  sprintf(psz, "%02d%c%02d", RTC.h, (f ? ':' : ' '), RTC.m);

}

void getTemperature(char *psz, bool f = true)
{
  float temperature = 0;
  int t = 0;
  
  if (sensor.readTemp()) temperature = sensor.getTemp();
  else Serial.println("error");
 
  sensor.requestTemp();

  t = (abs(temperature) - (byte)abs(temperature)+0.01)*10; //get 1 character after the decimal point

  if (temperature > 0 ) {
  sprintf(szTempP, "+"); 
  } 
  else {  
  sprintf(szTempP, "-"); 
  }
  
  sprintf(szTempS, "%1d", t);
  sprintf(psz, "%2d", (byte)abs(temperature));
 
}

void setup(void)
{
  P.begin(MAX_ZONES);
  P.setIntensity(0);

  P.setZone(0, 0, 3);
  P.setZone(1, 4, 6);
  P.setZone(2, 7, 7);
  P.setZone(3, 8, 8);
  P.setZone(4, 9, 11);

  P.setFont(0, clockFont2); 
  P.setFont(1, BigFontLower); 
  P.setFont(2, clockFont2); 
  P.setFont(3, clockFont2); 
  P.setFont(4, BigFontUpper); 
   
  P.setCharSpacing(P.getCharSpacing() * 1); 

  P.setZoneEffect(1, true, PA_FLIP_UD);
  P.setZoneEffect(1, true, PA_FLIP_LR);
  P.setZoneEffect(2, true, PA_FLIP_UD);
  P.setZoneEffect(2, true, PA_FLIP_LR);

  P.displayZoneText(0, szTimeL, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(1, szTempC, PA_LEFT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(2, szTempS, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(3, szTempP, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(4, szTempC, PA_RIGHT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);

  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);

  getTime(szTimeL);
}

void loop(void)
{
  static uint32_t  lastTime = 0; // millis() memory
  static bool flasher = false;  // seconds passing flasher

  P.displayAnimate();
  P.setIntensity(0);
  if (P.getZoneStatus(0) && P.getZoneStatus(1) && P.getZoneStatus(2) && P.getZoneStatus(3) && P.getZoneStatus(4))
  {
    // Adjust the time string if we have to. It will be adjusted
    // every second at least for the flashing colon separator.
    if (millis() - lastTime >= 1000)
    {
      lastTime = millis();
      getTemperature(szTempC);
      getTime(szTimeL, flasher);
      flasher = !flasher;

      P.displayReset();

      // synchronise the start
      P.synchZoneStart();
    }
  }
}

Welcome to the forum

The sprintf() function adds a terminating '\0' to the buffer specified but the buffer is only declared as having a single element. In any case, if you only want a single character why not use a simple char variable and set it to the required character ?

2 Likes

I am very grateful to you for clarifying about the '\0' . I spent a lot of time to understand why other values are added to the variable, and the problem was in the terminating '\0'.

works correctly:
char szTempP[2];
char szTempS[2];

If you use the hungarian notation, you should actually know what sz stands for :smiley:

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