Saturday, November 23, 2024

Week 5: Choose your own adventure!

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!


Everything was ready, I clicked the button.... and the stupid motor started spinning so fast both the paper and the wooden disc flew right off!  Okay, so now I have to figure out how to slow down the motor.

I had to go back to some of those functions I had previously deleted, because I remembered one of them said something about motor speeds.  Turns out you can control the motor, from 0 to 255, just like you'd kind of expect.  So all I really needed to do was maybe cut the motor speed by 75% and reassemble everything.

Of course the wooden disc was now 100% clogged with old glue, and I couldn't get the paper numbers off without ripping everything and potentially making the one wooden disc I had unusable.  So now I'm trying to rip out 25 year-old dried glue out of a tiny hole without ruining something glued to the other side of it.  After a toothpick and tiny Allen wrench failed, I found success using a tiny shrimp cocktail fork.  Getting everything reset took about an hour, but finally I was ready to try again.


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.

/*
SPINNING A MOTOR

  Transistor:
 
    The transistor has three pins. Looking at the flat side with the
    pins down, the order is COLLECTOR, BASE, EMITTER.
   
    Connect the black wire on the motor to the
    COLLECTOR pin on the transistor.

    Connect the BASE pin through a 330 Ohm resistor to
    digital pin 9.
   
    Connect the EMITTER pin to GND.
 
  Motor:

    You've already connected the black wire on the motor to the
    COLLECTOR pin on the transistor.
   
    Connect the other (red) wire on the motor to 5V.
 
  Flyback diode:

    When the motor is spinning and suddenly turned off, the
    magnetic field inside it collapses, generating a voltage spike.
    This can damage the transistor. To prevent this, we use a
    "flyback diode", which diverts the voltage spike away from
    the transistor.

    Connect the side of the diode with the band (cathode) to 5V
    Connect the other side of the diode (anode) to the black wire
    on the motor.
*/

// We'll be controlling the motor from pin 9.
const int motorPin = 9;

int randomNumber;
int buttonPin = 2;
int buttonState = 0;

void setup(){
 
  // Set up the motor pin to be an output:
  pinMode(motorPin, OUTPUT);
  pinMode(buttonPin, INPUT);
 
  Serial.begin(9600);
  randomSeed(analogRead(0));

} // end setup()

void loop(){
 
  buttonState = digitalRead(buttonPin);
 
  if (buttonState == HIGH) {
     Serial.println("Button state is 1");  
      motorOnThenOff();      
  } //end if

} // end loop

void motorOnThenOff(){
  int onTime;  // milliseconds to turn the motor on
  int offTime; // milliseconds to turn the motor off
 
  randomNumber = random(10,41); // Generate a random # from 10 to 40
  Serial.println(randomNumber);  

  onTime = randomNumber*100;

  Serial.println("On");  
  analogWrite(motorPin, 185);   // turn the motor on (full speed)
  delay(onTime);                // delay for onTime milliseconds

  Serial.println("Off");        // Don't actually need this functionality
  analogWrite(motorPin, 0);     // - just leaving it in there in case I want it later
  delay(offTime);               //   and it doesn't hurt anything

} // end motorOnThenOff

No comments:

Post a Comment

Nooooo, don't make me reflect on my learning!

 ... I just want to stick wires in breadboards and make froggies dance!!! Okay, but seriously, that stupid dancing frog was clearly my most ...