Hi everyone i'm definitely new to arduino but have dabbled in electronics for a while. Anyway I'm trying to control a servo through the arduino over serial so I can build a robotic arm but am having trouble getting it to work. I have the control pin connected to analog in but I think it should be connected to a PWM if anyone could give me some code examples to put on the board and how to control the servo through the computer i would definitely be very greatful.
Some servo test code for use with the serial monitor.
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 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.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
readString=""; //empty for next input
}
}
http://www.arduino.cc/playground/ComponentLib/Servo
http://www.arduino.cc/playground/Learning/SingleServoExample
Thanks a ton Zoomkat, i've currently found another example from someone which is working pretty well currently i'm still tweaking
#include <Servo.h>
int minPulse1 = 0; // minimum servo position
int maxPulse1 = 180; // maximum servo position
int turnRate1 = 90; // servo turn rate increment (larger value, faster rate)
int minPulse2 = 0; // minimum servo position
int maxPulse2 = 180; // maximum servo position
int turnRate2 = 90; // servo turn rate increment (larger value, faster rate)
int buttonPin = 13; // pin that the trigger will be connected to
/** The Arduino will calculate these values for you **/
int centerServo1;
int centerServo2;
int pulseWidth1; // servo pulse width
int pulseWidth2; // servo pulse width
Servo servo1;
Servo servo2;
void setup() {
pinMode(buttonPin, OUTPUT);
servo1.attach(9);
servo2.attach(10);
centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2);
centerServo2 = maxPulse2 - ((maxPulse2 - minPulse2)/2);
pulseWidth1 = centerServo1;
pulseWidth2 = centerServo2;
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.println(" Arduino Serial Servo Control");
Serial.println("Press a, s, d, or w to move, spacebar to center, and f to fire");
Serial.println();
}
void loop() {
// check for serial input
if (Serial.available() > 0) {
int data = Serial.read(); // read the incoming byte:
digitalWrite(buttonPin, LOW); // turn the pin off on any incoming data
switch(data)
{
case 'a' : pulseWidth1 = pulseWidth1 - turnRate1; break;
case 'd' : pulseWidth1 = pulseWidth1 + turnRate1; break ;
case ' ' : pulseWidth1 = pulseWidth2 = centerServo1; break;
case 's' : pulseWidth2 = pulseWidth2 - turnRate1; break;
case 'w' : pulseWidth2 = pulseWidth2 + turnRate1; break ;
case 'f' : digitalWrite(buttonPin, HIGH); delay (1000); digitalWrite(buttonPin, LOW); break;
}
// stop servo pulse at min and max
if (pulseWidth1 > maxPulse1) { pulseWidth1 = maxPulse1; }
if (pulseWidth1 < minPulse1) { pulseWidth1 = minPulse1; }
// stop servo pulse at min and max
if (pulseWidth2 > maxPulse2) { pulseWidth2 = maxPulse2; }
if (pulseWidth2 < minPulse2) { pulseWidth2 = minPulse2; }
servo1.write(pulseWidth1);
servo2.write(pulseWidth2);
// print pulseWidth back to the Serial Monitor (uncomment to debug)
Serial.print("Servo 1: ");
Serial.print(pulseWidth1);
Serial.print(" Servo 2: ");
Serial.print(pulseWidth2);
Serial.println("degrees");
}
}
and thanks runaway_pancake I did look at those but was still a little unsure. I am still trying to figure out how to build a little program to use with this to make moving the servo much quicker than using serial monitor. Again thanks for your help.
Someone else's code that might be of interest.
/*I decided to post this for Beginners getting into robotics.
I don't have schematics so i will just have to describe.
This is a simple two servo arm. It uses four push buttons to control the servos. The circuitry is not complex, but if you don't know how to wire up the buttons or servos to your Arduino you can look up schematic reference that i learned from at http://ardx.org/circ04 for servo(s) and http://ardx.org/circ07 for buttons. *tip* I recommend if using an ARDX kit to "plug" the male wires directly into the female wires of the servo, they seem to keep a better connection that way. As for the construction of the Arm i used cardboard and coat hanger wire to add support. Be creative and design to your liking instead of following exact to some one else's, it helps you learn and build critical/problem solving skills. Now for the code, it is wrote to where it is easy to understand what is going on, not necessarily in the shortest amount of lines. although over all length is short anyway.
*/
// control code for simple two servo robotic arm
#include <Servo.h> // servo library, not needed but makes things a whole lot easier
const int buttonAL = 2; //button to make servo A go turn left ie counter clockwise
const int buttonAR = 4; //for servo A right
const int buttonBL = 11; //for servo B left
const int buttonBR = 12; //for servo B right
//to the above direction depends on how you set up your servos
int posA = 90; // sets start postion for servos
int posB = 90;
int valAL = 0; // sets values for button read
int valAR = 0;
int valBL = 0;
int valBR = 0;
Servo servoA; // claims servo use
Servo servoB;
void setup(){
servoA.attach(5); // selects pin to send servo signal
servoB.attach(10);
pinMode(buttonAL,INPUT); // sets mode of the buttons as input
pinMode(buttonAR,INPUT);
pinMode(buttonBL,INPUT);
pinMode(buttonBR,INPUT);
Serial.begin(9600);
}
void loop(){
valAL = digitalRead(buttonAL);
valAR = digitalRead(buttonAR);
valBL = digitalRead(buttonBL);
valBR = digitalRead(buttonBR);
if(valAL == LOW){
servoA.write(posA += 2); //posA can be replaced with a degree i.e. (posA = 25)
delay(40);
}else if(valAR == LOW){
servoA.write(posA -= 2); //posA can be replaced with a degree
delay(40);
}/*else{
servoA.write(posA = 90);
delay(40);
}*/
if(valBL == LOW){
servoB.write(posB += 2); //posB can be replaced with a degree i.e. (posB = 25)
delay(40);
}else if(valBR == LOW){
servoB.write(posB -= 2); //posB can be replaced with a degree
delay(40);
}/*else{
servoB.write(posB = 90);
delay(40);
}*/
}
Sorry to bring this thread back but I need help trying to the serial data (text) from the arduino in a program i'm using to control the board in VB. I would like to display the text from the arduino in a text box in the program. I really have next to no idea how to even start this, here is the code for my current VB control program:
Imports System.IO.Ports
Public Class MainControls
Sub Main()
Dim d As Integer
For d = 0 To 2
SerialPort1.Open()
TextBox1.Text = SerialPort1.ReadLine
Next d
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("a")
TextBox1.Text = SerialPort1.ReadLine
SerialPort1.Close()
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("d")
SerialPort1.Close()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
SerialPort1.Open()
SerialPort1.Write("w")
SerialPort1.Close()
End Sub
Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
SerialPort1.Open()
SerialPort1.Write("s")
SerialPort1.Close()
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.A Then
SerialPort1.Open()
SerialPort1.Write("a")
SerialPort1.Close()
End If
If e.KeyCode = Keys.D Then
SerialPort1.Open()
SerialPort1.Write("d")
SerialPort1.Close()
End If
If e.KeyCode = Keys.W Then
SerialPort1.Open()
SerialPort1.Write("w")
SerialPort1.Close()
End If
If e.KeyCode = Keys.S Then
SerialPort1.Open()
SerialPort1.Write("s")
SerialPort1.Close()
End If
If e.KeyCode = 37 Then '* Press left arrow key to send command
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End If
If e.KeyCode = 39 Then '* Press right arrow key to send command
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End If
If e.KeyCode = 38 Then '* Press up arrow key to send command
SerialPort1.Open()
SerialPort1.Write("+")
SerialPort1.Close()
End If
If e.KeyCode = 40 Then '* Press down arrow key to send command
SerialPort1.Open()
SerialPort1.Write("-")
SerialPort1.Close()
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
SerialPort1.Open()
SerialPort1.Write("+")
SerialPort1.Close()
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
SerialPort1.Open()
SerialPort1.Write("-")
SerialPort1.Close()
End Sub
End Class
Any help would be greatly appreciated