Tag Archives: programming

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;
}

Installing OpenCV

So tinkering with OpenCV the past day has been fun.  This past summer, a co-worker and I made an application to crop images and resize them automatically to speed up booking at the jail we work at.

The first problem was face detection.  To create a method, we first invert the image.  Then run a line from left to right until the background color changes.  Do the same thing from right to left and then find the center.  Do that several times through the picture would lead us to a good center location for the face.

The second problem was that we did all this with Windows API/GDI.  These methods were very low level and difficult to run simple methods on.

For some reason, we never found OpenCV until reading a slashdot article last night.  This library is awesome for face detection and other visual work.  I use a Linux box at home and the work computers use Windows, so I like that code can be compiled with only changes to the compiler going between each computer.

Installation

The install in linux was very easy.  Ubuntu has great documentation for installing anything with apt.  Since I’m using Mint 12 (gotta get away from Gnome 3!), I was able to follow this guide.

For Windows, it was a bit more tricky.  I read guides walking me through CMake and such.  Fortunately, I found that OpenCV 2.0’s installer does not need to be compiled, just extracted.  Using this guide I was able to get it work.  To simplify things, I’ve put two zips on my server for anyone to use.

  1. OpenCV_INSTALL.zip has the installer for OpenCV 2.0, CMake guide for those using Visual C++, and a sample codeblocks project.
  2. OpenCV_DONE.zip includes the extracted source and a sample codeblocks project.

Let me know if you have any issues.  Remember, for the codeblocks project to compile, you’ll have to sent compiler search directory to [src]/include/opencv and the linker search directory to [src]/lib

Enjoy!