Introduction: D.D.Tap - Interactive Game Platform Using Processing, Arduino and Twitter



The D.D.Tap is a game that can be played using sensors connected to Arduino and interface it to the game produced with Processing. It is a simple dance dance revolution like game that accumulates point when the player activates the sensors or press the keyboard keys when the random arrows reaches the red transparent bar. The circuit built on Arduino is simple as I had limited sensors. I used the photocells in a pull-up configuration to act as sensors. I uploaded the applet too @http://www.openprocessing.org/visuals/?visualID=29670  but for the Twitter function to work, you must enter your own consumer key and so on taken from your OAuth account on http://dev.twitter.com/apps . Loading on openprocessing.org is quite long due to the large file so to skip it, just press "Source Code".
Also, i used the song yeahx3 by Chris Brown as it has the catchy beat for the random arrows~ 

The outcome is OK but i would still like to fix the problem of accumulating more than one bonus for each arrow. This problem was not so significant when played with the keyboard but when its reading serial data from the com port, the data rate was so fast that it triggers bonuses more than once for each arrow. I would be glad if anyone would help out with the Processing codes. i thought of creating a array to check the bonuses but that did not work quite well. So, I somehow half solved that problem by using delay in the Arduino code. This of course introduced other problems such as; if both sides of the arrow reaches at the same time, only one arrow bonus could be claimed. The video shows the interaction example and that if there was a new highscore, the score would be posted as a tweet at your page. I am willing to accept any upgrade suggestions. 

Here's the code!!! Without the twitter key of course...
//group of arrows
pointer[] pointers;
boolean reset = true;
int num;
public static float yPos, yPos2;

//twitter initialization
tweet twitter;

//Serial communication
import processing.serial.*;
Serial port;
String portname = "COM1";
int baudrate = 9600;
int reading = 0;

PImage picture,blueButton,redButton;
//initialize play for mp3
import ddf.minim.*;
Minim minim;
AudioPlayer yeah;

//initialize variables
PFont font;
int target;
int level = 1;
int score = 0;
int recorded;
int achievement;
boolean win = false;
boolean lose = false;
PrintWriter output;
String[] file;
String[] values;
int[] value;

void setup() {
size(600, 600);

//initialize port
port = new Serial(this, portname, baudrate);
println(port);

//load image
picture = loadImage("view.jpg");
blueButton = loadImage("bluebutton.png");
redButton = loadImage("redbutton.png");

//music
minim = new Minim(this);
yeah = minim.loadFile("yeah.mp3");
yeah.play();

//font
font = loadFont("BerlinSansFB-Bold-48.vlw");
textFont(font);

values = loadStrings("Values.txt"); //load values
value = new int[values.length];
for (int i=0; i < values.length; i++) {
println(values);
value[i] = Integer.valueOf(values[i]).intValue(); //convert string to integer
}

file = loadStrings("Highscore.txt"); //load previous record
for (int i=0; i < file.length; i++) {
recorded = Integer.valueOf(file[i]).intValue(); //convert string to integer
}

frameRate(value[0]);
}

void draw() {
yPos = 0; //reset arrow location
yPos2 = 0;
view(); //draw background design

//level 1
if (level == 1) {
if (reset) {
make();
target = value[1];
}
if ((millis() >= value[4])&&(score >= target)) {
level = 2;
reset = true;
redraw();
}
else if (millis() >= value[4]) {
lose = true;
}
}
//level 2
else if (level == 2) {
if (reset) {
make();
target = value[2];
}
if ((millis() >= value[5])&&(score >= target)) {
level = 3;
reset = true;
redraw();
}
else if (millis() >= value[5]) {
lose = true;
}
}
//level 3
else if (level == 3) {
if (reset) {
make();
target = value[3];
}
if ((millis() >= value[6])&&(score >= target)) {
win = true;
}
else if (millis() >= value[6]) {
lose = true;
}
}

//instructions
textSize(40);
text("Controls", 410, 50);
textSize(20);
text("'a' to hit left \n'l' to hit right", 410, 100);
String beat = ("Target score: " + target);
text(beat, 405, 200);
text("Ellapsed time:", 410, 250);
text(millis()/1000, 410, 280);
String beatH = ("Previous Highscore:\n"+recorded);
text(beatH,410,320);

//level and score
fill(0);
score = score + achievement;
textSize(40);
text("Score", 420, 450);
text(score, 420, 480);
String lev = "Level "+level;
text(lev, 420, 410);
achievement=0; //reset achievement

//display arrow
for (int i=0;i<num;i++) {
pointers[i].arr();//make the arrow
pointers[i].motion();
}

//Controls output and actions when player finishes all rounds
if (win) {
fill(255, 73, 73, 190);
textSize(80);
text("YOU WIN!!!\nPress ESC", 80, 300);
//save("Tap score " + score +".jpg"); //can be used if you want to save display upon winning

//compare stored highscore and save highest score
if (score >= recorded) {
fill(0);
text("\tNEW\nHIGHSCORE!!!", 20, 100);
output = createWriter("Highscore.txt");
output.println(score);
output.flush();
output.close();

//send result to twitter
twitter = new tweet(score);
twitter.sendHighscore();
}
noLoop(); //stop display
}
else if (lose) {
fill(255, 73, 73, 190);
textSize(80);
text("YOU LOSE!!!\nTRY AGAIN :P", 50, 350);
noLoop();
}
//read serial port
while (port.available () > 0) {
portRead();
}
}

