//Given 1022, we display "10:22" //Each digit is displayed for ~2000us, and cycles through the 4 digits //After running through the 4 numbers, the display is turned off void SevSeg::DisplayNumber(int toDisplay, byte DecPlace){ //For the purpose of this code, digit = 1 is the left most digit, digit = 4 is the right most digit //If the number received is negative, set the flag and make the number positive boolean negative = false; if (toDisplay < 0) { negative = true; toDisplay = toDisplay * -1; } for(byte digit = numberOfDigits ; digit > 0 ; digit--) { //Turn on a digit for a short amount of time switch(digit) { case 1: digitalWrite(digit1, DigitOn); break; case 2: digitalWrite(digit2, DigitOn); break; case 3: digitalWrite(digit3, DigitOn); break; case 4: digitalWrite(digit4, DigitOn); break; //This only currently works for 4 digits } if(toDisplay > 0) //The if(toDisplay>0) trick removes the leading zeros lightNumber(toDisplay % 10); //Now display this digit else if(negative == true) { //Special case of negative sign out in front lightNumber(DASH); negative = false; //Now mark negative sign as false so we don't display it multiple times } //Service the decimal point if(DecPlace == digit) lightNumber(DP); toDisplay /= 10; delayMicroseconds(2000); //Display this digit for a fraction of a second (between 1us and 5000us, 500-2000 is pretty good) //If you set this too long, the display will start to flicker. Set it to 25000 for some fun. //Turn off all segments lightNumber(BLANK); //Turn off all digits digitalWrite(digit1, DigitOff); digitalWrite(digit2, DigitOff); digitalWrite(digit3, DigitOff); digitalWrite(digit4, DigitOff); } }
void segmentDisplay::displayNumber(int number) { #define DISPLAY_BRIGHTNESS 5000 #define DIGIT_ON HIGH #define DIGIT_OFF LOW pinMode(segA, OUTPUT); pinMode(segB, OUTPUT); pinMode(segC, OUTPUT); pinMode(segD, OUTPUT); pinMode(segE, OUTPUT); pinMode(segF, OUTPUT); pinMode(segG, OUTPUT); pinMode(digit1, OUTPUT); pinMode(digit2, OUTPUT); pinMode(digit3, OUTPUT); pinMode(digit4, OUTPUT); for (int digit = 4; digit > 0; digit--) { //Turn on a digit for a short amount of time switch (digit) { case 1: digitalWrite(digit1, DIGIT_ON); break; case 2: digitalWrite(digit2, DIGIT_ON); break; case 3: digitalWrite(digit3, DIGIT_ON); break; case 4: digitalWrite(digit4, DIGIT_ON); break; } //Turn on the right segments for this digit lightNumber(number%10); number /= 10; delayMicroseconds(DISPLAY_BRIGHTNESS); //Display digit for fraction of a second (1us to 5000us, 500 is pretty good) //Turn off all segments lightNumber(10); //Turn off all digits digitalWrite(digit1, DIGIT_OFF); digitalWrite(digit2, DIGIT_OFF); digitalWrite(digit3, DIGIT_OFF); digitalWrite(digit4, DIGIT_OFF); } }