Now time for the painful part.
When you have defined your SEGMENT[] and DIGIT[] variables, you will then be able to write SetSegment().
This function will take those values and "magically" make a number appear on the Arduino's 7-segment display.
However, how exactly does it work?
Dr. Marz was nice enough to give you some comments to literally tell you how to do it:
Code (C++)
void SetSegment(int segment, int digit) {
//Set <segment>'s value to <digit>
//DO NOT USE shiftOut() in this code!!!
//Steps to write a value
//1. Open the latch (LATCH_DIO) by setting it's value LOW using digitalWrite()
//2. Write the first bit to DATA_DIO, cycle the clock (CLK_DIO) using digitalWrite()
//3. ... Write the nth bit, cycle the clock using digitalWrite()
//4. Close the latch (LATCH_DIO) by setting it's value HIGH using digitalWrite()
}
Why you are not allowed to use "shiftOut"
Here's what the function would look like if you used shiftOut.
Code (C++)
void SetSegment(int segment, int digit) {
digitalWrite(LATCH_DIO,LOW);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, DIGITS[digit]);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENTS[segment]);
digitalWrite(LATCH_DIO,HIGH);
}
It is way too easy when done this way. shiftOut does all of the handling of the CLK_DIO and DATA_DIO variables for you.
Hint: You can copy and paste this function to
test and make sure that your SEGMENT[] and DIGIT[] array values are correct.
Just make sure you don't turn in your lab with it (or else)!
"Ok Clara, then what should I do?"
Write it manually, of course!
Keep the LATCH_DIO code from above (he literally tells you to do this in the instructions).
Then loop through the 8 bits given in DIGITS[digit].
Bitshift them over until you can extract each 0 and 1 (so if I had 0b11111000, I should be able to get each and every bit from it via bitshifting).
And use
digitalWrite to DATA_DIO the value. Then cycle CLK_DIO.
Here is documentation for digitalWrite().
Use it like how it's used in the shiftOut example above.
Note: You are doing a for loop
twice.
One time for writing the DIGIT[digit] value to DATA_DIO, and then a second time for SEGMENTS[segment].
If your segment isn't working consider the following options:
- Check if your SEGMENT[] values are correct.
- Also try flipping the order of the bits that you write to DATA_DIO if it doesn't work.
If you are still struggling, check with me or another TA and we will help.
Hint: Just find out how shiftOut works, since you are essentially just writing that function manually.
Here is its documentation.