Hello, I'm working on a school project that focuses on the topic of multiplexing. I'm tasked to make a counter that counts up from zero to 999, all the while having switches S1 and S2. S1 resets the timer back to zero, while S2 functions as the pause/play button for the timer.
I'll be using the reference found here:
Here is the schematic, it's a bit rough:
Pin 2 - A - Red
Pin 3 - B - Blue
Pin 4 - C - Yellow
Pin 5 - D - Green
Pin 6 - E - Orange
Pin 8 - F - Cyan
Pin 9 - G - Brown
Pin 7 - DP - Purple
Pin 10 - Switch Input
Pin 11 to 13 - Hundreds, Tens, and Ones digits
The pin configuration are set as is and could not be changed, unfortunately.
Here is the code I've written so far. It is rough and I've yet to write a way to display the digits properly.
I found this as useful reference but I am having difficulty in understanding how the multiplexing works. Any help here is very much appreciated.
const int numeral[10] = {
// 0 means that corresponding pin will light up
// because the 7-segment display used is common anode
// ABCDEPFG (P = decimal point)
B00000101, //0
B10011111, //1
B00100110, //2
B00001110, //3
B10011100, //4
B01001100, //5
B11000100, //6
B00011111, //7
B00000100, //8
B00011100, //9
};
int runFlag = 0; //determines whether the device would run or pause
int switchKey = 0; //determines which key is active
const int maxValue = 999; //the timer would no longer run when this number is reached
const int numDigits = 3; //number of digits displayed by the timer
int number = 0; //number currently displayed by the timer
int incTime = 1000; //number of milliseconds between increments
unsigned long lastUpdate = 0;
boolean inputSwitch = LOW;
//pins for decimal point and each segment
//DP, G, F, E, D, C, B, A
//(decimal point is not used)
const int segPins[8] = { 7, 9, 8, 6, 5, 4, 3, 2};
const int digitPins[numDigits] = { 13, 12, 11};
//pin definitions
int displayOnes = 13;
int displayTens = 12;
int displayHundreds = 11;
int toggleSwitch = 10;
void setup() {
for(int i=0; i < 8; i++){
pinMode(segPins[i], OUTPUT); //set segment pins as output
}
for(int i=0; i < numDigits; i++){
pinMode(digitPins[i], OUTPUT); //set digit pins 13 to 11 as output
}
pinMode(toggleSwitch, INPUT);
Serial.begin(9600);
}
void reset(){ //reset function
runFlag = 0;
number = 0;
}
void loop(){
unsigned long timeElapsed = millis();
//The code will count down ever second that has passed
//regardless of the state of the runFlag
if (timeElapsed - lastUpdate > incTime){
//The number variable will not increment if the runFlag is zero
if(runFlag != 0){
number++;
}
lastUpdate = timeElapsed;
//When number reaches beyond maxValue, counting will stop
//but will still display the value of the number
if(number >= maxValue){
int runFlag = 0;
}
}
int hund = number / 100;
int tens = ( (number / 10) % 10);
int ones = number % 10;
//Multiplexing Code
for(int i = 0; i < 2; i++){
//Ones Digit
if(i == 0)
{
switchKey = 1; //for the Play/Pause switch
boolean inputSwitch = digitalRead(toggleSwitch);
//When the Pause/Play switch is pressed
if(switchKey == 1 && inputSwitch){
int runFlag = 1;
}
//display code for ones digit
//[incomplete]
delay(1);
}
//Tens Digit
else if(i == 1)
{
switchKey = 0;
//display code for tens digit
//[incomplete]
delay(1);
}
//Hundreds Digit
switchKey = 2; //for the Reset switch
boolean inputSwitch = digitalRead(toggleSwitch);
//When the Reset switch is pressed
if(switchKey == 2 && inputSwitch ){
reset();
}
//display code for hundreds digit
delay(1);
}
}
Thank you for your time