Created
August 28, 2021 00:30
-
-
Save m9aertner/dc772104605ab17767394c0c55b48551 to your computer and use it in GitHub Desktop.
TeensyLC : output integer to 8x7-segment MAX7219 LED display, with no leading zeroes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TeensyLC MAX7219 7-Segment Sample. | |
// https://www.pjrc.com/teensy/td_libs_LedControl.html | |
#include <Arduino.h> | |
#include <LedControl.h> | |
/* | |
* Inputs: DIN pin, CLK pin, LOAD pin, number of digits | |
* | |
* Note: one MAX7219 chip drives 8x8 LEDs. With a 7-segment | |
* module, this corresponds to 8 digits with 7 segment LEDs plus | |
* one dot LED each. | |
* | |
* The least significant (rightmost) digit is at position 0. | |
* | |
* Sample unit: Amazon B07D8ZC7Q3 | |
*/ | |
LedControl display = LedControl(14, 16, 15, 8); | |
/* | |
* Emit number n to passed-in LedControl display. | |
* Show the decimal number. | |
* Leading zero digits will be blanked. | |
* | |
* Millions and thousands dots will be set, adapt if | |
* you expect higher numbers and have more than 8 | |
* 7-segment devices. | |
*/ | |
void showNumber(LedControl display, unsigned int n) { | |
int pos = 0; // start with least significant digit (ones) | |
while(pos < display.getDeviceCount()) { | |
display.setDigit(0, pos, n % 10, pos == 3 || pos == 6); | |
pos++; | |
n /= 10; | |
if(n == 0) { | |
break; | |
} | |
} | |
while(pos < display.getDeviceCount()) { | |
display.setChar(0, pos++, ' ', false); | |
} | |
} | |
void setup() { | |
display.shutdown(0, false); // turns on display | |
display.setIntensity(0, 3); // 15 = brightest | |
} | |
unsigned int n = 1001234; // Start with 1.001.234 | |
void loop() { | |
showNumber(display, n--); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment