Hi, I'm making a 6 d.o.f. robot arm (exactly like a human arm), and have no idea what I'm doing wrong. My objective is to make a servo move when a joystick is at a certain position, ex: I move the joystick up and the servo moves in one direction until I put the joystick back in neutral position. I'm using an arduino due, if you're confused about the pin names. By the way, this is my first arduino project, so excuse me if I have stupid things in here, and please don't tell me to "start with something easier" or whatever, I want to learn so please help me with that. The other issue I'm having is that I don't know how to make a base that can rotate, so that all the weight of the arm isn't on the servo, the servo merely makes it turn. Thanks in advance
Here is my code:
#include <Servo.h>
Servo claw; //it's named claw because it will be the servo that operates the claw
int clawpos = 0; //to store the position of the servo
int onex = A0; //the x-axis reading from the thumbstick, on pin A0
void setup () {
claw.attach (2);
pinMode (onex, INPUT);
}
void loop () {
for (onex = 0; onex < 400; clawpos += 1);
claw.write (clawpos);
delay (20);
for (onex = 0; onex > 500; clawpos -= 1);
claw.write (clawpos);
delay (20);
}
3.3V and 5V produce different values when you read the pin. Make sure you calibrate it first, see what the maximum and minimum values are when you read the pin @ 3.3V.
Well, before the servo just went in one direction as far as it could go and stayed there, now the servo stays at 90 degrees. Not sure if that's progress or not.
When the analog value of the thumbstick is more then 450, and when the value is less than 750. I realize the flaw there, so I'll try vice versa tomorrow. Thanks
Here's my latest code. I don't know if the brackets around the claw.write stuff is necessary but they're there in the "sweep" example in the ide.
#include <Servo.h>
Servo claw; //it's named claw because it will be the servo that operates the claw
int clawpos = 0; //to store the position of the servo
int onex = A0; //the x-axis reading from the thumbstick, on pin A0
void setup () {
claw.attach (2);
pinMode (onex, INPUT);
}
void loop () {
for (onex = 0; onex < 450; clawpos += 1); //says that if the value of the thumbstick is less than 450 the position of the servo should be increased by 1
{
claw.write (clawpos); //writes the updated position to the servo
delay (200); //gives it time to get there without jitters
}
for (onex = 0; onex > 750; clawpos -= 1);
{
claw.write (clawpos);
delay (200);
}
}
Maybe not the best choice. The knob example uses a pot to control a servo. Below is some test code for a two pot joystick.
@zoomkat I modelled it off of the sweep example because when I move the joystick in a direction, I want the servo to move in one direction until I put the joystick back in to neutral position, ex: I move he joystick up, and the servo moves in one direction, until I put the joystick back to the centre. So: If the value of one of the joysticks analog out put is less than 450, I want the joy stick to move in one direction until it reaches its end point, or until the value of the same analog output changes to more than 750, which would cause it to move in the other direction. If the value is somewhere in between 450 and 750, then I want the servo to stay at the position it's at. I just don't know how to put that in to code. Thanks for all your help so far. By the way, I tried your code and it was exactly like the "knob" example in the ide. Move the joystick up, servo moves, move the joystick back to neutral, servo goes back to neutral.
I made the below code a while back to test sweeping a servo using buttons. I haven't gotten to try it yet, but probably could be adapted for using a pot.
//zoomkat servo button sweep test 12-23-2013
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 6; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 90; // variable to store and set the servo position
void setup()
{
Serial.begin(9600);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(7);
servo1.write(pos); //starting position
digitalWrite(6, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
Serial.println("servo button sweep test 12-23-2013");
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
pos=(pos+1);
if(pos>180) pos=180; //limit upper value
Serial.println(pos); //for serial monitor debug
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(150); // waits 150ms to slow servo movement
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
pos=(pos-1);
if(pos<0) pos=0; //limit lower value
Serial.println(pos); //for serial monitor debug
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(150); // waits 150ms to slow servo movement
}
}
@HazardsMind I'm using for loops instead of mapping the value because I want the servo to move continuously in a direction, unless the servo is in neutral position, ex: I move the joystick up, and the servo moves to the right until I put the joystick back to its center position. Much like telling a continuous servo to go to a certain position, and then it doesn't stop until you stop telling it to go there.
Ok, imagine the "sweep" program, except you are able to control which direction it sweeps in, and once the servo reaches it's endpoint it stays there. Now, imagine that the servo doesn't always rotate to its endpoint, but moves in steps of lets say 5 degrees, so it doesn't necessarily move to its endpoint unless it's told to do so. Now, imagine that a joystick, when in its up position makes the servo move in one direction, and that when you move the joystick in the other direction, the servo moves in the other direction. So exactly like "sweep" except it only moves as far as it can in one direction and doesn't rebound and move in the opposite direction, as well as only moves when the joystick tells it to, meaning it doesn't just move from end to end but can can stop at any position. Hope that's a bit clearer and doesn't confuse you more. Try reading this in reverse after reading it there right way, will help you understand.
You just want the servo to move in increments when some condition exist. Pot value greater than neutral, increase servo value, pot value less than neutral, decrease pot value. Pot value neutral, keep current servo value.