wayoda / LedControl

An Arduino library for MAX7219 and MAX7221 Led display drivers
Other
464 stars 243 forks source link

My float sketch - as a demo for other's to adapt to whatever as they please... #16

Open randmor opened 8 years ago

randmor commented 8 years ago

Thank you for your LedControl library. Works pretty well, but could use some more demo sketches to help newbies understand how to use the library in their sketches. So, here's my sketch that displays floating point numbers on 8-digit 7-segment displays using your LedControl library.

`// ////////////////////////////////////////////////////////////////////////////////////////////////// // // This sketch is designed to output a floating point number to an 8-digit, 7-segment LED display // using the LedControl library and a float-to-string conversion function called dtostrf(). // // The displayed numbers are often off a tad... digit 0 is off, probably some kind of rounding error // associated with dtostrf(). // // ///////////////////////////////////////////////////////////////////////////////////////////////////

include // For dtostrf(), a float-to-string conversion function

include "LedControl.h" // We need to use the LedControl library to create a object...

LedControl lc=LedControl(12,11,10,1); // "lc" is the name of our object // pin 12 is connected to the MAX7219 pin 1 // pin 11 is connected to the CLK pin 13 // pin 10 is connected to LOAD pin 12 // 1 as we are only using 1 MAX7219

// Some randomly generated test numbers: //float f_val = 85288.039; // correct //float f_val = 4355.7391; // very close, digit 0 was "3". Off by 2. //float f_val = 78.737960; // very close, digit 0 was "1". Off by 1. //float f_val = 633.04075; // very close, digit 0 was "7". Off by 2. //float f_val = 406719.39; // very close, digit 0 was "8". Off by 1. //float f_val = 9027.9018; // very close, digit 0 was "4". Off by 4! //float f_val = 3.4024752; // very close, digit 0 was "1". Off by 1. //float f_val = 0.4413655; // correct //float f_val = 206947.90; // very close, digit 0 was "1". Off by 1. //float f_val = 18573226; // correct //float f_val = 0.0; // showed 0.0000000 (would be nice if showed 0.0 with leading zeros blanked out). float f_val = 212.4999999; // showed 212.50000. Likely a rounding error.

static char outputBuffer[20]; // outputBuffer[] likely 2x the size it needs to be.

void setup() {

// The first argument ("0") refers to the MAX7219 number, it is zero for 1 chip lc.shutdown(0,false); // turn off power saving, enables display lc.setIntensity(0,5); // sets brightness (0~15 possible values) lc.clearDisplay(0); // clear screen

dtostrf(f_val,7, 7, outputBuffer);

Serial.begin(9600); Serial.println("\n====Reset====");

printBuffer(outputBuffer);

}

void printBuffer(char *outputBuffer) {

int fudgeridoo=7;

for(int i=0; i<9; i++) { Serial.print(outputBuffer[i]); if (outputBuffer[i] == '.') { i--; lc.setChar(0,fudgeridoo-i,outputBuffer[i],1); i++; fudgeridoo++; } else { lc.setChar(0,fudgeridoo-i,outputBuffer[i],0); }
} }

void loop() { // Nothing happening here. } `

Enjoy...

-Wm. Moore