Convert 24hr to 12hr clock

I have some code for a neo pixel 7 segment clock and the its get the correct time just in the 24hr format and I would like to have it display 12hr format. Any suggestions?

ESP32_CLOCK.ino (8.4 KB)

If(hour > 12)
{
   hour = hour - 12;
}

If you post your code as described in the forum guidelines more members will see your code.

You also have to deal with the hour starting at midnight…

Sorry here is the code.

/*

  WifiSettings basic example

  Source and further documentation available at
  https://github.com/Juerd/ESP-WiFiSettings

  Note: this example is written for ESP32.
  For ESP8266, use LittleFS.begin() instead of SPIFFS.begin(true).
*/
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <SPIFFS.h>
#include <WiFiSettings.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>


#define PIN 4
#define NUMPIXELS 244

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "north-america.pool.ntp.org", 21600, 36000);

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int twodots = 0;

int scanner[44];

int scannercounter = 0;

void setup() {
  Serial.begin(115200);
  SPIFFS.begin(true);
  WiFiSettings.connect();
  delay(500);
  pixels.begin();
  timeClient.begin();
}

void emptyMatrix()
{
  for (int j = 43; j < 400; j++)
  {
    pixels.setPixelColor(j, pixels.Color(0, 0, 0));
  }
}

void digit(int number, int shift, int red, int green, int blue)
{

  //UPPER SEGMENT
  if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
  {
    pixels.setPixelColor(28 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(29 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(30 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(31 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(32 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(33 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(34 + shift, pixels.Color(red, green, blue));

  }

  //UPPERLEFTSEGMENT
  if (number == 0 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
  {
    pixels.setPixelColor(21 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(22 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(23 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(24 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(25 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(26 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(27 + shift, pixels.Color(red, green, blue));

  }

  //UPPERRIGHTSEGMENT
  if (number == 0 || number == 1 || number == 2 || number == 3 || number == 4 || number == 7 || number == 8 || number == 9)
  {
    pixels.setPixelColor(35 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(36 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(37 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(38 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(39 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(40 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(41 + shift, pixels.Color(red, green, blue));

  }

  //MIDDLESEGMENT
  if (number == 2 || number == 3 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
  {
    pixels.setPixelColor(42 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(43 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(44 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(45 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(46 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(47 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(48 + shift, pixels.Color(red, green, blue));

  }

  //LOWERLEFTSEGMENT
  if (number == 0 || number == 2 || number == 6 || number == 8)
  {
    pixels.setPixelColor(14 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(15 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(16 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(17 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(18 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(19 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(20 + shift, pixels.Color(red, green, blue));

  }

  //LOWERRIGHTSEGMENT
  if (number == 0 || number == 1 || number == 3 || number == 4 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
  {
    pixels.setPixelColor(0 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(1 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(2 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(3 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(4 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(5 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(6 + shift, pixels.Color(red, green, blue));

  }

  //LOWERSEGMENT
  if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 8 || number == 9)
  {
    pixels.setPixelColor(7 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(8 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(9 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(10 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(11 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(12 + shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(13 + shift, pixels.Color(red, green, blue));
  }

  // This sends the updated pixel color to the hardware.

}

void twodotos(int shift, int red, int green, int blue)
{
  if (twodots == 0)
  {
    pixels.setPixelColor(shift, pixels.Color(red, green, blue));
    pixels.setPixelColor(shift + 1, pixels.Color(red, green, blue));
    pixels.setPixelColor(shift + 2, pixels.Color(red, green, blue));
    pixels.setPixelColor(shift + 3, pixels.Color(red, green, blue));
    twodots = 1;
  }
  else
  {
    pixels.setPixelColor(shift, pixels.Color(0, 0, 0));
    pixels.setPixelColor(shift + 1, pixels.Color(0, 0, 0));
    pixels.setPixelColor(shift + 2, pixels.Color(0, 0, 0));
    pixels.setPixelColor(shift + 3, pixels.Color(0, 0, 0));
    twodots = 0;
  }
}


void scannerdrawplus(int red, int green, int blue)
{
  for (int w = 0; w < 22; w++)
  {
    for (int i = 0; i < 44; i++)
    {
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    }
    pixels.setPixelColor(w, pixels.Color(red, green, blue));
    pixels.setPixelColor(w + 1, pixels.Color(red, green, blue));
    pixels.setPixelColor(w + 2, pixels.Color(red, green, blue));
    pixels.setPixelColor(w + 3, pixels.Color(red, green, blue));
    //pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
    //pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
    pixels.setPixelColor(42 - w, pixels.Color(red, green, blue));
    pixels.setPixelColor(42 - w + 1, pixels.Color(red, green, blue));
    pixels.setPixelColor(42 - w + 2, pixels.Color(red, green, blue));
    pixels.setPixelColor(42 - w + 3, pixels.Color(red, green, blue));
    //pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
    //pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
    pixels.show();
    delay(100);
  }
  for (int w = 21; w >= 0; w--)
  {
    for (int i = 0; i < 44; i++)
    {
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    }
    pixels.setPixelColor(w, pixels.Color(red, green, blue));
    pixels.setPixelColor(w + 1, pixels.Color(red, green, blue));
    pixels.setPixelColor(w + 2, pixels.Color(red, green, blue));
    pixels.setPixelColor(w + 3, pixels.Color(red, green, blue));
    //pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
    //pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
    pixels.setPixelColor(42 - w, pixels.Color(red, green, blue));
    pixels.setPixelColor(42 - w + 1, pixels.Color(red, green, blue));
    pixels.setPixelColor(42 - w + 2, pixels.Color(red, green, blue));
    pixels.setPixelColor(42 - w + 3, pixels.Color(red, green, blue));
    //pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
    //pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
    pixels.show();
    delay(125);
  }





}

void colorea(int red, int green, int blue)
{
  Serial.println(timeClient.getFormattedTime(if hour > 12
{
  hour = hour - 12;
});
String aux = timeClient.getFormattedTime();
             digit(int(aux.charAt(0)) - 48, 44, red, green, blue);
             digit(int(aux.charAt(1)) - 48, 44 + (7 * 7), red, green, blue);
             twodotos(44 + (7 * 7 * 2), red, green, blue);
             digit(int(aux.charAt(3)) - 48, 44 + (7 * 7 * 2) + 4, red, green, blue);
             digit(int(aux.charAt(4)) - 48, 44 + (7 * 7 * 3) + 4, red, green, blue);
             pixels.show();
}
void individual()
{
  Serial.println(timeClient.getFormattedTime());
  String aux = timeClient.getFormattedTime();
  digit(int(aux.charAt(0)) - 48, 44, 255, 0, 0); //HOUR TENTHS
  digit(int(aux.charAt(1)) - 48, 44 + (7 * 7), 255, 0, 255); //HOUR UNITS
  twodotos(44 + (7 * 7 * 2), 0, 0, 255);
  digit(int(aux.charAt(3)) - 48, 44 + (7 * 7 * 2) + 4, 0, 255, 255); //MINUTE TENTHS
  digit(int(aux.charAt(4)) - 48, 44 + (7 * 7 * 3) + 4, 0, 255, 0); //MINUTE UNITS
  pixels.show();
}

void loop() {

  timeClient.update();
  emptyMatrix();
  colorea(0, 95, 0);
  //individual();
  scannerdrawplus(0, 95, 0);

  //delay(1000);
}

Have a look at the modulo operator. It is how your clock operates and really useful for other stuff as well.

hour % 12 // will give a value between 0 and 12 (without 12)
11 % 12 -> 11
12 % 12 -> 0
13 % 12 -> 1

// this will give you the hour 4 hours later
// brackets are needed because % has the same order as multiplication over addition
hour = ( hour + 4 ) % 12

Note: For time computation you should use Epoch/UNIX time and not compute individual elements of time.

bool PM = hour > 11;
If( hour > 12)
{
hour -= 12;
}

add: #include <TimeLib.h>

and learn how to use that tool. or…

see:
http://w8bh.net/NTP_DualClock.pdf

they say ezTime is backwards compatible with TimeLib. you can #include <ezTime.h> in place of an existing #include <TimeLib.h> if you have the library installed, but it crashed and burned for me.

You appear to be using this NTP library:

Apart from the getFormattedTime() method, there are methods like getHours() which may be easier to work with instead of picking parts out of a String.

Did the compiler indicate a problem here?

void colorea(int red, int green, int blue)
{
  Serial.println(timeClient.getFormattedTime(if hour > 12
{
  hour = hour - 12;
});

Sorry, that was the code I was messing with trying to get it to display 12hr instead of 24hr. Here is the correct code that isn’t messed with.

/*

WifiSettings basic example

Source and further documentation available at
https://github.com/Juerd/ESP-WiFiSettings

Note: this example is written for ESP32.
For ESP8266, use LittleFS.begin() instead of SPIFFS.begin(true).
*/
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <SPIFFS.h>
#include <WiFiSettings.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>


#define PIN 4
#define NUMPIXELS 244

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "north-america.pool.ntp.org", 21600, 36000);

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int twodots =0;

int scanner[44];

int scannercounter = 0;

void setup() {
Serial.begin(115200);
SPIFFS.begin(true); 
WiFiSettings.connect();
delay(500);
pixels.begin();
timeClient.begin();
}

void emptyMatrix()
{
for (int j=43;j<400;j++)
{
pixels.setPixelColor(j, pixels.Color(0,0,0));
}
}

void digit(int number,int shift,int red,int green,int blue)
{

//UPPER SEGMENT
if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(28+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(29+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(30+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(31+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(32+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(33+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(34+shift, pixels.Color(red,green,blue));

}

//UPPERLEFTSEGMENT
if (number == 0 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(21+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(22+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(23+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(24+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(25+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(26+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(27+shift, pixels.Color(red,green,blue));

}

//UPPERRIGHTSEGMENT
if (number == 0 || number == 1 || number == 2 || number == 3 || number == 4 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(35+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(36+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(37+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(38+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(39+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(40+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(41+shift, pixels.Color(red,green,blue));

}

//MIDDLESEGMENT
if (number == 2 || number == 3 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(42+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(43+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(44+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(45+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(46+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(47+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(48+shift, pixels.Color(red,green,blue));

}

//LOWERLEFTSEGMENT
if (number == 0 || number == 2 || number == 6 || number == 8)
{
pixels.setPixelColor(14+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(15+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(16+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(17+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(18+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(19+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(20+shift, pixels.Color(red,green,blue));

}

//LOWERRIGHTSEGMENT
if (number == 0 || number == 1 || number == 3 || number == 4 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(0+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(1+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(2+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(3+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(4+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(5+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(6+shift, pixels.Color(red,green,blue));

}

//LOWERSEGMENT
if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(7+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(8+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(9+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(10+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(11+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(12+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(13+shift, pixels.Color(red,green,blue));
}

// This sends the updated pixel color to the hardware.

}

void twodotos(int shift, int red, int green, int blue)
{
if (twodots==0)
{
pixels.setPixelColor(shift, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+1, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+2, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+3, pixels.Color(red,green,blue));
twodots=1;
}
else
{
pixels.setPixelColor(shift, pixels.Color(0,0,0));
pixels.setPixelColor(shift+1, pixels.Color(0,0,0));
pixels.setPixelColor(shift+2, pixels.Color(0,0,0));
pixels.setPixelColor(shift+3, pixels.Color(0,0,0));
twodots=0;
}
}


void scannerdrawplus(int red, int green, int blue)
{
for (int w=0;w<22;w++)
{
for (int i=0;i<44;i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.setPixelColor(w, pixels.Color(red,green,blue));
pixels.setPixelColor(w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
pixels.show();
delay(100);
}
for (int w=21;w>=0;w--)
{
for (int i=0;i<44;i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.setPixelColor(w, pixels.Color(red,green,blue));
pixels.setPixelColor(w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
pixels.show();
delay(125);
}





}
void colorea(int red, int green, int blue)
{
Serial.println(timeClient.getFormattedTime());
String aux = timeClient.getFormattedTime();
digit(int(aux.charAt(0))-48,44,red,green,blue);
digit(int(aux.charAt(1))-48,44+(7*7),red,green,blue);
twodotos(44+(7*7*2),red,green,blue);
digit(int(aux.charAt(3))-48,44+(7*7*2)+4,red,green,blue);
digit(int(aux.charAt(4))-48,44+(7*7*3)+4,red,green,blue);
pixels.show();
}
void individual()
{
Serial.println(timeClient.getFormattedTime());
String aux = timeClient.getFormattedTime();
digit(int(aux.charAt(0))-48,44,255,0,0);//HOUR TENTHS
digit(int(aux.charAt(1))-48,44+(7*7),255,0,255);//HOUR UNITS
twodotos(44+(7*7*2),0,0,255);
digit(int(aux.charAt(3))-48,44+(7*7*2)+4,0,255,255);//MINUTE TENTHS
digit(int(aux.charAt(4))-48,44+(7*7*3)+4,0,255,0);//MINUTE UNITS
pixels.show();
}

void loop() {

timeClient.update();
emptyMatrix();
colorea(0,95,0);
//individual();
scannerdrawplus(0,95,0);

//delay(1000);
}

Maybe this gets you started. Function individual() is similar.
I’m assuming that getHours() is supported.

void colorea(int red, int green, int blue)
{
	Serial.println(timeClient.getFormattedTime());
	String aux = timeClient.getFormattedTime();
	// digit(int(aux.charAt(0))-48,44,red,green,blue);
	// digit(int(aux.charAt(1))-48,44+(7*7),red,green,blue);

    int hours24 = timeClient.getHours() ;  // getHours() supported ?
    int hours12 = hours24 == 0 ? 12 : hours24 - 12 ; 
	digit(hours12 / 10 ,44,red,green,blue); // leading 0 not blanked !
	digit(hours12 % 10 ,44+(7*7),red,green,blue);

	twodotos(44+(7*7*2),red,green,blue);
	digit(int(aux.charAt(3))-48,44+(7*7*2)+4,red,green,blue);
	digit(int(aux.charAt(4))-48,44+(7*7*3)+4,red,green,blue);
    pixels.show();
}

This code does not display the Hour units, as if the time was 06:34 it displays 00:34.

/*

WifiSettings basic example

Source and further documentation available at
https://github.com/Juerd/ESP-WiFiSettings

Note: this example is written for ESP32.
For ESP8266, use LittleFS.begin() instead of SPIFFS.begin(true).
*/
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <SPIFFS.h>
#include <WiFiSettings.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>


#define PIN 4
#define NUMPIXELS 244

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "north-america.pool.ntp.org", 21600, 36000);

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int twodots =0;

int scanner[44];

int scannercounter = 0;

void setup() {
Serial.begin(115200);
SPIFFS.begin(true); 
WiFiSettings.connect();
delay(500);
pixels.begin();
timeClient.begin();
}

void emptyMatrix()
{
for (int j=43;j<400;j++)
{
pixels.setPixelColor(j, pixels.Color(0,0,0));
}
}

void digit(int number,int shift,int red,int green,int blue)
{

//UPPER SEGMENT
if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(28+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(29+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(30+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(31+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(32+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(33+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(34+shift, pixels.Color(red,green,blue));

}

//UPPERLEFTSEGMENT
if (number == 0 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(21+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(22+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(23+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(24+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(25+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(26+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(27+shift, pixels.Color(red,green,blue));

}

//UPPERRIGHTSEGMENT
if (number == 0 || number == 1 || number == 2 || number == 3 || number == 4 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(35+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(36+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(37+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(38+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(39+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(40+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(41+shift, pixels.Color(red,green,blue));

}

//MIDDLESEGMENT
if (number == 2 || number == 3 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(42+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(43+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(44+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(45+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(46+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(47+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(48+shift, pixels.Color(red,green,blue));

}

//LOWERLEFTSEGMENT
if (number == 0 || number == 2 || number == 6 || number == 8)
{
pixels.setPixelColor(14+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(15+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(16+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(17+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(18+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(19+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(20+shift, pixels.Color(red,green,blue));

}

//LOWERRIGHTSEGMENT
if (number == 0 || number == 1 || number == 3 || number == 4 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(0+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(1+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(2+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(3+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(4+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(5+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(6+shift, pixels.Color(red,green,blue));

}

//LOWERSEGMENT
if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(7+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(8+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(9+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(10+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(11+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(12+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(13+shift, pixels.Color(red,green,blue));
}

// This sends the updated pixel color to the hardware.

}

void twodotos(int shift, int red, int green, int blue)
{
if (twodots==0)
{
pixels.setPixelColor(shift, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+1, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+2, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+3, pixels.Color(red,green,blue));
twodots=1;
}
else
{
pixels.setPixelColor(shift, pixels.Color(0,0,0));
pixels.setPixelColor(shift+1, pixels.Color(0,0,0));
pixels.setPixelColor(shift+2, pixels.Color(0,0,0));
pixels.setPixelColor(shift+3, pixels.Color(0,0,0));
twodots=0;
}
}


void scannerdrawplus(int red, int green, int blue)
{
for (int w=0;w<22;w++)
{
for (int i=0;i<44;i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.setPixelColor(w, pixels.Color(red,green,blue));
pixels.setPixelColor(w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
pixels.show();
delay(100);
}
for (int w=21;w>=0;w--)
{
for (int i=0;i<44;i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.setPixelColor(w, pixels.Color(red,green,blue));
pixels.setPixelColor(w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
pixels.show();
delay(125);
}





}
void colorea(int red, int green, int blue)
{
Serial.println(timeClient.getFormattedTime());
String aux = timeClient.getFormattedTime();
//digit(int(aux.charAt(0))-48,44,red,green,blue);
//digit(int(aux.charAt(1))-48,44+(7*7),red,green,blue);

int hours24 = timeClient.getHours() ;  // getHours() supported ?
int hours12 = hours24 == 0 ? 12 : hours24 - 12 ; 
digit(hours12 / 10,44,red,green,blue); // leading 0 not blanked !
digit(hours12 / 10,44+(7*7),red,green,blue);
twodotos(44+(7*7*2),red,green,blue);
digit(int(aux.charAt(3))-48,44+(7*7*2)+4,red,green,blue);
digit(int(aux.charAt(4))-48,44+(7*7*3)+4,red,green,blue);
pixels.show();
}
void individual()
{
Serial.println(timeClient.getFormattedTime());
String aux = timeClient.getFormattedTime();
//digit(int(aux.charAt(0))-48,44,red,green,blue);
//digit(int(aux.charAt(1))-48,44+(7*7),red,green,blue);

int hours24 = timeClient.getHours() ;  // getHours() supported ?
int hours12 = hours24 == 0 ? 12 : hours24 - 12 ; 
digit(hours12 / 10 ,44,0,255,255); // leading 0 not blanked !
digit(hours12 / 10 ,44+(7*7),0,255,0);
twodotos(44+(7*7*2),0,0,255);
digit(int(aux.charAt(3))-48,44+(7*7*2)+4,0,255,255);//MINUTE TENTHS
digit(int(aux.charAt(4))-48,44+(7*7*3)+4,0,255,0);//MINUTE UNITS
pixels.show();
}

void loop() {

timeClient.update();
emptyMatrix();
colorea(0,95,0);
//individual();
scannerdrawplus(0,95,0);

//delay(1000);
}

If I do the code with the % in it like you posted it displays 0 (Blank):34

It should be:

int hours12 = hours24 == 0 ? 12 : hours24 <= 12 ? hours24 : hours24 - 12;

Or alternatively but slower :

int hours12 = hours24 % 12 == 0 ? 12 : hours24 % 12;

And yes you must use % 10

Example here : dOSBg3 - Online C++ Compiler & Debugging Tool - Ideone.com

Oops. Yes. That would be better. The OP now has to work on a method of blanking the leading zero for the hours between 1 and 9. Presumably, omitting the call to digit() is not enough to erase any previous 1 in the transition from 12:59 to 1:00

I have it running and it seems to be transitioning from 12:59 to 1:00 just fine. I will post back in a few days just to confirm.

/*

WifiSettings basic example

Source and further documentation available at
https://github.com/Juerd/ESP-WiFiSettings

Note: this example is written for ESP32.
For ESP8266, use LittleFS.begin() instead of SPIFFS.begin(true).
*/
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <SPIFFS.h>
#include <WiFiSettings.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>


#define PIN 4
#define NUMPIXELS 244

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "north-america.pool.ntp.org", 25200);//21600

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int twodots =0;

int scanner[44];

int scannercounter = 0;

void setup() {
Serial.begin(115200);
SPIFFS.begin(true); 
WiFiSettings.connect();
//WiFiSettings.portal();
delay(500);
pixels.begin();
timeClient.begin();
}

void emptyMatrix()
{
for (int j=43;j<400;j++)
{
pixels.setPixelColor(j, pixels.Color(0,0,0));
}
}

void digit(int number,int shift,int red,int green,int blue)
{

//UPPER SEGMENT
if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(28+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(29+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(30+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(31+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(32+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(33+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(34+shift, pixels.Color(red,green,blue));

}

//UPPERLEFTSEGMENT
if (number == 0 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(21+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(22+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(23+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(24+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(25+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(26+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(27+shift, pixels.Color(red,green,blue));

}

//UPPERRIGHTSEGMENT
if (number == 0 || number == 1 || number == 2 || number == 3 || number == 4 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(35+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(36+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(37+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(38+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(39+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(40+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(41+shift, pixels.Color(red,green,blue));

}

//MIDDLESEGMENT
if (number == 2 || number == 3 || number == 4 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(42+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(43+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(44+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(45+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(46+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(47+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(48+shift, pixels.Color(red,green,blue));

}

//LOWERLEFTSEGMENT
if (number == 0 || number == 2 || number == 6 || number == 8)
{
pixels.setPixelColor(14+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(15+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(16+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(17+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(18+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(19+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(20+shift, pixels.Color(red,green,blue));

}

//LOWERRIGHTSEGMENT
if (number == 0 || number == 1 || number == 3 || number == 4 || number == 5 || number == 6 || number == 7 || number == 8 || number == 9)
{
pixels.setPixelColor(0+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(1+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(2+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(3+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(4+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(5+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(6+shift, pixels.Color(red,green,blue));

}

//LOWERSEGMENT
if (number == 0 || number == 2 || number == 3 || number == 5 || number == 6 || number == 8 || number == 9)
{
pixels.setPixelColor(7+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(8+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(9+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(10+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(11+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(12+shift, pixels.Color(red,green,blue));
pixels.setPixelColor(13+shift, pixels.Color(red,green,blue));
}

// This sends the updated pixel color to the hardware.

}

void twodotos(int shift, int red, int green, int blue)
{
if (twodots==0)
{
pixels.setPixelColor(shift, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+1, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+2, pixels.Color(red,green,blue));
pixels.setPixelColor(shift+3, pixels.Color(red,green,blue));
twodots=1;
}
else
{
pixels.setPixelColor(shift, pixels.Color(0,0,0));
pixels.setPixelColor(shift+1, pixels.Color(0,0,0));
pixels.setPixelColor(shift+2, pixels.Color(0,0,0));
pixels.setPixelColor(shift+3, pixels.Color(0,0,0));
twodots=0;
}
}


void scannerdrawplus(int red, int green, int blue)
{
for (int w=0;w<22;w++)
{
for (int i=0;i<44;i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.setPixelColor(w, pixels.Color(red,green,blue));
pixels.setPixelColor(w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
pixels.show();
delay(100);
}
for (int w=21;w>=0;w--)
{
for (int i=0;i<44;i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
pixels.setPixelColor(w, pixels.Color(red,green,blue));
pixels.setPixelColor(w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(w+5, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+1, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+2, pixels.Color(red,green,blue));
pixels.setPixelColor(42-w+3, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+4, pixels.Color(red,green,blue));
//pixels.setPixelColor(39-w+5, pixels.Color(red,green,blue));
pixels.show();
delay(125);
}





}
void colorea(int red, int green, int blue)
{
Serial.println(timeClient.getFormattedTime());
String aux = timeClient.getFormattedTime();
//digit(int(aux.charAt(0))-48,44,red,green,blue);
//digit(int(aux.charAt(1))-48,44+(7*7),red,green,blue);

int hours24 = timeClient.getHours() ;  // getHours() supported ?
int hours12 = hours24 == 0 ? 12 : hours24 <= 12 ? hours24 : hours24 - 12; 
digit(hours12 / 10,44,red,green,blue); // leading 0 not blanked !
digit(hours12 % 10,44+(7*7),red,green,blue);
twodotos(44+(7*7*2),red,green,blue);
digit(int(aux.charAt(3))-48,44+(7*7*2)+4,red,green,blue);
digit(int(aux.charAt(4))-48,44+(7*7*3)+4,red,green,blue);
pixels.show();
}
void individual()
{
Serial.println(timeClient.getFormattedTime());
String aux = timeClient.getFormattedTime();
//digit(int(aux.charAt(0))-48,44,red,green,blue);
//digit(int(aux.charAt(1))-48,44+(7*7),red,green,blue);

int hours24 = timeClient.getHours() ;  // getHours() supported ?
int hours12 = hours24 == 0 ? 12 : hours24 <= 12 ? hours24 : hours24 - 12; 
digit(hours12 / 10 ,44,0,255,255); // leading 0 not blanked !
digit(hours12 % 10 ,44+(7*7),0,255,0);
twodotos(44+(7*7*2),0,0,255);
digit(int(aux.charAt(3))-48,44+(7*7*2)+4,0,255,255);//MINUTE TENTHS
digit(int(aux.charAt(4))-48,44+(7*7*3)+4,0,255,0);//MINUTE UNITS
pixels.show();
}

void loop() {

timeClient.update();
emptyMatrix();
colorea(0,95,0);
//individual();
scannerdrawplus(0,95,0);

//delay(1000);
}

Clock has been running great and hasn’t missed a beat! Thanks for all the help!

Of course :slight_smile:

You could simplify your code. At least cache the value of pixels.Color(red, green, blue) instead of calling the function so many times, getting the value of a local variable is much faster than getting the result of a function.

You could do something like this (untested)

void digit( uint8_t number, int shift, int red, int green, int blue )
{
	/*	   A
	     F   B
	       G
	     E   C
	       D     */

	static const uint8_t segmentsData[] =
	{
	    //GFEDCBA
	    0b0111111, // '0'
	    0b0000110, // '1'
	    0b1011011, // '2'
	    0b1001111, // '3'
	    0b1100110, // '4'
	    0b1101101, // '5'
	    0b1111101, // '6'
	    0b0000111, // '7'
	    0b1111111, // '8'
	    0b1101111, // '9'
    };
    
	static const uint8_t segmentsFirstLed[] =
	{
		28, // A
		35, // B
		 0, // C
		 7, // D
		14, // E
		21, // F
		42, // G
	};
	
	const uint8_t digitData = segmentsData[ number ];
	const uint32_t color = pixels.Color(red,green,blue);
 
	for ( uint8_t i = 0; i < 7; i++ )
	{
		if ( bitRead( digitData, i ) )
		{
			uint8_t firstLed = segmentsFirstLed[ i ] + shift;
			
			for ( uint8_t j = 0; j < 7; j++ )
			{
				pixels.setPixelColor( firstLed + j, color );
			}
		}
	}
}

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