/*
Purpose - to repeat Prof Spoelstra's 7 seg timing counter algorithm
without use of switch and case statements and sevseg library
extend to the hex alphabet
Understand the approach to simplify the code
Approach
- include pattern for bit maps of 7 seg digits and letters
- assign digit and segment pins to variables
- funct add - timing algorithm one timing increment per call to update segments
- funct clearleds - turn off leds by high-ing digit pins and low-ering segment pins (no current flow)
- funct picknumber - passes pattern to digital writes for parsing to turn on/off segment pins for the digit
- funct pickdigit - determines which digit line to turn low to light the digit
*/
#include <TimerOne.h>
// pin assignments
const byte a = 2; // connect to a
const byte b = 3; // b
const byte c = 4; // etc
const byte d = 5;
const byte e = 6;
const byte f = 7;
const byte g = 8;
const byte dp = 9;
const byte d4 = 10;
const byte d3 = 11;
const byte d2 = 12;
const byte d1 = 13;
// assign segment array
const byte SEG_ARRAY[] = { a, b, c, d, e, f, g, dp };
//assign digit pin array
const byte DIGIT_ARRAY[ ] = {d1, d2, d3, d4};
//assign 7-seg pattern for characters (extend thru hex alphabet)
const byte PATTERN [] = {
0x3F, // 0
0x06, // 1
0x58, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
// 0x77, // A
// 0x7C, // B
// 0x39, // C
// 0x5E, // D
// 0x79, // E
// 0x71 // F
};
byte timeDelay = 5; // multiplexing time between digits
volatile int count = 0; // count will be displayed on the 7-seg digits
void setup() {
// set digit pins as outpuy
for (byte j = 0; j < sizeof(SEG_ARRAY); j++) {
pinMode (SEG_ARRAY[j], OUTPUT);
}
// set segment pins as input_pullups (with internal current limit resistors)
for (byte j = 0; j < sizeof(DIGIT_ARRAY); j++) {
pinMode (DIGIT_ARRAY[j], INPUT_PULLUP);
}
//timing routines
Timer1.initialize(1000000); // set timer for 1e6 microsecons or 1 second
Timer1.attachInterrupt(add); // attach the timer ISR
Serial.begin(9600);
}
void loop() {
clearLEDs(); // clear display
pickDigit(0); // High-ing digit d1
pickNumber( (count/1000)); // get value of thousands digit
delay(timeDelay); // short delay to avoid flicker
clearLEDs(); // clear display
pickDigit(1); // High-ing digit d2
pickNumber( (count & 1000) / 100 ); // get value of hundreds digit
delay(timeDelay); // short delay to avoid flicker
clearLEDs(); // clear display
pickDigit(2); // High-ing digit d3
pickNumber( (count % 100) / 10); // get value of tens digit
delay(timeDelay); // short delay to avoid flicker
clearLEDs(); // clear display
pickDigit(3); // High-ing digit d4
pickNumber( (count % 10 )) ; // get value of unit digit
delay(timeDelay); // short delay to avoid flicker
// repeat sequence on same count until ISR increments count
//delay(1000); // delay to read the output
}
void clearLEDs() { // clear the 7-segment display
// set digital pins LOW
for (byte j = 0; j < sizeof(SEG_ARRAY); j++) {
digitalWrite (SEG_ARRAY[j], LOW); // to 0 v (no current thru led diodes)
}
// set segment pins HIGH
for (byte j = 0; j < sizeof(DIGIT_ARRAY); j++) {
digitalWrite (DIGIT_ARRAY[j], HIGH); // to 5 v (no current thru led diodes)
}
}
void pickDigit( byte digit) { // picks the digit to drop LOW for current flow
digitalWrite( DIGIT_ARRAY [digit], LOW);
// Serial.print("Digit= ");
// Serial.println(digit);
}
void pickNumber(byte pickPattern) { // p[icks the number pattern to display (1=HIGH, 0 = LOW)
// .gfedcba order
// Serial.print("Pat = ");
// Serial.print(pickPattern);
// Serial.print(" , = ");
// Serial.println(pickPattern, BIN);
digitalWrite( a, bitRead( PATTERN[pickPattern], 0)) ; // picks the 0th bit of PATTERN[picked]
digitalWrite( b, bitRead( PATTERN[pickPattern], 1)) ; // picks the 1st bit of PATTERN[picked]
digitalWrite( c, bitRead( PATTERN[pickPattern], 2)) ; // picks the 2nd bit of PATTERN[picked]
digitalWrite( d, bitRead( PATTERN[pickPattern], 3)) ; // picks the 3rd bit of PATTERN[picked]
digitalWrite( e, bitRead( PATTERN[pickPattern], 4)) ; // picks the 4th bit of PATTERN[picked]
digitalWrite( f, bitRead( PATTERN[pickPattern], 5)) ; // picks the 5th bit of PATTERN[picked]
digitalWrite( g, bitRead( PATTERN[pickPattern], 6)) ; // picks the 6th bit of PATTERN[picked]
digitalWrite( dp, bitRead( PATTERN[pickPattern], 7)) ; // picks the 7th bit of PATTERN[picked]
}
// ISR for 1 second timer
void add () {
// increment counter
count++;
// check if out of range
if (count >= 10000) {
count = 0;
}
}