Then the loop can go to the next iteration Lines 37-39 activate our digit for a millisecond and then deactivate it. Line 36 GPIO.output(segments, (num])) sets all of the segments’ GPIO ports in one line using list variable segments and looking up the appropriate display_string character for the current digit in our dictionary num This is so that we can reuse the same code in two different loops in our main code block without repeating it. Lines 34-39 I’ve taken the main segment/digit driving code and put it in a function. Notice we’ve added a few new ones from last time, namely b, y, E, A, L, & X. Lines 16-32 define our dictionary num of characters to display. Lines 11-13 create a list variable for the digit ports and initialise them all to outputs at 1 (remember setting these ports to 0 activates the digits on the 7-seg) Lines 6-8 create a list variable for the segment ports and initialise them all to outputs at 0 I’ve also changed s to display_string to make the code easier to read and understand. I’ve incorporated all of the above changes into the new code. …using our list variable segments and taking advantage of the fact that we can set an initial value for each port at the time of setup, using initial=0 or initial=1.įor these two tweaks to work correctly, we have to add a ‘ ,0‘ to the end of each character defined in our dictionary num because we defined 8 ports in our list variable segments (the last entry represents the decimal point). We can do all this with one line of code… tup(segments, GPIO.OUT, initial=0) In (num]) we’re finding the segment values in our dictionary num for the n th digit of our display string s.Īnother way we can simplify the code is to change… for segment in segments: …which means one less loop and one less level of slicing to get our heads around. This makes our code shorter, simpler and easier to understand. Using this feature of RPi.GPIO allows us to completely eliminate our for loop in range(0,7): loop. I knew this was possible, but I’d never used it before. Now onto the code tweaks… RPi.GPIO Can Use A List Variable All letters with diagonal lines on are tricky for this kind of 7 segment display. I couldn’t do an X, so my X is the same as the standard 7seg H. So you add that entry to your dictionary – it doesn’t matter where, but make sure you have a comma at the end of the line to match the correct format (the last entry has a }).Īnd that’s really all there is to it. But we have to map this in the correct order for our dictionary. Looking at the diagram, we can make a P if we light all of the segments except for c, d and dp. So let’s suppose we wanted to create the letter P. Our letter L has a, b & c inactive (0), d, e & f active (1), g and dp inactive (0). The first list element corresponds to a and they go in alphabetical order, ending with dp (decimal point). In our dictionary in the Python script, each entry has an 8 digit list of 1s and 0s. Each of the segments has a name a-g or dp. Look at the white lettering on the right-hand side.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |