If I press any of these buttons. I press this button goes that way and if I press any of the other buttons it actually basically starts from wherever I press. We need to be able to read eight buttons and to write eight LEDs and that normally takes 16 pins. I only have connected here, two of the Arduino pins basically used two pins, a five and a four and the ground, and the power and that’s also ground and that’s it so basically ground power, and these two wires connected to the io expander that we talked about In the last video, but unlike in the last video now there’s, only one and it’s doing both input and output at the same time, which is pretty cool, so here’s the diagram. There are eight of these, of course, but I only dropped one they’re, all the same. Let’S talk about the two scenarios. One of them needs to read the button and the other one is to actually turn on the LED let’s turn on the LED first that’s simpler. Normally, all these pins will be all high, so it will be a plus here and a plus there, and therefore the LED will not let up because they’re balls plus to turn on one of these LEDs. What you would do in the code is you set the desired LED bit to be low, and so, when this is low, now all of a sudden, we have the current going through here through the LED and lit up so that’s how you would actually turn on Any of these LEDs by putting the particular bit one of those eight bits low now to actually read the button, you would first set the actual bits too high.
This is still will not lit up, but now it could detect. When you press this button, we will be bringing that pin low to ground and it will be able to detect. It is now low. So let’s look at the code so here’s the code, maybe it’s about two screenful. It shouldn’t take too long to go through since our io expander is a nice quality device. We need a wire library. I do not know why they call it wire and certify square C library, but that’s what we need I’m going to paste it here to notepad plus plus. So I could magnify the code like this so that’s, the library we need to talk to our io expander, which is on the I square C bus. We need some variable to keep track of what is the actual address of our I Square C device and I’m going to skip these for now and I’m going to go down here to the setup we need to initialize the wire library and since I’m, on the Nano I can take the default io port for I Square C, which is a 4 and a 5 and here’s the address of our I Square C device. The IOA expander is at address 20 next let’s talk about the two functions. I wrote to make life easier to communicate with the io expander. One of them is to write data to the io X vendor the other one is going to be to read data from so let’s talk about the write first, so you just tell it what pattern you want to display on these LEDs.
The first thing we do is we tell the wire library that we’re about to send something at that I Square C address, and then we actually send their bits and then we just say we’re done. This is going the other way. Instead of writing we’re about to read something from these switches, this is something specific to the io expander. It requires to actually set all the outputs to one before we can do a read and then we actually start doing the read and to do the rate is very similar, so we say we are about to communicate with that address and now we are Wes. Some data from that address and we tell it how many bytes we want. So when the data arrive, we put it into this variable and then we’re done. We basically return that variable onto whoever call us now that we know how to read and write to our expander let’s. Look at our loop loop is pretty simple. We just keep on reading those switches as long as nobody press any other switches. All these bits will be one remember, because when we press the button we’ll bring that to ground. So if nobody pressed that all these bits will be all ones. So as long as it is one we don’t want to do anything, but as soon as it is not all one, we do something now that we know that somebody press one of these switches. We need to figure out which one of those switches is actually press and to do that.
I just basically loop through bit index 0 through 7 I’ll check this bit and then I’ll check that bit and so on and so forth. Until I reach this one and for every one of those I just say, look at the bits that we got from the io expander and check that particular index, so bit index will be starting with 0. So we’ll check is this bit zero? Meaning did somebody press that button? If not we continue on, is? Is this bit zero, meaning somebody press that switch and so on and so forth until we find one that actually is low, which means that’s the one that’s being pressed and then when that happens, we know the index 0. 1. 2. 3 let’s say we press this. One we know that it’s bit index 3, that somebody press – and we just passed that bit index 3 to the explosion, so the animation could start doing the animation. From that point to the left and to the right, which we will see next so let’s say we pass in a 3 here, so that’s a start edge is going to be 3. What we’re doing basically is we’re trying to figure out something to its left and to its right and just continue on on both direction so to do that loop. I have basically this I going from 0 through 7 and it’s going to go both direction. We’Ll see here in a second, so on the lower side we say take wherever you’re starting and subtract 0 originally, which means it’s going to start wherever it was, and then the higher side we add 0, which is going to be whatever it was.
If so, you’ll stay at 3 and these two, if statement here, make sure that we are not trying to set a bit that is like outside. That is greater than the number of bits we have here or less than number of bits. So as long as we’re within the range we’re actually going to clear that bit, remember we’re going to assume that all the bits is going to be set to high and high actually means that they are off. Remember with this is plus, and that plus means off. So to turn the LED on, we actually want to clear the bit. So originally we only clear the bit. That is the one that is passed in here. So that will be three. This will be three BB 3, so we clear BIP 3 and we write it to the io expander. So bit 3 is the only one though it’s going to be low because that’s you only been cleared, all the other ones are still high, and so bit B is the only one that’s going to be turned on and then the next time around within this loop Here I will be close to one so now we are going to subtract one from this one we’re going to add one from that one. So basically, instead of three we’re going to have one of them setting this one there one setting that one again, we start with all of them being one meaning none of our on and we clear the bit for and bit two and that will turn on those Two LEDs and all the other ones will be off, and then we go on and so on and so forth, that’s yet bit five and that one will be on and not know the other ones will be off, because every time we go through a loop, we Set the bits all to back to one here and then after we set a bit, we actually wait here if we clear them right away.
We can’t see it. So we wait for 90 milliseconds, so we could actually see that and then we clear the display, meaning set them all back to high meaning turn them all off, and we just do this over and over again until we do this seven times guaranteeing that if we Start from here we hit or that one or we start from here we have that one if we start on the middle. Actually we did not have to go all the way to seven, but it doesn’t matter, because we have this excitement over here. That checks make sure that we do not set a bit that is beyond our boundary but that’s everything. So I was here to code in a youtube description. You can check it out and download and play with it. It’S been a fun project. I think it’s. So cool to be able to deal with basically sixteen io pins with just the two wires.
arduino 74hc165 Video
arduino 74hc165 news
Posted on Tuesday May 27, 2014The Wormhole Actualization Machine Boing Boing … Continue Reading »
arduino 74hc165 Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr
Originally posted 2017-08-01 06:25:21.