Time for some more fun with LEDs. I'm going to call this GUI the LED command center because on the breadboard I have 8 LEDs wired to the Raspberry Pi and I can control whether each of those LEDs is on or off by entering a string into this text box of 1's and 0's. For instance, everywhere there is a 1 that LED will be on, and everywhere there is a 0 that LED will be off. So if I enter alternate 1's and 0's, and load that code, we can see on the breadboard that the LEDs have lit up in an alternate fashion. So we could turn half on and half off very easily with 11110000 and we can see that this half of the LEDs are lit. So lets have a look at how this is done.
Over in the script we've got the imports that we have seen before, and we are using GPIO this time, and then we are creating a bunch of LED objects and these are just the pins that I have attached each LED to,and after those LED objects are made, I am arranging them into a list with this annotation, so this list is called LEDs. After we create the GUI window, we've seen that before, we create an LED code variable which is a stringVAR, this is the variable that is coming from our text box here. So let's see how that text box is created, that's one that we haven't seen before. So we've got the button, we've got the exit button but this is the text box, and this is a widget called entry. So when you have an entry box, that is something that you can use to enter in a string, now there is no variable associated with this, so we are going to have to dig a little bit deeped to find out how that code is extracted.
Now this load LED code button is what drives the whole show right, so that is connected to a command called LEDShow, and that is really the only event function that we have here. So we have LED show and what we are doing is running code.get, so because we have code as the text box entry objuect we can just perform a get on that straight away we don't need a variable for it, in fact we create the variable here for instance. Once we have that variable, we just print that straight back out into the shell, we just print LED code and then we print the code entered, and you can see that I have been playing around and we have a few LED codes stacked up in the shell. Now this is where the program is actually doing its decision making, we want to step through that LED code string and check whether that character is a 1 or not a 1, in our case a Zero. I chose not a 1 so we won't have as many errors. So what we do here, is we first create a loop counter and set that to zero, that's essentially our LED number, remember the 0th element of a list is the first entry into that list. So we create a loop counter and then we have a for loop and we say For c in ledCode, and because ledCode is a string, that means that c is going to be a character. So it means, for every character in this string, and what we are going to do is we are going to check if that character is equal to 1. If it is, then we turn and LED on somehow; this is where the power of the list comes into play. Where we would normally be able to, lets say we wanted to turn led 7 on, so we would type led7.on, but now because we have a list of these led objects, we can say, LEDs, and then we access that index which is the Ith index, that's our loop counter and we are setting that to on. So using this notation we can essentially substitute LEDs for the Ith entry, that is led7. So this is like saying, led7.on while ever I is 0. Now if that character is not a 1, we say else, then we turn that led off, and then we increment our loop counter.
So we have the for loop, automatically stepping through the string and we have our loop counter matching it's position and toggling that LED based off the value of that character. Now the astute amongst you may have noticed that I am only doing an else statement, so instead I could say, abcd1111 and half the LEDs will still be lit, as we are comparing to 1. This is a cute demonstration of how you can use a text entry box to pass in more complicated information. Like if you want to issue a text command, if you build a GUI that runs off text commands this is perhaps a neat way that you could experiment with using text boxes.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.