CAPACITIVE TOUCH GAME – PRODUINO

Exercise is to combine the logic of Arduino and Processing together to create a working Produino assignment.

Thor ragnarok game is created in Processing and capacitive touch controller is created in Arduino.

It is an interesting game to collect hammers and to stay away from fireballs, Thor is flying to save his home Asgard.

Try to protect Asgard and let the good win over evil.

It gets difficult as speed is going to be increased after every 2 points scored until you reach 10 and YOU WIN!

This game is just for fun and not to disturb any copyright issues.

 

 

<p>

</p>

Processing code for the game :

Fire [] en = new Fire[1000];
Fire [] po = new Fire[500];

import processing.serial.*;
Serial port;

import ddf.minim.*;

AudioPlayer player, player2, player3;
Minim minim, minim2, minim3;

PImage bg;
PImage coin;
PImage fire;
PImage sign;

int score = 0;
int t = 1000;
float ypos = 400;
float xpos = 800;
float speed = 2;
int n = 100;
float [] adata = new float[n];
float [] bdata = new float[n];
float [] cdata = new float[n];
float [] ddata = new float[n];

void setup() {

port = new Serial(this, Serial.list()[0], 115200);
port.bufferUntil(‘\n’);

size(1600,858);

for (int i = 0; i < en.length; i++){
en[i] = new Fire(1);
}
for (int i = 0; i < po.length; i++){
po[i] = new Fire(2);
}

bg = loadImage(“cartoon.png”);
coin = loadImage(“coin.png”);
fire = loadImage(“fire.png”);
sign = loadImage(“sign.png”);

minim = new Minim(this);
minim2 = new Minim(this);
minim3 = new Minim(this);
player = minim.loadFile(“themesong.mp3”, 2048);
player2 = minim2.loadFile(“game_over.mp3”, 2048);
player3 = minim3.loadFile(“win.mp3”, 2048);
player.play();
}
void draw() {
background(bg);
enemyFlow();
pointFlow();
eating();

if (score == 10){
player.pause();
player3.play();
fill(0);
textSize(200);
fill(21, 67, 96, 204);
text(“YOU WIN!!”, 350, height/2);
noLoop();
port.stop();
}
if (score == 2){
speed = 3;
}
if (score == 4){
speed = 4;
}
if (score == 6){
speed = 5;
}
if (score == 8){
speed = 6;
}
//saveFrame();
}

void enemyFlow(){
int i = 0;
for(t=500; millis() > t; t=t+480){
en[i].appear(255,0);
en[i].collisionR();
i++;
}
}

void pointFlow(){
float r2=random(0,700);
int i = 0;
for(t=2000; millis() > t; t=t+1800){
po[i].appear(0,255);
i++;
}
}

void eating(){
imageMode(CENTER);
image(sign ,xpos, ypos);

if(ypos > height){
ypos = 0;
}
else if(ypos+30<0){
ypos=height;
}
if (xpos > width){
xpos = 0;
}
else if (xpos < 0){
xpos = width;
}
}

void keyPressed() { // if wanted play with j,l,i,k keys
switch(key) {
case ‘j’: xpos -= 50; break; // left
case ‘l’: xpos += 50; break; // right
case ‘i’: ypos -= 50; break; // up
case ‘k’: ypos += 50; break; // down

}
}

void serialEvent (Serial port){
String inString = port.readStringUntil(‘\n’);

if (inString != null) {
inString = trim(inString);

println(inString);

try {
String[] data = split(inString, “,”);

float a = float (data[0]);
float b = float (data[1]);
float c = float (data[2]);
float d = float (data[3]);

adata[0] = a; // left
bdata[0] = b; // right
cdata[0] = c; // down
ddata[0] = d; // up

if (a > 100){
xpos -= 6;
}
if (b > 100){
xpos += 6;
}
if (c > 100){
ypos -= 6;
}
if (d > 100){
ypos += 6;
}
redraw();

}
catch (Exception e) { e.printStackTrace(); }
}
}

