Using a Servo to Open/Close a Computer Side Panel!

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Belial88
 
Posts: 73
Joined: Tue Sep 17, 2013 4:42 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Belial88 »

That didn't change it ;/

Image

It seems less like it's a connection issue. You would think if it was a connection issue, and I jiggle it, sometimes it wouldnt move at all and then suddenly just smoothly keep moving and then stop. It's like every jiggle = small 1* move. And only in one direction too, at which point I have to pull it back.

Could it be the servo or trinket?

edit: it does this no matter what pin the signal is connected (or not connected) to.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by adafruit_support_bill »

It could be a connection issue, a power issue, a noise issue or some combination of the above. If jiggling affects it in any way, the connectivity is definitely part of the problem.

Servos are also (electrically) noisy and power hungry little devices. The startup current can be enough to cause a 'brownout' that resets the processor. Commutator brush noise can feed back through the power wires and cause erratic behavior also. You might try powering the servo from a different source to see if that changes things at all (be sure to connect the grounds of your two power sources together.

Belial88
 
Posts: 73
Joined: Tue Sep 17, 2013 4:42 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Belial88 »

yea but when i upload the button or pot programs jiggling it isn't necessary, it just works.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by adafruit_support_bill »

But if you don't jiggle it with your program it never moves?

Belial88
 
Posts: 73
Joined: Tue Sep 17, 2013 4:42 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Belial88 »

Yea if I jiggle with pot/button or sweep program, it jerks around a bit.

But I just hold the connnections taut (simulating that foam mod, I suppose) there's no jerking, and it reacts every time I hit the button (it just doesnt move more than ~20* is the problem). With the sweep program nothing happens if the connections are on tight, and it only moves if I do jiggling, even with the signal pin removed.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by adafruit_support_bill »

Do you have another servo to try? You did mention earlier that you smelled smoke. It is possible that something internal is burnt.

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by 1chicagodave »

Belial88 wrote:Yea if I jiggle with pot/button or sweep program, it jerks around a bit.
Yeah...jerking around, or sporadic response, when 'jiggling' is typically a sign of loose connection(s) somewhere.
... (it just doesnt move more than ~20* is the problem). With the sweep program nothing happens if the connections are on tight, and it only moves if I do jiggling, even with the signal pin removed.
For what it's worth, I think I was able to somewhat duplicate your problem (sans jiggling part). I loaded up example sketch ("TrinketKnob") with Trinket set at 16mhz and....forget about it. It only moved about 20°-30°. I've never had much luck with Trinket at 16mhz; I can't get NeoPixels to work smoothly that way either. So, I set it to 8mhz and tried again. This time it worked smoothly, but....only traveled 90° total, from 0V to 5V.
It's a TowerPro SG-5010
Here's a video of that - http://youtu.be/YysPJymzU6U

I hooked up my DSO Nano to check the servo signal.
According to the tutorial, the period of the servo signal should be 20ms.
Tutorial pulse width
Tutorial pulse width
image.jpg (11.58 KiB) Viewed 1053 times
But, when I measured with my DSO... It says it's 1.1ms.
DSO Nano period
DSO Nano period
image.jpg (253.1 KiB) Viewed 1053 times
(Sorry it's difficult to see. When I try saving the BMPs straight from device they keep getting corrupted.)
Perhaps the jiggling without solder connections is somehow accidentally stumbling upon the proper timing....? Probably not, but I thought it was a fun theory. :P
***EDIT: Nevermind! I just noticed that I had grid size set to 10ms & the period covers two grid squares which IS 20ms. There must be something wrong with the DSO firmware — the yellow number in bottom left is incorrect. :?

It's been a few months since I played with my servos. But, I seem to remember having to use three conditions (instead of just the pin number) with the attachServo() function in order to adjust the pulse width because the default pulse width settings were giving me 'jerky' movement & less than maximum range of movement.

I know none of this solves the problem, but more information is always better....and is usually helpful.

Belial88
 
Posts: 73
Joined: Tue Sep 17, 2013 4:42 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Belial88 »

Bought another servo, a micro-servo from radio shack. Works ;/

Pin connections were never the issue, though I do have to hold them tight like to simulate that foam thing. However, a few things:

1. I get about 150* of movement, I have to set it to 0 - 250 degrees in the code. Kinda odd, youd think I'd have to set like 0-170to180 or something.
2. Is there a way to slow down the servo movement speed? I know with the sweep command you can slow it down. I looked at the sweep code to see if I could copy/paste it but it's an if/then statement and if servo is before X position, than it moves Y degrees (which is how you determine it's speed). I don't know how to make the servo slower. At the current rate, it's too fast, it'd swing the door way too fast especially since I need to mount it so close to the hinge.

Thanks guys.

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Franklin97355 »

You could use a loop that moves the servo from the start position to the end position in smaller steps with a pause in each step to slow it down.

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by 1chicagodave »

Belial88 wrote:Bought another servo, a micro-servo from radio shack. Works ;/

Pin connections were never the issue, though I do have to hold them tight like to simulate that foam thing. However, a few things:

1. I get about 150* of movement, I have to set it to 0 - 250 degrees in the code. Kinda odd, youd think I'd have to set like 0-170to180 or something.
2. Is there a way to slow down the servo movement speed? I know with the sweep command you can slow it down. I looked at the sweep code to see if I could copy/paste it but it's an if/then statement and if servo is before X position, than it moves Y degrees (which is how you determine it's speed). I don't know how to make the servo slower. At the current rate, it's too fast, it'd swing the door way too fast especially since I need to mount it so close to the hinge.

Thanks guys.
Servos typical only have a range of 150° to 170°, unless it's a constant rotation servo (like for robot wheels).

The "Sweep" code has a range of movement coded in, no? I think it's 0-180....or similar?
Find that, adjust it to the range you want, and copy that with all supporting code into your sketch.
...then again, I might be making that up. I can take a closer look later & maybe find exacts. (I'm just taking a break from writing a presentation right now :roll: )

Belial88
 
Posts: 73
Joined: Tue Sep 17, 2013 4:42 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Belial88 »

I can't get sweep to do anything besides making this weird clicking sound that sounds like breaking.

It appears that what is going on, is it sweeps clockwise, and then stops and just keeps clicking. It's like it doesn't realize to turn the other way because it's still trying to continue before realizing it needs to bounce. I've replaced the 2 180's in the code with like 100 and no change.

It also doesnt appear to matter where my signal is plugged in when doing this, or if it's even plugged in at all. I've tried changing the pin to 1 and 2.

Code: Select all

#include <Adafruit_SoftServo.h>

// Sweep
// by BANNED <http://BANNED.com> 
// This example code is in the public domain.


Adafruit_SoftServo myServo; //create a servo object 

 
int pos = 0;    // variable to store the servo position  
 
void setup() 
{ 
  myServo .attach(2);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 
on a side note, is there no way to pm anyone? Evertime I submit a message it takes me to a really obnoxious "Thanks, your message has been submitted! view your submitted message, go back to index' page. There's no quick reply button. Editing always has to be taken to a dedicated page. And it never saves me being logged in, every time i come back i have to relog in (it's not like logging out right away, i mean if i leave for a few hours). ive never had this on any other forum.

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Franklin97355 »


Belial88
 
Posts: 73
Joined: Tue Sep 17, 2013 4:42 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Belial88 »

Tried what exactly?

Belial88
 
Posts: 73
Joined: Tue Sep 17, 2013 4:42 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by Belial88 »

Okay I think my other servo broke. It just moves all the way to the right and clicks. It's like the pot is messed up.

I'm going to go to a local hobby shop, with the arduino + batteries, and see if I can test out servos and stuff. The code will be preloaded onto the trinket so I can see if it works there and all. Find a servo with a full 180* throw would be nice.

Also, could I run the trinket off an internal USB header, and battery pack? I dont know if I'd have to wire the servo ~5v line to both usb and battery at same time, or just battery and it'll know to go usb when it's on, assuming this could be done. That would be really awesome, that'd be the whole Battery Pack as back-up power solution without having to buy anything! Really, I'd actually run it off the 5VSB line if this was possible (maybe voltage/grnd to 5vsb, and then data lines to internal usb headers!).

Someone gave me the following code, in order to slow down the movement of the open/close. The only issue is that if you hit the button during an open/close, it'll quickly finish the command, and then it'll start going the other way slowly. It'd be better if you couldn't interrupt an open/close movement, but what would be best is if you issued a command during one, it just reversed direction at the same, slower speed.

Code: Select all

#include <Adafruit_SoftServo.h> // SoftwareServo (works on non PWM pins)

//declare the constants to be used
#define LEDPIN 1            //pin attached to led
#define servoPIN 2          //Servo control line (orange) on Trinket Pin #2
#define SWitchPIN 3         //input from N.O. momentary switch

#define posOpenCMD 170      //command in deg to servo to open the door
#define posCloseCMD 10      //command in deg to servo to close the door
#define DBdelay 100         //delay in ms btw readings of the switch
#define doorTime 5000       //time in ms btw opening and closing of door
#define MAXcnt 3            //need this many same consecutive readings of button pressed

//declare the variables used
boolean doorOPEN = false;   //desired door state set to closed
boolean SWstate = true;     //state of switch, open = TRUE = not pushed
byte SWcnt = 0;             //counter of same consecutive switch readings w/button = pressed
int STEPcnt = 0;            //counter for stepping door command from open - closed
int STEPsize = 0;           //size of step for door command
int posCMD = 0;             //servo position command in deg

Adafruit_SoftServo myServo; //create a servo object

void setup() {
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;               // any number is OK
  TIMSK |= _BV(OCIE0A);     // Turn on the compare interrupt (below!)

  //set the pins to be ins or outs
  pinMode(LEDPIN, OUTPUT);
  digitalWrite(LEDPIN, LOW);  //turn LED off
  pinMode(servoPIN, OUTPUT);
  pinMode(SWitchPIN, INPUT_PULLUP);
  myServo.attach(servoPIN);   // Attach the servo on Trinket
  
  //compute the step size used by the servo to open/close the door
  STEPsize = max(1,(posOpenCMD - posCloseCMD)/(doorTime/DBdelay));
}

void loop() {
  //read the switch
  if(digitalRead(SWitchPIN) == LOW){   //see if switch is being pushed
    SWcnt ++;                //SW reads low = pushed, increment debounce counter
    if(SWcnt >= MAXcnt){     //test if switch is really being pushed AND done bouncing
      SWcnt = 0;             //done bouncing, reset counter for next push
      doorOPEN = !doorOPEN;  //reverse desired door state
      STEPcnt = 0;           //zero out position step counter
    }
  } 
  else {
    SWcnt = 0;              //switch is released or bouncing
  }
  //now compute servo command based on desired door state
  if(doorOPEN == true){
    digitalWrite(LEDPIN, HIGH);      //turn LED on
    posCMD = max(0,(posCloseCMD + STEPcnt*STEPsize));   //compute position, prevent rollover
    posCMD = min(posOpenCMD, posCMD); //limit max command to door open value
    myServo.write(posCMD);       //tell servo to go to open position, step by step
  } 
  else {
    digitalWrite(LEDPIN, LOW);       //turn LED off
    posCMD = max(0,(posOpenCMD - STEPcnt*STEPsize));   //compute position, prevent rollunder
    posCMD = max(posCloseCMD, posCMD); //limit min command to door closed value
    myServo.write(posCMD);      //tell servo to go to closed position, step by step
  }
  STEPcnt = min(10000, STEPcnt++);   //increment step count but prevent rollover
  delay(DBdelay);           //wait for next read of switch
}

// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
  // this gets called every 2 milliseconds
  counter += 2;
  // every 20 milliseconds, refresh the servos!
  if (counter >= 20) {
    counter = 0;
    myServo.refresh();
  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Using a Servo to Open/Close a Computer Side Panel!

Post by adafruit_support_bill »

Also, could I run the trinket off an internal USB header, and battery pack? I dont know if I'd have to wire the servo ~5v line to both usb and battery at same time, or just battery and it'll know to go usb when it's on, assuming this could be done.
Not a good idea to connect the battery directly in parallel with your 5v supply. This is especially dangerous if you are using LiPo batteries since they can overheat and go up in flames. You really need a charger to safely charge your cells. Our LiPo chargers will let you run a load while charging the battery.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”