Can someone tell me how to control a servo with serial inputs from the keyboard
Like :
If i click
1(On the keyboard) then the servo should go 30 degrees to the left.
2(On the keyboard) then the servo should go 30 degrees to the right.
So that if I enter "111" and click "Enter" , then
the servo goes 90 degrees to the left
So that if I enter "11" and click "Enter" , then
the servo goes 60 degrees to the left
So that if I enter "1111" and click "Enter" , then
the servo goes 120 degrees to the left
and so on...
And the servo should not rotate back when it has gone to maximum of 180 degrees or minimum of 0 degrees, until and unless I rotate it by clicking either "1" or "2".
This is for making a turret in my RC car with a LED.I place the LED on the Servo and rotate the servo and flash the LED once to shoot.
Can someone tell me how to control a servo with serial inputs from the keyboard Like :
You will need to write an application for your computer to send your keyboard commands via the serial port to the arduino, and load code on your arduino to decode the commands into servo commands.
Below is some simple code that uses the serial monitor to control a servo. You are on your own to write an application for your pc to read keyboard presses.
// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n;
char carray[6]; //converting string to number
readString.toCharArray(carray, sizeof(carray));
n = atoi(carray);
myservo.writeMicroseconds(n); // for microseconds
//myservo.write(n); //for degees 0-180
readString="";
}
}
Is there an easier method because this is too complicated for me?Or maybe is there a tutorial or something that will tell me about these stuff.
Could you give me the most basic code of :
How to rotate the servo to 45 degrees from 0, then delay 1 second and rotate back to 0 degrees?
Please try and give me that simple code as then later on I will derive the code I need from that basic code....
Could you give me the most basic code of :
How to rotate the servo to 45 degrees from 0, then delay 1 second and rotate back to 0 degrees?
#include <Servo.h>
const int servoPin = 2;
Servo myServo;
void setup ()
{
myServo.write (0);
myServo.attach (servoPin);
delay (1000); // give the servo time to reach 0 "degrees".
}
void loop ()
{
myServo.write (45); // send the servo to 45 "degrees"
delay (1000); // wait a second
myServo.write (0); // send it back to zero
for (;;); // do nothing for the rest of time.
}
Thanks a lot AWOL I used that basic code and made my program and the LED turret works perfectly.
The part of the code I was looking for is "myservo.write(45)"
Thanks again
and if this is wrong could you please give me the right code for that one step above.
I just need to create something that can read my servo's position so that when I press a key on the keyboard and increment of 20 degrees is added to the present servo location.