void stop() //stop music
{
// always close Minim audio classes when you finish with them
yeah.close();
// always stop Minim before exiting
minim.stop();
super.stop();
}

void view() { //background design
smooth();
noStroke();
background(0); //set white background
image(picture,0,0);
fill(151, 16, 201, 90);//purple bar
quad(15, 450, 385, 450, 385, 490, 15, 490);
fill(2,56,242,30);//side bars
quad(400, 10, 590, 10, 590, 590, 400, 590);
fill(242,2,79,30);
quad(405, 15, 585, 15, 585, 585, 405, 585);
image(blueButton, 0,450);
image(redButton, 200,450);
fill(30);
}

void make() { //create arrows
num = value[7]; //number of arrows
reset = false;
pointers = new pointer[num];

//make arrows
for (int i=0;i<num;i++) {
pointers[i] = new pointer(level);//make a new arrow
}
background(0);//show refresh
delay(2000);
}

void portRead() { //acts almost like an interrupt when keys are pressed
fill(255);
reading = port.read();
if (reading == 'A') {
ellipse(100, 550, 80, 80);
for (int i=0; i<pointers.length ;i++) {
if ((yPos >= 450)) {
achievement = 100;
}
}
}
else if (reading == 'B') {
ellipse(300, 550, 80, 80);
for (int i=0; i<pointers.length ;i++) {
if ((yPos2 >= 450)) {
achievement = 100;
}
}
}
fill(30);
}

void keyPressed() { //acts near to an interrupt when keys are pressed
if (key == 'a') {
ellipse(100, 550, 80, 80);
if ((yPos >= 450)) {
delay(5);
achievement = 100;
}
}
else if (key == 'l') {
ellipse(300, 550, 80, 80);
if ((yPos2 >= 450)) {
delay(5);
achievement = 100;
}
}
}

class pointer
{
float left, right;
PVector pos, pos2;
PVector vel;

pointer(int v) {
pos = new PVector (100, int(random(-12000, 0)));
pos2 = new PVector (300, int(random(-12000, 0)));
vel = new PVector (0, (v*2)+3); //only moving in y direction
}

void arr() {
fill(255);
strokeWeight(9);
stroke(54,83,234, 160);//vary colours

//left arrow
if (pos.y <= 490) {
if(pos.y >= yPos){
yPos = pos.y;
}
beginShape();
vertex(pos.x-20, pos.y);
vertex(pos.x, pos.y-20);
vertex(pos.x, pos.y-10);
vertex(pos.x+10, pos.y-10);
vertex(pos.x+10, pos.y+10);
vertex(pos.x, pos.y+10);
vertex(pos.x, pos.y+20);
endShape(CLOSE);
}

stroke(13,242,2, 160);//vary colours
//right arrow
if (pos2.y <= 490) {
if(pos2.y >= yPos2){
yPos2 = pos2.y;
}
beginShape();
vertex(pos2.x+20, pos2.y);
vertex(pos2.x, pos2.y+20);
vertex(pos2.x, pos2.y+10);
vertex(pos2.x-10, pos2.y+10);
vertex(pos2.x-10, pos2.y-10);
vertex(pos2.x, pos2.y-10);
vertex(pos2.x, pos2.y-20);
endShape(CLOSE);
}
}
void motion() {//move arrows
pos.add(vel);//add the velocity vector to your position
pos2.add(vel);
}
}

class tweet {
import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;

String msg = "Tap Tap Game New Highscore!!! : ";
boolean sent;
int highscore;

TwitterFactory tf;

tweet(int hscore) {
highscore = hscore;
String ConsumerKey = "**********";
String ConsumerSecret = "**********";
String oauth_token = "**********";
String oauth_token_secret = "***********";

sent = false;

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(ConsumerKey)
.setOAuthConsumerSecret(ConsumerSecret)
.setOAuthAccessToken(oauth_token)
.setOAuthAccessTokenSecret(oauth_token_secret);
tf = new TwitterFactory(cb.build());
}

void sendHighscore() {
Twitter twitter = tf.getInstance();
try {
Status st = twitter.updateStatus(msg + highscore + " on " + getDateTime());
println("Successfully updated: [" + st.getText() + "].");
sent = true;
}
catch (TwitterException e) {
println(e.getStatusCode());
}
}

String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
}

_____________________________________________________________________
Arduino Code

int ledPin = 13;
int value1,value2;

void setup(){
  Serial.begin(9600);
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(ledPin,OUTPUT);
}

void loop(){
  value1 = analogRead(A0);
  value2 = analogRead(A1);

  value1 = constrain(value1,0,1023);
  value2 = constrain(value2,0,1023);
  //value1 = map(value1,0,1023,0,1);
  //value2 = map(value2,0,1023,0,1);

  if(value1<=10){
  //Serial.print("Value1 = ");
  //Serial.println(value1);
  Serial.println("A");
  }

  if(value2<=10){
  //Serial.print("Value2 = ");
  //Serial.println(value2);
  Serial.println("B");
  }
  delay(200);
}



You will have to create two txt files named 'value' and highscore' and if you read the codes, you should know how many fields there are. Good luck!

Adafruit Make It Tweet Challenge

Participated in the
Adafruit Make It Tweet Challenge