I have an Arduino powering a small 180 degree Parallax servo that is used to control the throttle link for a small engine. I have code right now that will allow me to open/close the throttle (in either 1/4 or 1/5 increments) using the keyboard of my computer. This is well and fine but I really want to get it working so that when I press (hold) the hot key "t" the throttle will open but when I release the hot key I want the servo to cycle back to its original position (where the engine idles). If you have suggestions I would really appreciate them.

I am using Putty (Putty.org) to interface with the serial monitor so the arduino knows when 't' is pressed.

My Arduino code is just below this bit of wording (55 deg and 0 deg are the bounds of my throttle link, 55 is closed throttle (idle) and 0 is open throttle):

#include <Servo.h> 

Servo servo;

int pos = 55;  // Stores the position (angle) of the servo. Range is [0, 180].

void setup() 
  servo.attach(9);  // Attaches the servo on pin 9 to the servo object.
  servo.write(55);  // Resets the position.

void loop() 
  if (Serial.available())   // Returns true if there is serial input.
    char ch = Serial.read();

    if (ch == 't') 

      {if (pos <= 55) 
        {pos -= 11.25 ;}}


      {if (pos >= 0) 
        {pos += 11.25 ;}}

    // Now ask the servo to move to that position.
    // Mechnical limitation to the frequency of commands given.


1 Answer 1


If your terminal program uses key-repeat, then let each 't' operate the throttle for just slightly longer than the repeat period (slightly longer than twice the period, if you want to tolerate single dropped characters). After that period without another 't', close the throttle. One implementation would be to (re-)start a countdown timer every time you see a 't', and close the throttle when the timer goes to zero.

No auto-repeat? You'll have to simulate it with your finger (and code for a somewhat longer repeat interval, of course).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.