class Fire {
float yloc = height;
float xloc = width;
float rcol;
float gcol;
float randomizer;

Fire(float tempXloc) {
randomizer = tempXloc;

if (randomizer == 1) {
xloc = random(300, 1600);
}
else if (randomizer == 2) {
xloc = random(300, 1600);
}
}

void appear(float tempRcol, float tempGcol) {
rcol =tempRcol;
gcol =tempGcol;

if (rectBall(xpos, ypos, 30, 30, xloc, yloc, 10)==true && rcol == 0) {
score++;
xloc = -20;
}

if (tempRcol == 255){
imageMode(CENTER);
image(fire, xloc, yloc) ;
}
else if (tempGcol == 255){
imageMode(CENTER);
image(coin, xloc, yloc) ;
}
yloc=yloc-speed;
fill(255);
textSize(30);
text(“Score:” +score, 20, 30);
}

void collisionR() {
if (rectBall(xpos, ypos, 30, 30, xloc, yloc, 10)==true) {
player.pause();
player2.play();
fill(0);
textSize(200);
fill(255, 0, 0, 204);
text(“You Lost”, 400, height/2);
noLoop();
port.stop();
}
}

boolean rectBall(float rx, float ry, float rw, float rh, float bx, float by, float d) {
if (by+d/2 >= ry-rh/2 && by-d/2 <= ry+rh/2 && abs(rx-bx) <= d/2) {
return true;
}
else if (bx+d/2 >= rx-rw/2 && bx-d/2 <= rx+rw/2 && abs(ry-by) <= d/2) {
return true;
}
float xDist = (rx-rw/2) – bx;
float yDist = (ry-rh/2) – by;
float shortestDist = sqrt((xDist*xDist) + (yDist * yDist));
xDist = (rx+rw/2) – bx;
yDist = (ry-rh/2) – by;
float distanceUR = sqrt((xDist*xDist) + (yDist * yDist));
if (distanceUR < shortestDist) {
shortestDist = distanceUR;
}
xDist = (rx+rw/2) – bx;
yDist = (ry+rh/2) – by;
float distanceLR = sqrt((xDist*xDist) + (yDist * yDist));
if (distanceLR < shortestDist) {
shortestDist = distanceLR;
}
xDist = (rx-rw/2) – bx;
yDist = (ry+rh/2) – by;
float distanceLL = sqrt((xDist*xDist) + (yDist * yDist));
if (distanceLL < shortestDist) {
shortestDist = distanceLL;
}
if (shortestDist < d/2) {
return true;
}
else {
return false;
}

Arduino code  and wiring for the capacitive touch sensors :

#include <CapacitiveSensor.h>

int capSensePinL = 4;
int capSensePinR = 5;
int capSensePinU = 6;
int capSensePinD = 7;
int touchedCutoff = 60;

static unsigned long currentTime = 0;

CapacitiveSensor cs_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor cs_2_5 = CapacitiveSensor(2,5);
CapacitiveSensor cs_2_6 = CapacitiveSensor(2,6);
CapacitiveSensor cs_2_7 = CapacitiveSensor(2,7);
void setup()
{
cs_2_4.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 – just as an example
Serial.begin(115200);
}

void loop() {
long start = millis();
long capSenseL = cs_2_4.capacitiveSensor(30);
long capSenseR = cs_2_5.capacitiveSensor(30);
long capSenseU = cs_2_6.capacitiveSensor(30);
long capSenseD = cs_2_7.capacitiveSensor(30);

Serial.print(capSenseL);
Serial.print(“, “);
Serial.print(capSenseU);
Serial.print(“, “);
Serial.print(capSenseR);
Serial.print(“, “);
Serial.print(capSenseD);
Serial.print(“, “);
Serial.println(” “);

if (abs (millis() – currentTime) > 100) {
if (capSenseL > 33) {
Serial.print(“1, “);
}
else {
Serial.print(“0, “);
}

if (capSenseR > 34){
Serial.print(“1, “);
}
else{
Serial.print(“0, “);
}
if (capSenseU > 31){
Serial.print(“1, “);
}
else {
Serial.print(“0, “);
}
if (capSenseD > 32){
Serial.print(“1”);
Serial.println(“”);
}
else{
Serial.print(“0”);
Serial.println(“”);
}
currentTime = millis();
}
}
uint8_t readCapacitivePin(int pinToMeasure){
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
byte bitmask;
if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
port = &PORTD;
ddr = &DDRD;
bitmask = 1 << pinToMeasure;
pin = &PIND;
}
if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
port = &PORTB;
ddr = &DDRB;
bitmask = 1 << (pinToMeasure – 8);
pin = &PINB;
}
if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
port = &PORTC;
ddr = &DDRC;
bitmask = 1 << (pinToMeasure – 13);
pin = &PINC;
}

*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);

*ddr &= ~(bitmask);
int cycles = 16000;
for(int i = 0; i < cycles; i++){
if (*pin & bitmask){
cycles = i;
break;
}
}

*port &= ~(bitmask);
*ddr |= bitmask;

return cycles;
}

Game visuals :

 

Game Credits :

Thor cartoon character – 

http://moziru.com/explore/Thor%20clipart%20flying/

Thor hammer – 

https://www.amazon.ca/Thors-Hammer-Avengers-2-Kids/dp/B00HA4X21I

Thor background music – Led Zepplin  – Immigrant song  – Youtube

Game over sound – Youtube

You lose sound – Youtube

 

Capacitive Touch Thor game is a project of IaaC, Institute for Advanced Architecture of Catalonia.
developed at Master in Advanced Architecture in 2017/18 by:

Students:

 

Deepankar Pahuja

Elene kavtaradze

Gabriele Jureviciute

Jasser Salas Castro

 

 

Faculty:

Angelos Chronis
Angel Muñoz