Here it goes… 🙂
My final project idea was to get something from an alternate reality. After some brainstorming, I came up with this idea to make this arcade claw game (everyone knows this when this game where it is almost impossible to pull out an item) and make it virtual (a.k.a. on the computer) and make the prize be real. So it turned out to be this:
Description of the software/hardware:
The game itself consists of a big script made in processing which drew the claw machine game (code will be attached in the end) and an arduino script which opened the door and gave the prize. Sounds simple, but it took a lot of effort to do it. The claw was controlled with a joystick and the goal of the game was to drop an item off screen.
Expectation/evaluation:
The expectation was to get an item from the alternate reality and it worked out pretty well 🙂 The prizes were some haribos and chocolate chip cookies
Code:
Arduino code:
#include <Servo.h>; int left = 0; int right = 0; int servoPin1 = 3; int servoPin2 = 4; const int inX = A1; const int pressed = 6; int xValue = 0; int notPressed = 0; Servo Servo1; Servo Servo2; void setup() { pinMode(inX, INPUT); // setup y input pinMode(pressed, INPUT_PULLUP); // we use a pullup-resistor for the button functionality Servo1.attach(servoPin1); Servo2.attach(servoPin2); Serial.begin(9600); Serial.println("1,1"); pinMode(3, OUTPUT); pinMode(4, OUTPUT); } void loop() { xValue = analogRead(inX); notPressed = digitalRead(pressed); Serial.print(xValue); Serial.print(","); Serial.println(notPressed); while (Serial.available()) { right = Serial.parseInt(); left = Serial.parseInt(); if (Serial.read() == '\n') { Servo1.write(right); Servo2.write(left); } } }
Processing code:
main:
import shiffman.box2d.*; import org.jbox2d.common.*; import org.jbox2d.dynamics.joints.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.collision.shapes.Shape; import org.jbox2d.common.*; import org.jbox2d.dynamics.*; import org.jbox2d.dynamics.contacts.*; import processing.serial.*; Box2DProcessing box2d; Claw claw; bounds bounds; movement move; Serial myPort; float moveX = 0; float moveY = 0; int directionX = 0; int directionY = 0; float gravity = 1; float ellipseX = 200; float ellipseX1 = 600; float ellipseY = 400; float ellipseY1 = 400; int s; int current = 0; boolean time = false; boolean pickup = false; boolean attach = false; boolean attach1 = false; boolean ball1_out = true; boolean ball2_out = true; PImage img1,img2; void setup() { //size(1280,1024); fullScreen(); smooth(); imageMode(CENTER); claw = new Claw(); bounds = new bounds(); move = new movement(); printArray(Serial.list()); img1 = loadImage("/home/neonovi/Desktop/cookie.png"); img2 = loadImage("/home/neonovi/Desktop/snake.png"); String portname=Serial.list()[32]; println(portname); myPort = new Serial(this, portname, 9600); myPort.clear(); myPort.bufferUntil('\n'); myPort.write("1,1"); } void keyPressed() { pickup=false; if (key == 'd' && moveY==0) directionX = 5; else if (key == 'a' && moveY==0) directionX = -5; else if (keyCode == 32) { attach = false; attach1 = false; if (moveY < height-300) directionY=3; claw.add = 0.01; } } void keyReleased() { directionY=-3; directionX=0; claw.add=-0.01; } void draw() { background(255); move.display(); claw.display(moveX, 150+moveY); fill(0); if (ellipseY<height-76 && !attach || ellipseX > width-150 && !attach) ellipseY+=gravity; if (ellipseY1<height-76 && !attach1 || ellipseX1 > width-150 && !attach1) ellipseY1+=gravity; image(img1,ellipseX, ellipseY, 100, 80); image(img2, ellipseX1, ellipseY1, 100,80); if (ellipseY >= height) ball1_out = false; if (ellipseY1 >= height) ball2_out = false; bounds.display(); //image(img1,0,0,100,80); myPort.write(135*int(ball1_out)+","+135*int(!ball2_out)+"\n"); println("ellipseY = "+ellipseY+" | moveY = "+moveY);//135*int(!ball1_out)+","+135*int(ball2_out)+"\n"); println("ellipseX = "+ellipseX+" | moveX = "+moveX); }
boundries:
class bounds{ void display(){ rect(0, height-50, width-200, 50); rect(width-200, height-100, 50, 100); } }
claw:
class Claw { float x=0; float y=0; float z=0; float add=0; Claw() { } void display(float x, float y) { fill(255); //ellipse(x+5, y+50, 100, 100); fill(0); rect(x, -1024+y, 10, 1024); pushMatrix(); translate(x, y-5); rotate(z%TWO_PI); beginShape(); vertex(0, 0); vertex(10, 0); vertex(-30, 50); vertex(0, 100); vertex(-50, 50); endShape(); popMatrix(); pushMatrix(); translate(x+10, y-5); rotate(-z%TWO_PI); beginShape(); vertex(0, 0); vertex(0-10, 0); vertex(30, 50); vertex(0, 100); vertex(50, 50); endShape(); popMatrix(); if (z<0.6 && add>0) z+=add; if (z>0 && add<0) z+=add; } }
movement:
class movement { void display() { if (moveY<=height-301) moveY += directionY; moveX += directionX; if (moveY<0) { directionY=0; moveY=0; } if (moveY>height-300) { directionY=0; if (!time) { current = millis(); time = true; } if (millis()-current>1500) { time=false; pickup=true; } } if (pickup) { if (moveY > 0) { moveY-=2; if (moveX >= ellipseX-25 && moveX<=ellipseX+25 && (ellipseY-moveY)<230) { attach = true; gravity=0; ellipseY = moveY+220; } else if (moveX >= ellipseX1-25 && moveX<=ellipseX1+25 && (ellipseY1-moveY)<230) { attach1 = true; gravity=0; ellipseY1 = moveY+220; } } else moveY=0; } gravity+=0.1; if (moveX>1280) { moveX = 0; } if (moveX<0) { moveX = 1280; } if (attach) { ellipseX=moveX+5; gravity=0; ellipseY=moveY+220; } else if (attach1) { ellipseX1=moveX+5; gravity=0; ellipseY1=moveY+220; } else gravity = 5; } }