Tag Archives: device

Arduino Game

At times, I have fun creating devices.  The similarity to programming amazes me and the ability to be able interact with the physical world is a lot of fun.  I took my standard game, a tank that moves side to side and shoots objects from the sky (think Space Invaders).  Here’s the shopping list.

  1. Microcontroller – Arduino Uno
  2. Display – Nokia 5110 LCD
  3. Sensor – ADXL334 Triple Axis Acelerometer
  4. Tactile Switch and misc wiring/resistors.

So for a relatively small amount of shopping, you have the ability to make an interactive game.  The wiring basically merges basic wiring for the tactile switch, standard input for triple axis acelerometer, and

Enough talk, here’s a picture:

Here’s a video of it:

And here’s the code I wrote:

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
 
// Display
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
 
// Setup
int pressed = false;
int times = 0;
int led = 13;
int but = 12;
int val = 0;
int count = 0;
 
// Tank size and direction
int tx = 0;
int ty = 0;
int tw = 10;
int th = 4;
int td = 0;
 
// Bullet
bool bAlive = false;
int bx = 0;
int by = 0;
int bw = 3;
int bh = 3;
 
// Accelerometer
const int powerpin = 18;
const int groundpin = 14;
const int xpin = A3;
int xMoveInit = 0;
 
void setup()
{
  Serial.begin(9600);
   
  pinMode(led, OUTPUT);
  pinMode(but, INPUT);
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
   
  digitalWrite(groundpin, LOW); 
  digitalWrite(powerpin, HIGH);
   
  display.begin();
  display.setContrast(50);
  
  tx = (display.width()/2)-(tw/2);
  ty = display.height()-10-th;
   
  xMoveInit = analogRead(xpin);
}
 
void loop()
{
  // clear
  display.clearDisplay();
  // x,y,h,w,color
   
  // movement
  if(isPressed() == true)
  {
    //display.fillRect(30,12,24,24,BLACK);
    if(bAlive == false)
    {
      bAlive = true;
      bx = tx+(tw/2);
      by = ty+bh;
    }
  }
   
  // draw
  drawGround();
  drawTank();
  drawBullet();
   
  // show
  display.display();
   
  // wait
  delay(10);
}
 
void drawTank()
{
  doMove();
   
  tx += td;
   
  if(tx+tw+1 >= display.width())
  {
    tx = display.width()-tw-1;
  }
   
  if(tx <= 0)
  {
    tx = 1;
  }
   
  display.drawRect(tx,ty,tw,th,BLACK);
}
 
void drawBullet()
{
  if(by+bh <= 0)
  {
    bAlive = false;
    return;
  }
  by -= 2;
 
  display.fillRect(bx,by,bw,bh,BLACK);
}
 
void drawGround()
{
  int lx = 0;
  int ly = display.height()-10;
  int lw = display.width();
  int lh = 10;
   
  display.fillRect(lx,ly,lw,lh, BLACK);
}
 
void doMove()
{
  int xMoveTemp = analogRead(xpin);
  int diff = xMoveInit-xMoveTemp;
   
  // DEBUG
  Serial.print("Diff: ");
  Serial.print(diff);
  Serial.println();
   
  if(diff >= 20)
  {
    if(diff >= 40)
    {
      td = -2;
      return;
    }
    td = -1;
    return;
  }
   
  if(diff <= -20)
  {
    if(diff <= -40)
    {
      td = 2;
      return;
    }
    td = 1;
    return;
  }
   
  td = 0;
}
 
bool isPressed()
{
  val = digitalRead(but);
  if(val == LOW and pressed == false) {
    return false;
  }
  return true;
}