Friday, November 29, 2024

Week 6: Flipping the Blinds

I was happy that I struggled so much with servos last week, because this week I knew going in that the servo wasn't going to go more than 180 degrees.  With that in mind, I started thinking about how to physically open and close the blinds using the servo.  I didn't use the photoresistor this time because (a) I'd already used it on past projects and felt comfortable with it, and (b) my kit actually didn't come with one, and I had only had access to one because of a kit I'd checked out from our library... which I'd already returned.

That said, I decided just to stick with the push button and focus on the servo and the physical aspects of getting the blinds to move.  My house has the blinds like these where you can twist the rod to tilt the blinds or pull the string to make them go up and down.  

I didn't think I could get the mechanics of the "up and down" part because for down you have to pull the cord off to the side and that seemed really complicated so I decided to go with the twisting part.  I tested it and found that the rod needed to rotate seven times in order to go from open to closed.  That was definitely going to be an issue with having a servo that could only go 180 degrees, but I thought maybe not insurmountable if I just used gear ratios.


Business as usual: Tinkercad

As per my standard operating procedure, I set up my model on Tinkercad, and got things working:


The code was relatively straightforward as well, since I already had programmed the button and had programmed a servo before.  But for some reason, I absolutely could not get the thing to work.  A seemingly-infinite amount of time later, I finally realized I was missing a reference.  I had this code:

servoBase.attach(A1);

and it should've been this code:

 servoBase.attach(servoPin);

So that was an hour of my life I wasn't getting back!  I now had working code and everything was going to be perfect.  I had it set up so that each time I pushed the button the servo either ran 180 degrees clockwise or 180 degrees counterclockwise (opening or closing the window).

#include <Servo.h>

Servo servoBase;
// We'll be controlling the servo from pin A1.
const int servoPin = 10;

int buttonPin = 2;
int buttonState = 0;
int goCW = 1;

void setup(){
 
  pinMode(buttonPin, INPUT);
 
  servoBase.attach(servoPin);
  servoBase.write(0);  

  Serial.begin(9600);

} // end setup()

void loop(){
 
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    Serial.println("Button Activated");  
    if (goCW == 1){    
      goCW = 0;
      servoCW();      
    } else {
      goCW = 1;
      servoCCW();
    } // end CW or CCW
  } //end if

} // end loop

void servoCW(){
  Serial.println("In Servo CW");  

  for(int i=0; i<=180; i=i+10)  {
   Serial.println(i);  
   servoBase.write(i);
   delay(200);
  }
 
} // end servoCW

void servoCCW(){
  Serial.println("In Servo CCW");  

  for(int i=180; i>=0; i=i-10)  {
   Serial.println(i);  
   servoBase.write(i);
   delay(200);
  }
 
} // end servoCCW

Next Problem: Nothing Uploads

Now being ready to upload the code to my breadboard I ran into a terrifying problem: the code would not upload to the breadboard.  Just wouldn't do it.  It compiled, everything was great, but then it just would not copy over to the Arduino.  

Clearly I'm just dumb and didn't connect things well.  Take out USB. Reconnect USB.  Try again.  No.

Clearly I just need to close the Arduino IDE and open it again.  Nope.

Clearly I need to the close the Arduino IDE, take out the USB, reconnect, the USB, then open the IDE again.  Keep trying, sweetie.

Okay clearly I need to do all that, plus restart my computer.  No dice.

Okay must just be this code.  Let's try uploading code from last week because we know that works.  But no, it doesn't!   Terror grips my soul.

Now we hit the forums.


I was able to confirm my Arduino board wasn't "fried" (thank all that is holy), but then resetting it still didn't actually help.  The next step on the forum was terrifying:

Okay so that was fun.


Good news is, it worked!  Now the code uploaded and I could breathe again. Everything's good from here on out, right?  Right?????

Pretend this isn't a problem: backwards button reading

For some reason, even though I hadn't changed anything else related to the button wiring in my code or on my breadboard, the button state was reading high even when it wasn't pressed (and reading low when it was pressed).  

I tried using ChatGPT, by giving it my code and asking what might be going on:


It gave me quite a few ideas for troubleshooting:



I attempted all the suggestions, including this one:

Change pinMode(buttonPin, INPUT); to pinMode(buttonPin, INPUT_PULLUP);

That still didn't work, so I changed the code to look for LOW instead of HIGH and just moved on.


Let's do this: The gear setup

It was time to pull out the Legos and some 3D printed gears I had lying around.  First, I made a little holder for the servo.


I got everything wired up and put together the servo system.  Success!


And because the Karma Gods are fickle, the screw that came with the box for the servo wasn't long enough to go through the gear and the servo.  Luckily I found a screw with the same diameter and just a little longer!


I put the gear on the servo, and then put together another two gears so we could get a better gear ratio (180 going to 270).  It wasn't great, but it looked like an okay-enough set up.


Now confident I could do this, I set out to start adding pairs of gears until I got my gear ratio to make the full 2520 degree (7 revolution) setup.




Things looked good - time to make it spin!


And... this is where the dream died.  I realized couldn't get the precision on the gears, and that even if I could, I wouldn't be able to get enough power on the servo to make all those rotations.

So all that work just to discover another way to not make a lightbulb...

I had one more trick up my sleeve.

Let's do this (again): The moment arm setup

My son's apartment has different kinds of blinds, that instead of a rotation just use a string pull to made the blades rotate.  I thought I might have a chance.


I got a paint stirring stick, drilled a hole in the middle to attach to the servo and drilled two larger holes on each end for the strings of the blinds.  If the servo could do the 180 degree rotation with each string on each side, it would fully open or close the blinds.


Now let's see how this turned out...



So essentially, the servo that came with the kit just wasn't strong enough to handle either set of blinds in my house.  I do feel like I have a "proof of concept" in that if I got a stronger servo, I might have been able to make this happen!  

(As I was lamenting my struggles, my father-in-law suggested a real-world application related to toilet paper dispensing, which gave me giggles and lifted my spirits!)


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 ...