So, I'm really new to both programming and microcontrollers, although I have a (very) small bit of experience in C++. While reading this, keep in mind my inexperience! After doing some of the beginner projects, I thought it would be cool to make a 4 digit counter. I planned out the multiplexing with transistors and pins on the arduino, and made (incomplete and probably atrociously disorganized and working around, I know there's probably a better way to do this) code. I got to here in the loop function before I realized something:
void loop() {
settoDisp(4, timeCount1);
if (timeCount1 == 0) {
settoDisp(3, timeCount2);
timeCount2++;
if (timeCount2 == 0) {
settoDisp(2, timeCount3);
timeCount3++;
if (timeCount3 == 0) {
settoDisp(1, timeCount4);
timeCount3++
if
}
}
}
timeCount1++;
delay(1000);
}
I realized that if it delays for a second, it can't rapidly switch between the displays. I did some research on the internet, and found some stuff about shift registers that I didn't really get, and then I came here. I'd like to know how this would be possible, and also if there's something that would rapidly switch between the transistors so that I can write to the digits without stopping anything.
What you can do is use millis() or micros() & watch time go by, performing 2 functions at once.
The first will watch for 1 second to go by then, the update stuff as above.
The other will watch for say 500uS to go by, and (assuming you have common cathode parts) will:
turn off any cathodes (transistors)
turn on the anodes for a digit
turn on the cathode for that digit.
When 500uS go by, do the same for next digit.
Conceptually:
void loop(){
if (1 second elapsed){
update the time elements
}
if (500uS elapsed){
update the display with the current time elements
update counter to update the next digit on the next pass thru
}
} // end void loop
long counter1 = 1000;
long counter2 = 480;
long oldTime;
long oldTime2;
void loop() {
unsigned long currentTime = millis();
unsigned long currentTime2 = micros();
if (currentTime - oldTime > counter1) {
update the times for that point
}
if (currentTime2 - oldTime2 > counter2){
setdisp1(updatedDigit1);
delayMicroseconds(5);
setdisp2(updatedDigit2);
delayMicroseconds(5);
setdisp3(updatedDigit3);
delayMicroseconds(5);
setdisp4(updatedDigit4);
delayMicroseconds(5);
}
}
//setdisp1-4() takes the number passed in and writes the cathodes and anodes
In the if statement you should update the oldTimer variable so it will run again after the correct time.
You need to post all your code.
You would be better off not having a delay and only updating one digit in your time out routine. Use a variable to keep track of wat digit to update with each time out.
So I finished the code. I know it's not streamlined in the slightest bit, but when I started, I wanted to try figuring it out on my own so I could learn more. Would it work for my purpose?