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 31Code using switches

Since this controller is just a bunch of switches, we can throw away all the code that had to do with the potentiameters that were in the thumb joysticks.  This means that we won't have control over the speed. 

Here is the controller in action:



Here is the code to run the Crane Game using the Leap Frog controller.  This just uses  6 push bottons to control the stepper motor and the two DC motors.


//////////// Arduino Code ////////////

 

// Speeds
  int Delay_X = 1000;   //step delay: microseconds (up delay = slower speed
  int Speed_S = 255; // spindle speed 0-255
  int Speed_C = 255; //clow speed 0-255

//STEPPER PINS
  //Outputs
     int Step_X = 13;
     int Dir_X = 12;
     int Enable_X = 8;
  //Inputs
     int Left = 16;
     int Right = 17;
  // variables
     int val_Left = LOW;
     int val_Right = LOW;
    
//SPINDLE PINS  
   //Outputs
      int Dir_A = 4;
      int Speed_A = 5;
   //Inputs
       int Up = 14;
       int Down= 15;
   //variables
       int val_Up = LOW;
       int val_Down = LOW;
      
//CLAW PINS
    //Outputs
       int Dir_B = 7;
       int Speed_B = 6;
    //Inputs
       int Open = 18;
       int Close = 19;
    //variables
       int val_Open = LOW;
       int val_Close = LOW;
      
void setup (){
    // setup up stepper pins
        pinMode(Step_X, OUTPUT);
        pinMode(Dir_X, OUTPUT);
        pinMode(Enable_X, OUTPUT);
        pinMode(Left, INPUT);
        pinMode(Right, INPUT);

    //setup DC motor A pins - for spindle
        pinMode(Dir_A, OUTPUT);
        pinMode(Speed_A, OUTPUT);
        pinMode(Up, INPUT);
        pinMode(Down, INPUT);

    //setup DC motor B pins - for claw
        pinMode(Dir_B, OUTPUT);
        pinMode(Speed_B, OUTPUT);
        pinMode(Open, INPUT);
        pinMode(Close, INPUT);
}

void loop(){
// STEPPER READ AND CONTROL //
   val_Left = digitalRead(Left);
   val_Right = digitalRead(Right);
 
   if (val_Left == HIGH){
      digitalWrite(Enable_X,HIGH); // enable
      digitalWrite(Dir_X, HIGH);  // Set direction
      digitalWrite(Step_X,HIGH);
      delayMicroseconds(2);
      digitalWrite(Step_X,LOW);
      delayMicroseconds(Delay_X);
       }
    
    if (val_Right == HIGH){
      digitalWrite(Enable_X,HIGH); // enable
      digitalWrite(Dir_X, LOW);  // Set direction (other direction)
      digitalWrite(Step_X,HIGH);
      delayMicroseconds(2);
      digitalWrite(Step_X,LOW);
      delayMicroseconds(Delay_X);
      }
 
    if ((val_Left == LOW) && (val_Right == LOW))  {
     digitalWrite(Enable_X, LOW); //stepper off
        }
       
 // SPINDLE READ AND CONTROL //
   val_Up = digitalRead(Up);
   val_Down = digitalRead(Down);
  
   if (val_Up == HIGH){
     digitalWrite(Dir_A, HIGH);
     analogWrite(Speed_A, Speed_S);
     }
 
   if (val_Down == HIGH){
     digitalWrite(Dir_A, LOW);
     analogWrite(Speed_A, Speed_S);
     }
    
    if ((val_Up ==LOW) && (val_Down == LOW)){
      analogWrite(Speed_A, 0);
    }
   
 
// CLAW READ AND CONTROL //
   val_Open = digitalRead(Open);
   val_Close = digitalRead(Close);
  
   if (val_Open == HIGH){
     digitalWrite(Dir_B, HIGH);
     analogWrite(Speed_B, Speed_C);
     }
 
   if (val_Close == HIGH){
     digitalWrite(Dir_B, LOW);
     analogWrite(Speed_B, Speed_C);
     }
    
    if ((val_Open ==LOW) && (val_Close == LOW)){
      analogWrite(Speed_B, 0);
    }
 
}
 

« 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.