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()
{
Serial.begin(9600);
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 ;}}
else
{if (pos >= 0)
{pos += 11.25 ;}}
// Now ask the servo to move to that position.
servo.write(pos);
// Mechnical limitation to the frequency of commands given.
delay(15);
}
}