Step 15: Claw code and testing
I found another thumb joystick, so we'll use that to control the opening and closing of the claw.
This is the same code used to control the spindle motor (for raising and lowering the claw), it just has different pin assignments and variable names.
If I dig up an old PS2 controller I will hack that and use it as the main controller.
After testing this it will be folded in with the rest of the code.
Here is a video. The code is below.
///////////////// Arduino Code ////////////////////
///Claw control/////
// declare pins for DC motor B // claw open/close
int potPin_B = 3; // select the input pin for the pot
int val_B = 0; // variable to store the value coming from the sensor
//Declare variables for DC motor B
int k = 0;
int Dir_B = 7;
int Speed_B = 6; //pwm
void setup(){
//setup DC motor B pins
pinMode(Dir_B, OUTPUT);
pinMode(Speed_B, OUTPUT);
}
void loop() {
////////////// DC MOTOR B - READ AND CONTROL ///////////////////
// Read location of joystick and calculate the distance and from center
val_B = analogRead(potPin_B); // read the value from the sensor
k = val_B - 517; // 517 is center positions - how far from center
k = abs(k); //absolute value
// put some bounds on j to keep PWM values useful
// below 100 the motor won't move and PWM max is 255
if (k >= 510){
k = 510; //the most the PWM pin can do is 255
}
if (k <=200 && k>=10){
k=200; //below 100 PWM the motor makes a high pich sound and does not move
}
if (k <=10){
k=0; // below 10 the joystick is very close to center
}
//Run DC motor B based on analog input from joystick
if (val_B >= 530){
digitalWrite(Dir_B, HIGH); // other direction
analogWrite(Speed_B, k/2); // PWM out (divide by 2 because max is 255)
}
if (val_B <= 500){
digitalWrite(Dir_B, LOW); //
analogWrite(Speed_B, k/2); //
}
if (val_B <=530 && val_B >= 500) {
analogWrite(Speed_B, 0); // turn off if the joystick is in the center
}
}
Remove these ads by
Signing Up














































Not Nice
















Visit Our Store »
Go Pro Today »



