Require components:
1x Arduino Uno
4x 220ohm Resistors
12x wires
1x 4-DIGIT 7-SEGMENT LED (model MR-3461SRB-2 13 20)
Code:
const byte PINS_DIGIT[] = {8, 11, 12, 7}; // 4 digits 7 segment pins: 12, 9, 8, 6
const byte TOTAL_DIGITS = sizeof(PINS_DIGIT);
const byte PINS_ABCDEFGP[] = {9, 13, 5, 3, 2, 10, 6, 4}; // pins from left to right: A, B, C, D, E, F, G, DotPoint, 4 digits 7 segment pins:
const byte MESSAGE[] = { // list of bytes to display from top to bottom, 1 byte = 8 bits, bit froms right to left
B00111101, // G
B01011100, // o
B01011100, // o
B01011110, // d
B00000000, // (space)
B00110111, // N
B00110111, // N
B01011100, // o
B01010000, // r
B01010100, // n
B00010000, // i
B01010100, // n
B01101111, // 9
B00000000, // (space)
B01110110, // H,X
B01011100, // o
B00011100, // u
B00011100, // u
B00000000, // (space)
B11011100, // a
B01010000, // r
B01111011, // e
B00000000, // (space)
B01101110, // Y
B01011100, // o
B00011100, // u
B00000000, // (space)
B00110111, // N
B00110111, // N
B01101110, // Y
B00000000, // (space)
B01010100, // n
B11011100, // a
B01010100, // n
B01010100, // n
B01111011, // e
B00000000, // (space)
B00000110, // 1
B01101101, // 5,S
B00000000, // (space)
B00111000, // L
B01111011, // e
B00000000, // (space)
B00111001, // C
B01011100, // o
B01010100, // n
B01101111, // 9
B00000000, // (space)
B01011110, // d
B00011100, // u
B01011000, // c
B00000000, // (space)
B00000000, // (space)
B00000000, // (space)
};
const short MESSAGE_LENGTH = sizeof(MESSAGE);
const short DELAY_DIGIT = 50;
const short DELAY_MOVE = 1000; // "short" cost 2 byte, value from -32,768 to 32,767
short delayRemain = DELAY_MOVE;
short offsetMessage = 0;
byte indexDigit = 0;
void setup() {
for(byte indexPin = 0; indexPin < sizeof(PINS_ABCDEFGP); indexPin++) {
pinMode(PINS_ABCDEFGP[indexPin], OUTPUT); // set pins A, B, C, D, E, F, G are output.
}
for(byte indexPin = 0; indexPin < TOTAL_DIGITS; indexPin++) {
pinMode(PINS_DIGIT[indexPin], OUTPUT); // set pins digits 1, 2, 3, 4 are output
}
}
void switchDigit(byte indexDigit) { // turn on 1 digit at time
for(byte indexPin = 0; indexPin < TOTAL_DIGITS; indexPin++) {
bool isOn = indexPin == indexDigit;
digitalWrite(PINS_DIGIT[indexPin], isOn);
}
}
void writeDigit(short byteDisplay) { // digitValue value from 0 to 9, this function turn on/off 8 LED (A, B, C, D, E, F, G, DotPoint)
for(byte indexPin = 0; indexPin < sizeof(PINS_ABCDEFGP); indexPin++) { // indexPin (from left to right in array) = indexBit (from right to left in calculator)
bool isOnModeCommonCathod = byteDisplay & (1 << indexPin); // get bit at indexPin; from right to left; value 0 = LED off = LOW; value 1 = LED on = HIGH
bool isOnModeCommonAnode = !isOnModeCommonCathod;
digitalWrite(PINS_ABCDEFGP[indexPin], isOnModeCommonAnode); // write out put pins (A, B, C, D, E, F, G, DotPoint) on or off, we are using mode common anode (CA)
}
}
void loop() {
while(delayRemain > 0) { // switch 4 digits until
switchDigit(indexDigit);
byte byteDisplay = MESSAGE[(offsetMessage + indexDigit) % MESSAGE_LENGTH];
writeDigit(byteDisplay);
delay(DELAY_DIGIT);
delayRemain -= DELAY_DIGIT; // each digit display will decrease time display the number.
indexDigit = (indexDigit + 1) % TOTAL_DIGITS; // switch to next digit on the right
}
delayRemain += DELAY_MOVE;
offsetMessage = (offsetMessage + 1) % MESSAGE_LENGTH;
}
Video: