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 BYTES_DISPLAY_DIGIT[] = { // list of bytes to display from top to bottom, 1 byte = 8 bits, bit froms right to left
B00111111, // 0
B00000110, // 1
B01011011, // 2
B01001111, // 3
B01100110, // 4
B01101101, // 5
B01111101, // 6
B00000111, // 7
B01111111, // 8
B01101111, // 9
};
const byte DELAY_DIGIT = 1;
const short DELAY_NUMBER = 1000; // "short" cost 2 byte, value from -32,768 to 32,767
short delayRemain = DELAY_NUMBER;
void setup() {
// put your setup code here, to run once:
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(byte digitValue) { // digitValue value from 0 to 9, this function turn on/off 8 LED (A, B, C, D, E, F, G, DotPoint)
byte byteDisplay = BYTES_DISPLAY_DIGIT[digitValue];
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)
}
}
byte getDigitValue(short number, byte indexDigit) { // input a number, with index of digit to get
short offset = pow(10, TOTAL_DIGITS - indexDigit - 1);
return (number / offset) % 10;
}
void showNumber(short number) { // number value from 0 to 9999
byte indexDigit = TOTAL_DIGITS - 1; // update digit from right to left is better, value from the most right changed more times than left one.
while(delayRemain > 0) { // display 4 digits until this number timeout
byte digitValue = getDigitValue(number, indexDigit);
if(digitValue == 0) { // DO NOT display 0 value, saving power, focus on displaying number, digits on the right display better
indexDigit = TOTAL_DIGITS - 1; // update digit from right to left
digitValue = getDigitValue(number, indexDigit);
}
switchDigit(indexDigit);
writeDigit(digitValue);
delay(DELAY_DIGIT);
delayRemain -= DELAY_DIGIT; // each digit display will decrease time display the number.
indexDigit = (TOTAL_DIGITS + indexDigit - 1) % TOTAL_DIGITS; // switch to next digit on the left
}
}
void loop() {
// put your main code here, to run repeatedly:
for (short number = 0; number < 9999; number++) { // for each number, from min to max
showNumber(number);
delayRemain += DELAY_NUMBER; // reset display number time
}
}
How to make this:
No comments:
Post a Comment