Controlling Servo with 1 button and Arduino - Need help.

Discussion in 'Embedded Systems and Microcontrollers' started by Device Unknown, Apr 11, 2016.

  1. Device Unknown

    Thread Starter New Member

    Apr 11, 2016
    3
    0
    Hey guys,
    I have been at this for some time and I just can not seem to get it. Programming never was my strong suit.
    I am simply trying to push a button and make a servo move from point A to B and stop. Then push button again and have it move from Point B back to point A.

    It seems so easy in theory and what sample code i get. But all the code I work with has 2 issues. 1) I have to old the button down for the servo to stay at point B, upon releasing it returns to A. and 2) the servo jitters.
    I have the servo powered externally and grounded to the Mega also. I also added the debouncing and delay to supposedly help with jitter.

    Any and all help would be greatly appreciated.
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,386
    496
    Is push button connected to digital pin or to ADC pin?
     
  3. MrSoftware

    Member

    Oct 29, 2013
    497
    122
    The jitter is probably due to the clock on the arduino not being stable. Does it jitter at points a and b, or just b?

    In your code set a flag that flip flops when you press the button. Something like (I'm trying to remember servo code from a couple years ago lol):

    Position = 100; // position a, 200 is position b

    When button pushed:

    Position = (position == 100 ? 200 : 100)

    To reduce jitter, do as little as possible in-between button pushes. Using processor power can cause variations in the pulses sent to the servo, causing jitter.
     
  4. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,677
    899
    What is the repeat rate (frame rate) for the servo pulse? The problem you describe can occur when the repeat rate is too high. It should be around 50 Hz (20 ms between repeats) for most inexpensive hobby servos. Some newer servos are different.

    John
     
  5. Device Unknown

    Thread Starter New Member

    Apr 11, 2016
    3
    0
  6. Device Unknown

    Thread Starter New Member

    Apr 11, 2016
    3
    0
    servo.jpg
    This is how I wired it. When I use this code...
    Code (C):
    1.  
    2. #include <Servo.h>
    3. int button = 8; //button pin, connect to ground to move servo
    4. int press = 0;
    5. Servo servo;
    6. boolean toggle = true;
    7.  
    8. void setup()
    9. {
    10.   pinMode(button, INPUT); //arduino monitor pin state
    11.   servo.attach(9); //pin for servo control signal
    12.   digitalWrite(8, HIGH); //enable pullups to make pin high
    13. }
    14.  
    15. void loop()
    16. {
    17.   press = digitalRead(button);
    18.   if (press == LOW)
    19.   {
    20.     if(toggle)
    21.     {
    22.       servo.write(160);
    23.       toggle = !toggle;
    24.     }
    25.     else
    26.     {
    27.       servo.write(20);
    28.       toggle = !toggle;
    29.     }
    30.   }
    31.   delay(500);  //delay for debounce
    32. }
    33.  
    It just starts sweeping.

    Moderators note: please use code tags for pieces of code
     
    Last edited by a moderator: Apr 12, 2016
  7. MrSoftware

    Member

    Oct 29, 2013
    497
    122
    Only read your button press when the state changes, not on every loop. When the button state changes, only toggle the servo position when the button change was to be pressed down.
     
  8. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
Loading...