3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

CRANE GAME

Step 15Claw code and testing

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
  }
}

 

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
41
Followers
16
Author:marc.cryan
Looking for access to land or water in Boston Metrowest area.