|
| 1 | +/* |
| 2 | + * Bas on Tech - 8 segment LED display |
| 3 | + * |
| 4 | + * This course is part of the courses on https://arduino-tutorials.net |
| 5 | + * |
| 6 | + * (c) Copyright 2019 - Bas van Dijk / Bas on Tech |
| 7 | + * This code and course is copyrighted. It is not allowed to use these courses commercially |
| 8 | + * without explicit written approval |
| 9 | + * |
| 10 | + * YouTube: https://www.youtube.com/c/BasOnTech |
| 11 | + * Facebook: https://www.facebook.com/BasOnTechChannel |
| 12 | + * Instagram: https://www.instagram.com/BasOnTech |
| 13 | + * Twitter: https://twitter.com/BasOnTech |
| 14 | + * |
| 15 | + * ---------------------------------------------------------------------- |
| 16 | + * |
| 17 | + * LedControl library: https://github.com/wayoda/LedControl |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "LedControl.h" |
| 22 | + |
| 23 | +// Initialise the LedControl library |
| 24 | +// pin 7 on DIN, |
| 25 | +// pin 6 on Clk, |
| 26 | +// pin 5 on LOAD |
| 27 | +// number of displays: 1 |
| 28 | +LedControl lc = LedControl(7, 6, 5, 1); |
| 29 | + |
| 30 | +int i = 0; |
| 31 | + |
| 32 | +void setup() { |
| 33 | + |
| 34 | + // On startup, the MAX72XX chip is in power-safe mode |
| 35 | + // we are waking this chip for device 0 with the shutdown command |
| 36 | + lc.shutdown(0, false); |
| 37 | + |
| 38 | + // Set the intensity of the screen on device 0 with a value between 0 and 15 |
| 39 | + lc.setIntensity(0, 5); |
| 40 | + |
| 41 | + // Clear the display of device 0 |
| 42 | + lc.clearDisplay(0); |
| 43 | +} |
| 44 | + |
| 45 | +// Show a number of max 4 positions, so 0...9999 |
| 46 | +void printNumber(int number) { |
| 47 | + int ones; |
| 48 | + int tens; |
| 49 | + int hundreds; |
| 50 | + int thousands; |
| 51 | + |
| 52 | + // Calculate the number of ones |
| 53 | + // we take the remainder when the number is divided by 10 |
| 54 | + ones = number % 10; |
| 55 | + // Save the number of divides without remainder |
| 56 | + number = number / 10; |
| 57 | + |
| 58 | + // Calculate the number of tens |
| 59 | + // we take the remainder when the number is divided by 10 |
| 60 | + tens = number % 10; |
| 61 | + number = number / 10; |
| 62 | + |
| 63 | + // Calculate the number of hundreds |
| 64 | + // we take the remainder when the number is divided by 10 |
| 65 | + hundreds = number % 10; |
| 66 | + number = number / 10; |
| 67 | + |
| 68 | + thousands = number; |
| 69 | + |
| 70 | + // Show the digits on the display |
| 71 | + lc.setDigit(0, 3, thousands, false); |
| 72 | + lc.setDigit(0, 2, hundreds, false); |
| 73 | + lc.setDigit(0, 1, tens, false); |
| 74 | + lc.setDigit(0, 0, ones, false); |
| 75 | +} |
| 76 | + |
| 77 | +void printNumber2(long number) { |
| 78 | + |
| 79 | + // Array of 8 bytes for each postion on the LED display |
| 80 | + byte digits[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 81 | + |
| 82 | + // Loop over each position to determine the number to show |
| 83 | + for (int i; i < 8; i++) { |
| 84 | + digits[i] = number % 10; |
| 85 | + number = number / 10; |
| 86 | + } |
| 87 | + |
| 88 | + // Loop over all LED positions |
| 89 | + for (int i; i < 8; i++) { |
| 90 | + // Show digit on position i on the LED display |
| 91 | + lc.setDigit(0, i, digits[i], false); |
| 92 | + } |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +void printNumber3(long number) { |
| 98 | + |
| 99 | + // Array of 8 bytes for each postion on the LED display |
| 100 | + byte digits[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 101 | + |
| 102 | + // Loop over each position to determine the number to show |
| 103 | + for (int i; i < 8; i++) { |
| 104 | + digits[i] = number % 10; |
| 105 | + number = number / 10; |
| 106 | + } |
| 107 | + |
| 108 | + // True when we've found the first non-zero digit |
| 109 | + bool firstNonZeroFound = false; |
| 110 | + |
| 111 | + // Loop backwards from 7 to 0 |
| 112 | + for (int i = 7; i >= 0; i--) { |
| 113 | + |
| 114 | + // If the number is not equal to 0 |
| 115 | + if(digits[i] != 0) { |
| 116 | + firstNonZeroFound = true; |
| 117 | + } |
| 118 | + |
| 119 | + // If the first non-zero number is found show this on the LED display |
| 120 | + if (firstNonZeroFound) { |
| 121 | + lc.setDigit(0, i, digits[i], false); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +void loop() { |
| 128 | + lc.clearDisplay(0); |
| 129 | + printNumber3(i); |
| 130 | + i++; |
| 131 | + delay(100); |
| 132 | +} |
0 commit comments