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