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 ARDX: Kumpulan Situs MPO Paling Gacor Anti Rungkad Terbaru 2023 for servo(s) and ARDX: Kumpulan Situs MPO Paling Gacor Anti Rungkad Terbaru 2023 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);
}*/
}
I encourage all beginners to work with or change code. experiment!, and good luck!
i need information on how to control servo using rxtx pins of the arduino in a simple coding for my robotic arm..when i use serial to LED communication, it runs perfectly but when i attach to a servo, it gives out a weird output.
What I mean is '10' is a multicharacter constant, not a string, and it consists of two ASCII values '1' (in hex 0x31) and '0' (0x31). It is a perfectly acceptable construct in C.
These two characters will fit into an Arduino "int" (16 bits) and you can use such a value in comparisons or "case" statements as you did.
However, you have not provided a means to read more than one character and put the result into an "int", so a single character can never trigger case '10':
can someone help me recode my code???im not getting any results, even for just one servo port via serial comm. i dont get it, why i can let an LED be high but moving a servo, i cant..help pls! tnx!
can someone help me recode my code???im not getting any results, even for just one servo port via serial comm. i dont get it, why i can let an LED be high but moving a servo, i cant..help pls! tnx!
Some servo test code I use for testing two servos:
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
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); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
servo2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}
}
thank you zoomkat! actually i have read these posts of yours, but im bothered on what i am to send at your code in order for me to trigger a specific servo.. im not going to be sending servo positions, il only be sending a specific character then my receiver arduino would process it.
what i am to send at your code in order for me to trigger a specific servo.. im not going to be sending servo positions, il only be sending a specific character then my receiver arduino would process it.
I would think you could use a series of "if" or "case" statements to evaluate the character received by the arduino, and send a value to the appropriate servo based on the received character.
can someone help me recode my code???im not getting any results, even for just one servo port via serial comm. i dont get it, why i can let an LED be high but moving a servo, i cant..help pls! tnx!
Some servo test code I use for testing two servos:
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
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); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);