Created
July 23, 2017 16:13
-
-
Save izzuddin91/ecf51cef0f6064e057bb6bce106cf7dc to your computer and use it in GitHub Desktop.
4 digits 7 segment LED
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
| //ADDED 24 JULY 2017 | |
| //model ATA8041AB, 6 pins | |
| int segA = 11; // top | |
| int segB = 7; // right-top | |
| int segF = 10; // left-top | |
| int segD = 2; // bottom | |
| int segE = 1; // left-bottom | |
| int segC = 4; // right-bottom | |
| int segG = 5; // middle | |
| int digit1 = 14; // common cathode for digit1 (i.e. A0) | |
| int digit2 = 15; // common cathode for digit2 (i.e. A1) | |
| int digit3 = 16; // common cathode for digit3 (i.e. A2) | |
| int digit4 = 17; // common cathode for digit4 (i.e. A3) | |
| // Number to display | |
| static int number = 0; | |
| static int ms = 0; | |
| void setup() { | |
| // put your setup code here, to run once: | |
| // All pins in digital mode | |
| pinMode(digit1, OUTPUT); | |
| pinMode(digit2, OUTPUT); | |
| pinMode(digit3, OUTPUT); | |
| pinMode(digit4, OUTPUT); | |
| pinMode(segA, OUTPUT); | |
| pinMode(segB, OUTPUT); | |
| pinMode(segC, OUTPUT); | |
| pinMode(segD, OUTPUT); | |
| pinMode(segE, OUTPUT); | |
| pinMode(segF, OUTPUT); | |
| pinMode(segG, OUTPUT); | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| // if( millis() / 1000 > ms ) | |
| // { | |
| // ms = millis() / 1000; | |
| // if( ++number > 9999 ) number = 0; | |
| // } | |
| // | |
| // // activate common cathode for digit1 | |
| // digitalWrite(digit1, LOW); | |
| // // light on segments for 'thousands' | |
| // drawDigitFast( (number/1000) % 10 ); | |
| // // wait 2 ms | |
| // delay(2); | |
| // // turn off all segments | |
| // drawDigitFast( -1 ); | |
| // // turn off cathode for digit1 | |
| // digitalWrite(digit1, HIGH); | |
| // | |
| // digitalWrite(digit2, LOW); | |
| // drawDigitFast( (number/100) % 10 ); | |
| // delay(2); | |
| // drawDigitFast( -1 ); | |
| // digitalWrite(digit2, HIGH); | |
| // | |
| // digitalWrite(digit3, LOW); | |
| // drawDigitFast( (number/10) % 10 ); | |
| // delay(2); | |
| // drawDigitFast( -1 ); | |
| // digitalWrite(digit3, HIGH); | |
| // | |
| // digitalWrite(digit4, LOW); | |
| // drawDigitFast( number % 10 ); | |
| // delay(2); | |
| // drawDigitFast( -1 ); | |
| // digitalWrite(digit4, HIGH); | |
| } | |
| void drawDigitFast(int n) | |
| { | |
| const byte aPins[8] = { | |
| segA, segB, segC, segD, segE, segF, segG | |
| }; | |
| const byte aSegments[11][8] = { | |
| // A B C D E F G | |
| { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW }, // 0 | |
| { LOW, HIGH, HIGH, LOW, LOW, LOW, LOW }, // 1 | |
| { HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH }, // 2 | |
| { HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH }, // 3 | |
| { LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH }, // 4 | |
| { HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH }, // 5 | |
| { HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH }, // 6 | |
| { HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW }, // 7 | |
| { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH }, // 8 | |
| { HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH }, // 9 | |
| { LOW, LOW, LOW, LOW, LOW, LOW, LOW } // all off | |
| }; | |
| if( n < 0 || n > 10 ) | |
| { | |
| n = 10; | |
| } | |
| for( int i = 0; i < 7; i++ ) | |
| { | |
| digitalWrite( aPins[i], aSegments[n][i] ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment