Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
STEMpedia
Published © CC BY

Candy Claw Machine - Arduino Based Arcade Game

This project will show you how to make your own DIY candy claw machine.

ExpertFull instructions providedOver 2 days4,947
Candy Claw Machine - Arduino Based Arcade Game

Things used in this project

Hardware components

evive
STEMpedia evive
×1

Software apps and online services

Arduino IDE
Arduino IDE
STEMpedia GamePad

Story

Read more

Custom parts and enclosures

3mm_Acrylic_White_1200mmx900mm

3mm_Acrylic_White_1200mmx900mm2

5mm_Acrylic_White_300mmx400mm

Schematics

Fritzing Diagram for Claw Machine

Code

Arduino Code for Claw Machine

Arduino
#include <evive.h>
#include <BasicStepperDriver.h>
#include <LiquidCrystal.h>

int motor1Dir1 = 28; // motor direction 1 pin
int motor1Dir2 = 29; // motor direction 2 pin
int motor1PWM = 44; // motor pwm pin
Motor Motor1 = Motor(motor1Dir1, motor1Dir2, motor1PWM);

#define MOTOR_STEPS 200
#define DIR1 2
#define STEP1 3
#define DIR2 4
#define STEP2 5
#define MICROSTEPS 1
#define LimitX1 22
#define LimitX2 23
#define LimitZ 24
#define LimitY1 26
#define LimitY2 27

BasicStepperDriver stepper1(MOTOR_STEPS, DIR1, STEP1);
BasicStepperDriver stepper2(MOTOR_STEPS, DIR2, STEP2);

Servo myservo;   
int ServoVal = 50;
const int rs = 12, en = 11, d4 = 6, d5 = 7, d6 = 8, d7 = 9;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
long current;

void setup() {
  Serial3.begin(38400);
  Serial.begin(9600);
  pinMode(LimitX1, INPUT);
  pinMode(LimitX2, INPUT);
  pinMode(LimitY1, INPUT);
  pinMode(LimitY2, INPUT);
  pinMode(LimitZ, INPUT);
  pinMode(18, OUTPUT);
  pinMode(19, OUTPUT);
  pinMode(20, OUTPUT);
  pinMode(40, OUTPUT);

  stepper1.setRPM(300);
  stepper2.setRPM(200);
  myservo.attach(45);
  myservo.write(ServoVal);
  lcd.begin(20, 2);
  // Print a message to the LCD.
  lcd.print("Insert Coin");
  
}

void loop() {
  if(digitalRead(40)){
    digitalWrite(18,HIGH);
  digitalWrite(19,HIGH);
  digitalWrite(20,HIGH);
  if(analogRead(4) > 30){
    long Timer = 120 ;
    lcd.setCursor(0, 0);
    lcd.print("Game Starts in");
    for(int i=0; i < 5; i ++){
      lcd.setCursor(0, 1);
      lcd.print(5-i);
      lcd.print("   ");
      delay(1000); 
    }
    lcd.setCursor(0, 0);
    current = millis();
    lcd.print("Time ends in    ");
    while((millis()-current) < Timer*1000){
      if((millis()-current) < Timer*300){
        digitalWrite(18,LOW);
        digitalWrite(19,LOW);
        digitalWrite(20,HIGH);
        delay(50);
        pinMode(13,OUTPUT);
        digitalWrite(13,HIGH);
      }
      else if((millis()-current) < Timer*700){
        digitalWrite(18,HIGH);
        digitalWrite(19,LOW);
        digitalWrite(20,LOW);
        delay(50);
      }
      else if((millis()-current) < Timer*1000){
        digitalWrite(18,LOW);
        digitalWrite(19,HIGH);
        digitalWrite(20,LOW);
        delay(50);
      }
      lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
      if (analogRead(0) > 800) {
    while(analogRead(0) > 800){
      if(!digitalRead(LimitY1)){
        stepper2.setMicrostep(MICROSTEPS);
        stepper2.rotate(-18);
        lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   "); 
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(0) < 200) {
    while(analogRead(0) < 200){
      if(!digitalRead(LimitY2)){
        stepper2.setMicrostep(MICROSTEPS);
        stepper2.rotate(+18); 
        lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(1) > 800) {
    while(analogRead(1) > 800){
      if(!digitalRead(LimitX2)){ 
      stepper1.setMicrostep(MICROSTEPS);
      stepper1.rotate(-18); 
      lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(1) < 200) {
    while(analogRead(1) < 200){
      if(!digitalRead(LimitX1)){ 
      stepper1.setMicrostep(MICROSTEPS);
      stepper1.rotate(18); 
      lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(2) > 800) {
    while(analogRead(2) > 800){
      if(!digitalRead(LimitZ)){ 
      Motor1.moveMotor(255);
      delay(20);
      Motor1.freeMotor();
      delay(5);
      lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(2) < 200) {
    while(analogRead(2) < 200){
      Motor1.moveMotor(-255);
      delay(10);
      Motor1.freeMotor();
      delay(5);
      lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
    }
  }
  if (analogRead(3) > 800) {
    while(analogRead(3) > 800){
      if(ServoVal >= 30){ 
      ServoVal = ServoVal - 2;
      myservo.write(ServoVal);
      delay(19);
      lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(3) < 200) {
    while(analogRead(3) < 200){
      if(ServoVal <= 70){ 
      ServoVal = ServoVal + 2;
      myservo.write(ServoVal);
      delay(19);
      lcd.setCursor(0, 1);
      lcd.print(Timer - (millis() - current)/1000);
      lcd.print("   ");
      }
      else {
        break;  
      }
    }
  }
    }
    digitalWrite(18,HIGH);
    digitalWrite(19,HIGH);
    digitalWrite(20,HIGH);
    delay(500);
    digitalWrite(18,LOW);
    digitalWrite(19,LOW);
    digitalWrite(20,LOW);
    delay(500);
    digitalWrite(18,HIGH);
    digitalWrite(19,HIGH);
    digitalWrite(20,HIGH);
    delay(500);
    digitalWrite(18,LOW);
    digitalWrite(19,LOW);
    digitalWrite(20,LOW);
    delay(500);
    digitalWrite(18,HIGH);
    digitalWrite(19,HIGH);
    digitalWrite(20,HIGH);
    delay(500);
    digitalWrite(18,LOW);
    digitalWrite(19,LOW);
    digitalWrite(20,LOW);
    delay(500);
    
  }
  else{
    lcd.setCursor(0, 0);
    lcd.print("Insert Coin     ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    
  }
  }
  else{
    if (analogRead(0) < 200) {
    while(analogRead(0) < 200){
      if(!digitalRead(LimitY2)){
        stepper2.setMicrostep(MICROSTEPS);
        stepper2.rotate(+18); 
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(1) > 800) {
    while(analogRead(1) > 800){
      if(!digitalRead(LimitX2)){ 
      stepper1.setMicrostep(MICROSTEPS);
      stepper1.rotate(-18); 
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(1) < 200) {
    while(analogRead(1) < 200){
      if(!digitalRead(LimitX1)){ 
      stepper1.setMicrostep(MICROSTEPS);
      stepper1.rotate(18); 
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(2) > 800) {
    while(analogRead(2) > 800){
      if(!digitalRead(LimitZ)){ 
      Motor1.moveMotor(255);
      delay(20);
      Motor1.freeMotor();
      delay(5);
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(2) < 200) {
    while(analogRead(2) < 200){
      Motor1.moveMotor(-255);
      delay(10);
      Motor1.freeMotor();
      delay(5);
    }
  }
  if (analogRead(3) > 800) {
    while(analogRead(3) > 800){
      if(ServoVal >= 30){ 
      ServoVal = ServoVal - 2;
      myservo.write(ServoVal);
      delay(19);
      }
      else {
        break;  
      }
    }
  }
  if (analogRead(3) < 200) {
    while(analogRead(3) < 200){
      if(ServoVal <= 70){ 
      ServoVal = ServoVal + 2;
      myservo.write(ServoVal);
      delay(19);
      }
      else {
        break;  
      }
    }
  }
  }

}

evive Library

C/C++
No preview (download only).

Stepper Motor Library

C/C++
No preview (download only).

Credits

STEMpedia
42 projects • 169 followers
STEMpedia blends theory with experiential learning by offering state-of-the-art technology, projects, tutorials, courses, and much more.

Comments