Claw Machine Game

Ahoi Mateys! Avast ye timber. Aye be done with a school project with the Arduino. Arggh! Have a lookie before all ye abandon hope.

I left room for improvement so people could criticize my project. I used K’nex, Arduino UNO, shoeboxes and miscellaneous nuggets I found around the house that I chose to recycle. Here are some photos and a video. By the way, is there a way to resize the photos as I link them?

Thanks Korman, PaulS and zoomkat for helping me out with some of my questions. I placed your forum name in my video. I know this project in it’s general form is simplistic but I loved building and figuring out how everything was going to configure and work with the motors and electric wiring. I also improved on my soldering skills. Don’t hesitate to hit me up if you need soldering as I would love to get more practice and try it.








//Claw Machine by Tyler L
//This claw machine works by triggering a photocell sensor to be dark enough so that it will
//start the timer for 30 seconds in which the user has that much time to control where the
//claw goes. The claw can be prematurely initiated by pushing the button at a position.
#include <Stepper.h>
int sensorPin = 0;    // vertical sensor
int sensorPin2 = 1; // horizontal sensor
int photocell = 2;
int sensorphoto = 0;
int vertical = 0;
int horizontal = 0;
int x=0;int y=0;
int j=0;int k=0;int l=0;int m=0;
int locationx,locationy;
int timer = 75;
int motor1a = 10;//dc motor
int motor1b = 11;
int motor2a = 18;//dc motor
int motor2b = 19;
unsigned long time = 0;
int test = 0;
int z, b;
int enable1=13;//Step motor enable
int enable2=12;//Step motor enable
int buttonPin=17;

Stepper stepper1(200, 2,3,4,5); //pin 2 is blue ribbonwire (y), 3 is yellow(r), 4 is green(o), 5 is red(b)
Stepper stepper2(200,6,7,8,9);

void setup() {

  stepper1.setSpeed(60);//max speed 150
  stepper2.setSpeed(60);
  pinMode(motor1a,OUTPUT);
  pinMode(motor1b,OUTPUT);
  pinMode(motor2a,OUTPUT);
  pinMode(motor2b,OUTPUT);
  pinMode(enable1,OUTPUT);
  pinMode(enable2,OUTPUT);
  pinMode(buttonPin,INPUT);
}

void loop() {
  sensorphoto = analogRead(photocell);
if (sensorphoto <200){
  delay(1500);
  if(analogRead(photocell)<200){}
  else if(analogRead(photocell)>200){
time = millis() + 30000;
digitalWrite(enable1,HIGH);
digitalWrite(enable2,HIGH);
test++;
stepper1.step(95);
j=95;
}}
if ((millis() < time) && test ==1){
  vertical = analogRead(sensorPin);    
  horizontal = analogRead(sensorPin2);
  b = digitalRead(buttonPin);
if (b==LOW){
  lowerClaw();
}

if((vertical >=540 && j<=900) ){//up
  stepper1.step(1);
  j++;

    locationy = -j;
  }
if((vertical <= 500 && j>=95) ){//down
  stepper1.step(-1);
  j--;

    locationy = -j;
}
if((horizontal<=500 && l<760) ){//right
  stepper2.step(-1);
  l++;
    locationx = l;
  }
if((horizontal>=540 && l>=0) ){//left
  stepper2.step(1);
  l--;
    locationx = l;
}
if((vertical >=540 && j<=900) && (horizontal<=500 && l<=760)){//move up and right
  stepper1.step(1);
  stepper2.step(-1);
  j++;
  l++;

  locationy=-j;
  locationx=l;
    }
if((vertical >=540 && j<=900) && (horizontal>=540 && l>=0)){//move up and left
  stepper1.step(1);
  stepper2.step(1);
  j++;
  l--;

  locationy=-j;
  locationx=l;
    }
if((vertical <= 500 && j>=95) && (horizontal<=500 && l<=760)){//move down and right
  stepper1.step(-1);
  stepper2.step(-1);
  j--;
  l++;

  locationy=-j;
  locationx=l;
}
if((vertical <= 500 && j>=95) && (horizontal>=540 && l>=0)){//move down and left
  stepper1.step(-1);
  stepper2.step(1);
  j--;
  l--;

  locationy=-j;
  locationx=l;
}}
if(millis() > time && test==1){
lowerClaw();
}
if(test==2){
  for(z = 0; z<=900;z++){
  if(j>0){
    stepper1.step(-1);
  j--;
}
  if(l>7){
    stepper2.step(1);
  l--;
  }
}
  digitalWrite(enable1,LOW);
  digitalWrite(enable2,LOW);
  delay(1000);
  digitalWrite(motor2a,HIGH);//claw open
  digitalWrite(motor2b,LOW);
  delay(580);
  digitalWrite(motor2a,LOW);
  digitalWrite(motor2b,LOW);
  delay(1000);
  test = 0;
}}

void lowerClaw(){
  delay(2000);
  test++;
  digitalWrite(enable1,LOW);
  digitalWrite(enable2,LOW);
  
  digitalWrite(motor1a, HIGH);//claw is lowered
  digitalWrite(motor1b, LOW);

  delay(1040);
  digitalWrite(motor1a, LOW);//claw is in position
  digitalWrite(motor1b, LOW);
  delay(2000);
  digitalWrite(motor2a,LOW);//claw closes
  digitalWrite(motor2b,HIGH);
  delay(550);
  digitalWrite(motor2a,LOW);
  digitalWrite(motor2b,LOW);
  delay(2000);
  digitalWrite(motor1a, LOW);//claw is raised
  digitalWrite(motor1b, HIGH);

  delay(1185);
  digitalWrite(motor1a, LOW);
  digitalWrite(motor1b, LOW);
  delay(1000);
  digitalWrite(enable1,HIGH);
  digitalWrite(enable2,HIGH);
  
}

This is totally wicked!

Thanks. I'm still continuing to modify it before the final date is which is actually this next Monday.

Where do you get ribbon cable?

Where do you get the little kid to stick inside?

http://news.blogs.cnn.com/2010/12/10/gotta-watch-parliamentary-brawls-toddler-in-toy-machine/?hpt=C2

Where do you get ribbon cable?

I bought mine from a local hobby store; HobbyTown USA (http://www.hobbytown.com/). The ribbon wire was meant for those mini-electric train sets. Those train sets, can use up to 17VDC, it's crazy how dangerous those things are and yet kids back in the old days had to be smart to play with them or they'd electrocute themselves.

Where do you get the little kid to stick inside?

That's too cute. I wonder what was going through that little girl's mind. "Oooh stuffed animals!!"

Brilliant idea of interfacing K-NEX with arduino! Ive got boxes of the stuff lying around, and you've inspired me to do something with it! Excellent project too!

Thanks, Onions. There are some improvements I know I do not have time for but for future work, if anyone wants to do a project similar to mine, I would recommend buying motor couplers and attach the Knex rods to the motor shaft in that manner. They both have the same diameter but I felt the motor shaft was not long enough for what I needed and a coupler would give more power to the shaft. I will say this, though. The NEMA 23 bi-polar stepper motors I used fit almost perfectly with the Knex housing that I made for it which was a surprise.

Another would be if I had a larger budget, I would definitely have made this larger and order all-threads (not sure if I would go with plastic or metal) to move the motor along the axis rather than using the wheel/pulleys. I would make the claw machine larger in the sense that there would actually be a reward drop zone that people can only grab their prize from and they cannot reach up into it just like the real thing.

As for electrical hardware, I wish I had bought L298 Dual Full-Bridge Driver. I was having power issues with having all four motors "on" at once so I had to turn drivers off at certain times when not in use. Using a L298 would eliminate that along with providing an additional 24V DC power source.