This one was fun! I decided I wanted to use the random number generator to make a roulette wheel style spinner (with only a few numbers, not all of them!). I thought I could use the servo motor, because apparently you can tell that exactly where to go. My theory was to generate the random number in the code, then spin the servo a little bit for show, and then have it rotate to the right number on the board. Since it already kind of had a "pointer," I figured it wouldn't be too bad.
I got the servo out, and realized it had to be assembled. I felt like kind of a pro at this point, because I got to use a screwdriver and that felt Real (as the kids these days would say).
First of all, it took a while to pinpoint exactly which servo we had in the box, but I finally found the right one and found a wiring diagram:
But no matter what I did, I couldn't get it to actually spin. Didn't even get to the random number part, I just kept going back and forth trying to get it to spin. (This was all on Tinkercad.) I thought maybe the simulation software just wasn't great, so I tried wiring it up for real, but it still wasn't working. A few more hours on Google, and I came across this gem:
Apparently they can't do a 360 degree spin at all and I was working with the wrong hardware to begin with! So yeah, that was a fun afternoon...
Okay let's try something else. There was also a motor in the kit, but that seemed a little scary. Motors spin fast, and they have all kinds of extra things you have to put on the board to make it work. I did see a motor project in the book though, and found the related code in the zip file, so I decided to give it a shot. (If you're interested, read the comments in the code about how the extra parts work, it's kind of crazy how much extra stuff is going on at an electrical level that we can just wire up by pattern matching and move on.)
I decided to forego the idea of getting it to go to a specific number, and just wanted to get the motor to spin. There were a lot of extra functions in the provided code, so I deleted all but the "spin the motor just normally" one, and used what we learned previous to wire it up to go on a button press. But still in Tinkercad it wasn't working great. Again, out of desperation, I decided to forego the model and just wire it up for real, expecting to be yet again disappointed. But no! It actually worked! Success!!!!!
Now we're cookin' with gas!
The next step was to build something to hold the motor so it could spin. Out came the Legos. I also wanted to attach the number circle on the motor, so I needed a little base to glue onto the motor.
Now comes out the hot glue gun that I've used about four times in the last twenty years, and a misleadingly-short-and-easy amount of time later, my device was ready to go!
I put everything together, set the motor really low, and.... Nothing Happened. I knocked it down about 50%, thinking that would probably be enough, but still worried it was too high. Not only was it not too high, it didn't even spin. I could hear the motor trying to engage, but it didn't move. Oh, no, I broke the motor! Okay, maybe I didn't, but let's just try increasing the number a little bit.
Turns out that the motor isn't spinning on a power of 0 to 255. It's actually spinning on a power from <whatever gets it going> to 255. And with the weight put on the motor, this guy wasn't going to start spinning until it had a power over at least 160. I was really wary of it going too fast, though, so I didn't experiment too much - basically increased the power until I felt it was "just enough" to give it a decent spin.
With that success, I wanted to put in a bit of randomness, so I also put in a spin timer based on a random number, so the motor would spin anywhere from 1 to 4 seconds. (I actually generated a number between 10 and 40 and multiplied by 100 instead of 1 to 4 and multiplying by 1000 so I'd get a little bit more variability.)
Here is the final version of the code. Comments at the top came from the project documentation (I deleted a bunch of the comments and just left the ones in about the hardware). I could definitely see real-life extensions, even with just board games, so this was really fun to put together.




No comments:
Post a Comment