First step towards the mid-term project

For the mid-term project, Mint and I decided to work on a game.
Without exposing too much about the the project – we are working on a game that will combine physical cannon, and a digital targets.

The cannon is sort of a shaker. User needs to shake to cannon to a certain point in order to shoot it. Needless to say, shaking and aiming at the same time will make the game challenging, and hopefully interesting and enjoyable.

We used the built-in accelerometer on Arduino 101, and we wrote Arduino code that will identify when user shakes the cannon, and when the cannon was shakened  long enough, and it is ready to fire.

It sounds a little abstract, but I’m sure that it will be very clear when the game will be ready.

This is what we have so far:

The making of ‘the pcomp shooter’: part 1 from Dror Ayalon on Vimeo.

 

This is the Arduino code we used (the examples on www.arduino.cc were very helpful):

#include "CurieIMU.h"
#include "pitches.h"

float oldx, oldy, oldz, newx, newy, newz = 0;   //scaled accelerometer values
float axp, ayp, azp;
int bullet = 0;

int bang[] = {
 NOTE_B3, NOTE_C4
};

int noteDurations[] = {
 2, 2
};

void setup() {
 Serial.begin(9600); // initialize Serial communication
 while (!Serial);    // wait for the serial port to open

 // initialize device
 Serial.println("Initializing IMU device...");
 CurieIMU.begin();

 // Set the accelerometer range to 2G    -2 to 2
 CurieIMU.setAccelerometerRange(2);
}

void loop() {

 // read accelerometer measurements from device, scaled to the configured range
 CurieIMU.readAccelerometerScaled(newx, newy, newz);

 // percentage
 axp = 100 - (oldx / newx) * 100;
 ayp = 100 - (oldy / newy) * 100;
 azp = 100 - (oldz / newz) * 100;

 // display tab-separated accelerometer x/y/z values
 Serial.print("axp: ");
 Serial.print(axp);
 Serial.print("%");
 Serial.print("\t");

 Serial.print("ayp: ");
 Serial.print(ayp);
 Serial.print("%");
 Serial.print("\t");

 Serial.print("azp: ");
 Serial.print(azp);
 Serial.print("%");
 Serial.print("\t");

 Serial.print("bullet: ");
 Serial.print(bullet);
 Serial.print("%");
 Serial.print("\t");

 if (( axp > -100) && ( axp < 100 )) { bullet = bullet - 0.5; } else { bullet = bullet + 4; Serial.print("SHAKING!!!"); Serial.print("\t"); } if (bullet >= 100) {
   Serial.print("BANG!!!!!");
   Serial.print("\t");

   int noteDuration = 1000 / noteDurations[0];
   tone(8, bang[0], noteDuration);
   int pauseBetweenNotes = noteDuration * 0.5;
   Serial.print(bang[0]);
   delay(pauseBetweenNotes);
   noTone(8);

   noteDuration = 1000 / noteDurations[1];
   tone(8, bang[1], noteDuration);
   pauseBetweenNotes = noteDuration * 0.5;
   Serial.print(bang[1]);
   delay(pauseBetweenNotes);
   noTone(8);

   bullet = 0;
 }

 Serial.println();

 oldx = newx;
 oldy = newy;
 oldz = newz;

 delay(5);
}

What’s next?

  • To build the graphical part of the game in p5.js.
  • To solve the sound problem: We couldn’t find a way to make the sound louder from the speaker connected to the Arduino.
  • To do some fabrication work and to make sure that the cannon has the ‘right’ feeling in the users’ hands.
  • To play the game over and over, make some changes, and make sure that it’s fun.

Published by

Dror Ayalon

@drorayalon

One thought on “First step towards the mid-term project”

  1. The sound from the Arduino won’t get louder without a larger speaker, an amplifier, and a larger housing. Better to play your sound from P5.js

Leave a Reply

Your email address will not be published. Required fields are marked *