Go Down

Topic: Beginner's Robotic Arm (Read 7216 times) previous topic - next topic

williamkoepp

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.
Code: [Select]
// 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,Robot."
"Well I,Tarzan!"

pao_wikhan

what would be the use of your Serial.begin function???

AWOL

Quote
what would be the use of your Serial.begin function?

http://arduino.cc/en/Serial/Begin
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

pao_wikhan

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.

retrolefty

Quote
it gives out a weird output.


Only if you post your complete sketch that gives the "weird output" can we help you.

Use the # edit button to place your code in proper display format in a posting

Lefty

pao_wikhan

this was my sketch for my robotic arm.im using 3 servos and 1 dc motor also for the grip would be a pneumatic cylinder.

Code: [Select]


#include <Servo.h>

Servo sh; //servo for shoulder
Servo el; //servo for elbow
Servo wr; //servo for wrist
int incomingByte = 0;
int shpos = 97;
int elpos = 100;
int wrpos = 180;
char s;
char w;


void setup() {
    pinMode(2,OUTPUT);   //base turn left pulse
    pinMode(4,OUTPUT);  //base turn right pulse
    pinMode(8,OUTPUT); //solenoid output
    pinMode(12,OUTPUT);//buffer output
    sh.attach(9); //shoulder servo
    el.attach(10); //elbow servo
    wr.attach(11); //wrist servo
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
    Serial.println("Ready");
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.println("I received: ");
Serial.println(incomingByte, DEC);
}
switch(incomingByte){
   case '1':
      sh.write(shpos+2);
      incomingByte=0;
      break;     
  case '2':
      el.write(elpos+2);
      incomingByte=0;
      break;
  case '3':
     wr.write(wrpos+2);
     incomingByte=0;
     break;
  case '4':
    digitalWrite(2,HIGH);
    digitalWrite(4,LOW);
    delay(800);
    digitalWrite(2,LOW);
    incomingByte=0;
    break;
  case '5':
    digitalWrite(8,HIGH);
    incomingByte=0;
    break;
  case '6':
    sh.write(shpos-2);
    incomingByte=0;
    break;
case '7':
   el.write(elpos-2);
   incomingByte=0;
   break;
case '8':
   wr.write(wrpos-2);
   incomingByte=0;
   break;
case '9':
    digitalWrite(2,LOW);
    digitalWrite(4,HIGH);
    delay(800);
    digitalWrite(4,LOW);
    incomingByte=0;
    break;
case '10':
   digitalWrite(8,LOW);
    incomingByte=0;
    break;
}   
   if(incomingByte == 0){
      digitalWrite(8,HIGH);
      delay(20);
      digitalWrite(8,LOW);
}

if(shpos <= 95){
   shpos = 97;
   sh.write(shpos);
}
if(elpos <= 95){
  elpos = 100;
el.write(elpos);
}

}


the serial is connected to a UHF transceiver, il be sending 0x01 format over the serial...what would be my problem?

AWOL

#6
Jul 07, 2011, 04:19 pm Last Edit: Jul 07, 2011, 04:21 pm by AWOL Reason: 1
Code: [Select]
case '10':
That's not going to happen.

You still need to describe the "weird output".

Quote
il be sending 0x01 format over the serial

What does that mean?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

pao_wikhan

Code: [Select]
case '5':
    digitalWrite(8,HIGH);
    incomingByte=0;
    break;


would this remain high?

Code: [Select]

case '10':
   digitalWrite(8,LOW);
    incomingByte=0;
    break;


case 10 would turn case 5 off.

ive replaced the sending using char 'A'...'J'

AWOL

Quote
case 10 would turn case 5 off.

Fitting two ASCII characters into a "int" isn't impossible, but you haven't included a mechanism to do it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

pao_wikhan

i dont understand what your saying..what do you mean mechanism?

AWOL

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
Code: [Select]
case '10':
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

pao_wikhan

ohhh ok tnx! will post update on my work..

pao_wikhan

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!

zoomkat

Quote
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:

Code: [Select]
// 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="";
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

pao_wikhan

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.

Go Up