Well, I don't really know if it's the right place for my topic, but I believe this is the most relevant one at least, so let's get to it.
What I'm trying to accomplish: I have a 4 digit 8 segment display, and I'd like to have it display the seconds elapsed, with showing the 1000th of seconds after a dot on the last 3 digits.
I got the said 4 digit 8 segment display in my Infiduino starter kit, I attached the data sheet, and a basic sketch of the circuit to this post. This is a common anode one.
The code I'm using was provided with the starter kit, it does what it should without problem, it displays the number 2014:
/*
4 Digital 8-Segment LED Display
Display four numbers on 4 Digital 8-Segment LED Display
In this sketch, we use D2-D9 to control A/B/C/D/E/F/G/DP segment respectively. And use D10/D11/D12/D13 to control the anode of DIG1-DIG4 respectively.
*/
int DIG[4]={10,11,12,13};//The pins used to controll the anode of the 4 bit,DIG1-DIG4.
char Str[4] = {'2', '0', '1', '4'};//The number to be displayed.
byte Numcode[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};//the character code for number 1-9
void setup()
{
int i=2;
//Set D2-D13 as OUTPUT
for(i=2;i<14;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,HIGH);
}
}
void loop()
{
int i=0;
int j;
int k=0;
int l=0;
//Display DIG1-DIG4
for(k=0;k<4;k++)
{
digitalWrite(DIG[k],HIGH);//Set DIG[K] to be ON.
i=Str[k]-48;//find out the number's position in the character code array.
//display the number
for(j=0;j<8;j++)
{
if(Numcode[i]&1<<j)
digitalWrite(9-j,LOW);
else
digitalWrite(9-j,HIGH);
}
delay(1);
digitalWrite(DIG[k],LOW);
}
}
I thought this sample code would be a good start to tinker around with to try and achieve my goal. I unfortunately don't fully understand the code, I'm quite new to Arduino, electronics and C in general, but how it works is the following I think:
Each digit has a pin, which has to be on HIGH, for us to be able to set the segments of the actual digit. It sets DIG1 to HIGH, then it uses the variable 'j' to go through the 8 segments (A,B,C,D,E,F,G and the dot, DP) somehow it determines using the 'Numcode' array that which segment should be on, and sets those to LOW, while the others to HIGH (since this is a common anode display) and when all the segments have been set, then sets DIG1 to LOW, and moves on to DIG2, repeat until all 4 digits have been set.
There are some parts that I don't really understand in this code, and would really like to get guidance with:
- The whole code which sets the digits has to be looped constantly. Otherwise it just lights up for a brief moment, then goes dark forever. I cannot really see based on this code, why is that?
- What is in the Numcode array, and how does it help to determine which segments should be on for each number through 1-9?
- i=Str[k]-48; The comment next to it says that it finds out the number's position in the character code array (which is 'Numcode' I think?). How does this do that exactly?
- if(Numcode(i)&1<<j). This statement determines if a particular segment should be on or off. This one is the most "cryptic" to me, I looked up the logic operators in C, and still don't really understand what this one does. (I replaced the square brackets to round ones due to it parsing it as italic)
- digitalWrite(9-j,... The "for" cycle this command is in implies that the order of the segments and the order the program goes through said segments are reversed if I'm correct. Why?
So I had these questions in mind, but I still tinkered around with the code, trying to get it to display the timer as I wanted. I put the whole code in the loop function into another function, then increasing the last member of the array, hoping that it'd display the two numbers after each other. This didn't really work, probably because the type of the 'Str' array is char. Then I tried manually re-declaring the array with the new number, but it didn't work either. Then I thought since it needs to be looped to display the numbers continuously, I tried various combination of "for" cycles, then re-declaring the array. For example looping the displaying function 10 times, re-declaring the "Str" array, then looping the display function another 10 times, no luck this time either.
I'm pretty much out of ideas, and would really like to get help with this (pretty much first) project of mine. If someone could give me a sketch which does the timer thing what I originally wanted that would be great, but I'd highly appreciate if someone could answer the questions I had, and maybe help with the code too. The Arduino is a great platform, and I'm just getting into it, and realizing the potential it has. I hope this forum can help me.