I've got a number (in this case between 0 and 1023, but it could be longer. I'm trying to output it to a display with '_NumOfDigits' digits so I need to add leading zeros. eg if the number is 123 and I have 7 digints then I want '0000123' as my string.
I've had a stab at it using strlen but it hasn't worked. Something to do with chars and strings. This is what I tried but I'm happy to go another way:
Snippet
char strDisplay;
int x;
int NumberSize;
int AddZeros;
int res;
int shiftword;
strDisplay = String(intNumber, DEC);
NumberSize = strlen(strDisplay);
AddZeros = _NumOfDigits - NumberSize;
if ( AddZeros > 0)
{
for (x =0; x <= AddZeros; x++)
{
strDisplay = "0" + strDisplay;
}
}
In context:
#include "Arduino.h"
#include "Shifter.h"
Shifter::Shifter(int NumOfDigits, int SRCKPin, int SERINPin, int RCKPin)
{
_NumOfDigits = NumOfDigits;
pinMode(SRCKPin, OUTPUT);
_SRCKPin = SRCKPin;
pinMode(SERINPin, OUTPUT);
_SERINPin = SERINPin;
pinMode(RCKPin, OUTPUT);
_RCKPin = RCKPin;
}
void Shifter::clear()
{
int x;
digitalWrite(_RCKPin, LOW);
// shift out 0's to clear display
for (x = _NumOfDigits; x >= 0; x--)
{
shiftOut(_SERINPin, _SRCKPin, LSBFIRST, 0);
}
digitalWrite(_RCKPin, HIGH);
}
int Shifter::display(int intNumber)
{ // Displays number on display
int SegmentArray[] = {252, 96, 218, 242, 102, 182, 190, 224, 254, 230};
int SegmentArrayDP[] = {253, 97, 219, 243, 103, 183, 191, 225, 255, 231};
char strDisplay;
int x;
int NumberSize;
int AddZeros;
int res;
int shiftword;
strDisplay = String(intNumber, DEC);
NumberSize = strlen(strDisplay);
AddZeros = _NumOfDigits - NumberSize;
if ( AddZeros > 0)
{
for (x =0; x <= AddZeros; x++)
{
strDisplay = "0" + strDisplay;
}
}
digitalWrite(_RCKPin, LOW);
for (x = _NumOfDigits ; x >= 0; x--)
{
res = strDisplay[x] - '0';
if (x == 2)
{
shiftword = SegmentArrayDP[res];
}
else
{
shiftword = SegmentArray[res];
}
// shift out the bits
shiftOut(_SERINPin, _SRCKPin, LSBFIRST, shiftword);
}
digitalWrite(_RCKPin, HIGH);
}