Note that the Adafruit 0.54" Quad Alphanumeric FeatherWing Display PN 3218 is a kit that contains the Adafruit 14-Segment Alphanumeric LED FeatherWing (backpack), 2x pack - 0.54" Ultra-bright dual alphanumeric blue display, and two 16-pin headers.
Code that demonstrates displaying a floating point number on the display. Only four digits, so limited to numbers between +/- 0.01 to +/- 999. The +/- sign is always displayed.
/*
Code development for the conversion and formatting of
a floating point number in the range of +/- 0.01 to
+/- 999 for display on the Adafruit 0.54" Quad Alphanumeric
FeatherWing Display
Adafruit 0.54" Quad Alphanumeric FeatherWing Display
I2C 0x70
~ 40mA
https://www.adafruit.com/product/3130
https://www.adafruit.com/product/3127
https://www.adafruit.com/product/3128
https://www.adafruit.com/product/3129
https://www.adafruit.com/product/3131
https://www.adafruit.com/product/3132
*/
void BuildForCharAlphaNumericDisplayCharArr(float f, char *arr, unsigned int len, char cErr);
void WriteFloatCharToAlphaDisplay(char *arr, unsigned int len);
/////////////////////////////////////////////////////////////////////////
// Adafruit 0.56" 4-Digit LED Display
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
// The statement below is specific to the Alphanumeric FeatherWing display
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
/////////////////////////////////////////////////////////////////////////
void setup () {
Serial.begin(115200);
while (!Serial) {
delay(1);
}
Serial.println("\nSerial ready\n");
// Initialize the Adafruit 0.54" Quad Alphanumeric FeatherWing Display
alpha4.begin(0x70); // pass in the address
alpha4.clear();
alpha4.writeDisplay();
randomSeed(millis());
float f = 0.0;
char arrFloat[5]; // 4 characters + 1 for decimal point + 1 for Cr
f = -0.01234;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = 0.01234;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = -0.1234;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = 0.1234;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = -1.2345;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = 1.2345;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = -12.345;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = 12.345;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = -123.567;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = 123.567;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
// floats too big..
f = -1234.567;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = 1234.567;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = -12345.67;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
f = 12345.67;
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
} // setup()
void loop() {
char arrFloat[5]; // 4 characters + 1 for decimal point + 1 for Cr
float f = fGetSmUnSignedRandFloat();
Serial.print("'"); Serial.print(f); Serial.print("' -> '");
//BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), 0x20);
BuildForCharAlphaNumericDisplayCharArr(f, arrFloat, sizeof(arrFloat), '-');
for (unsigned int i=0; i<sizeof(arrFloat); i++){
Serial.print(arrFloat[i]);
}
Serial.println("' \n");
WriteFloatCharToAlphaDisplay(arrFloat, sizeof(arrFloat));
delay(1000);
// The yield() function allows ESP8266 microcontroller to run a
// number of utility functions in the background, without causing
// the ESP8266 to crash or reset. Include it within any
// while() + digitalRead() and other loops;
yield();
} // loop()
////////////////////////////////////////////////////////////////
void WriteFloatCharToAlphaDisplay(char *arr, unsigned int len) {
// Write the contents of arr to the Adafruit 0.54" Quad Alphanumeric FeatherWing Display
// Note: uint8_t is an alias of unsigned char
// void Adafruit_AlphaNum4::writeDigitAscii(uint8_t n, uint8_t a, bool d)
alpha4.clear();
//alpha4.writeDisplay();
if (arr[2] == '.') {
// '-0.01'
alpha4.writeDigitAscii(0, arr[0], false);
alpha4.writeDigitAscii(1, arr[1], true);
alpha4.writeDigitAscii(2, arr[3], false);
alpha4.writeDigitAscii(3, arr[4], false);
} else if (arr[3] == '.') {
// '-12.3'
alpha4.writeDigitAscii(0, arr[0], false);
alpha4.writeDigitAscii(1, arr[1], false);
alpha4.writeDigitAscii(2, arr[2], true);
alpha4.writeDigitAscii(3, arr[4], false);
} else if (arr[4] == '.') {
// '-123.'
alpha4.writeDigitAscii(0, arr[0], false);
alpha4.writeDigitAscii(1, arr[1], false);
alpha4.writeDigitAscii(2, arr[2], false);
alpha4.writeDigitAscii(3, arr[3], true);
} else {
alpha4.writeDigitAscii(0, arr[0], false);
alpha4.writeDigitAscii(1, arr[1], false);
alpha4.writeDigitAscii(2, arr[2], false);
alpha4.writeDigitAscii(3, arr[3], false);
}
alpha4.writeDisplay();
delay(2000);
} // WriteFloatCharToAlphaDisplay()
void BuildForCharAlphaNumericDisplayCharArr(float f, char *arr, unsigned int len, char cErr) {
// Returns a five character array built from float f that always
// begins with a +/- sign. If the number of digits to the left
// of the decimal point is greater than four, then arr is assigned
// the character specified by cErr.
// Note: 0x20; // space character
// Largest floating point retured is -999.0 or +999.0
for (unsigned int i=0; i<len; i++){
arr[i] = 0x20;
}
// buff needs to be large enough to handle any size float f passed as an argument.
char buff[30];
// https://www.programmingelectronics.com/dtostrf/
dtostrf(f, 4, 2, buff);
// Find the position of the decimal point in buff
byte iPos = 99;
for (unsigned int i=0; i<sizeof(buff); i++){
if (buff[i] == '.') {
if (buff[0] == '-') {
iPos = i;
} else {
iPos = i;
}
break;
}
}
//Serial.print("iPos = "); Serial.println(iPos);
if (buff[0] == '-' && iPos > len-1) {
//Serial.println("Case Y");
// float is too large. Return '----'
for (unsigned int i=0; i<len; i++){
arr[i] = cErr;
}
} else if (buff[0] != '-' && iPos > len-2) {
//Serial.println("Case Z");
// float is too large. Return '----'
for (unsigned int i=0; i<len; i++){
arr[i] = cErr;
}
} else {
if (buff[0] == '-' && buff[len-1] == '.') {
//Serial.println("Case A'");
// buff[] -> arr[]
// '-123.57' -> '-123.'
for (unsigned int i = 0; i<len; i++){
arr[i] = buff[i];
}
} else if (buff[0] == '-' && iPos < len-1) {
//Serial.println("Case B");
for (unsigned int i = 0; i<len; i++){
arr[i] = buff[i];
}
} else if (buff[0] == '-') {
//Serial.println("Case C");
// buff[] -> arr[]
// '-321' -> '-321'
for (unsigned int i = 0; i<len; i++){
arr[i] = buff[i];
}
} else if (buff[len-2] == '.') {
//Serial.println("Case D'");
// buff[] -> arr[]
// '123.57' -> '+123'
// '123.57' -> '+123.'
// '123.57' -> '+123 ' -> '+123.'
arr[0] = '+';
for (unsigned int i = 1; i<len; i++){
arr[i] = buff[i-1];
}
} else {
//Serial.println("Case E");
// buff[] -> arr[]
// '1.23' -> '+1.2'
// '12.35' -> '+12.3'
arr[0] = '+';
for (unsigned int i = 1; i<len; i++){
arr[i] = buff[i-1];
}
}
// '-43.' -> ' -43'
// '-321' -> '-321'
// '321.0' -> '+321'
// '1.234' -> '+1.2'
} // (iPos > len)
} // BuildForCharAlphaNumericDisplayCharArr()
float fGetSmUnSignedRandFloat() {
long unsigned myLong = random(1000, 9999);
long divisor;
int expnt = random(2,6);
switch (expnt) {
case 2:
divisor = 100;
break;
case 3:
divisor = 1000;
break;
case 4:
divisor = 10000;
break;
case 5:
divisor = 100000;
default:
divisor = 10;
}
float f = (float)myLong / divisor;
int sign = random(10);
if (sign <= 5) {
f = f * (-1.0);
}
return f;
} // fGetSmUnSignedRandFloat()
Do you need help developing or customizing a IoT product for your needs? Send me an email requesting a free one hour phone / web share consultation.
The information presented on this website is for the author's use only. Use of this information by anyone other than the author is offered as guidelines and non-professional advice only. No liability is assumed by the author or this web site.