Lab submission 6 - DC Motor

Submitted by stlim on Tue, 03/12/2013 - 22:49

Description

I made a Duck Hunt game. Two potentiometers detects the virtual gun's orientation. The gun gives the user vibration feedback to simulate a machine gun.

 

Components Used

- 1 DC motor

- 1 diode (1N4004)

- 1 transistor (TIP120)

- 1 1k resistor

- 1 10k resistor

- 2 potentiometers

- 1 FSR

- 1 arduino uno board

 

Video Demo

http://youtu.be/wROHrBMEM4k

 

Code: Arduino

 

const int FSR_THRESHOLD = 500;
 
int pot1Pin = 3;
int pot1Val = 0;
 
int pot2Pin = 4;
int pot2Val = 0;
 
int fsrPin = 5;
int fsrVal = 0;
 
int motorPin = 9;
 
char shoot = ' ';
 
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  fsrVal = analogRead(fsrPin);
  pot1Val = analogRead(pot1Pin);
  pot2Val = analogRead(pot2Pin);
  
  if(fsrVal>FSR_THRESHOLD) {
//    analogWrite(motorPin, 25); // analogWrite can be between 0-255
    
    if(pot1Val/30<12) {
      if(pot2Val/20<10) {
        shoot = 'a';
      }
      else if(pot2Val/20<14) {
        shoot = 'b';
      }
      else {
        shoot = 'c';
      }
    }
    else {
      if(pot2Val/20<10) {
        shoot = 'd';
      }
      else if(pot2Val/20<14) {
        shoot = 'e';
      }
      else {
        shoot = 'f';
      }
    }
    
    Serial.println(shoot);
  }
}
 

Code: Processing

 

import processing.serial.*;
 
import processing.serial.*;
 
String portname = "/dev/tty.usbmodem411";
Serial port;
 
char val = 0;
int inputX = 0;
int inputY = 0;
 
int duckX = 0;
int duckY = 0;
 
PImage duck;
 
PImage bg;
int bgOldW;
int bgOldH;
int bgNewW;
int bgNewH;
 
PImage crosshair;
int cW;
 
boolean started = false;
 
PImage intro;
 
void setup() {
  size(displayWidth, displayHeight);
  frameRate(30);
  smooth();
  noStroke();
  textSize(40);
  
  //  draw three buttons
  colorMode(RGB, 255);
  
  //  open serial port
  port = new Serial(this, portname, 9600);
  
  //  set background
  bg = loadImage("bg.png");
  bgOldW = bg.width;
  bgOldH = bg.height;
  bg.resize(displayWidth, displayHeight);
  bgNewW = bg.width;
  bgNewH = bg.height;
//  background(bg);
  
  //  set duck
  duck = loadImage("duck.png");
  duck.resize(duck.width * bgNewW/bgOldW, duck.height * bgNewH/bgOldH);
  
  //  set crosshair
  crosshair = loadImage("crosshair.png");
  crosshair.resize(crosshair.width * bgNewW/bgOldW, crosshair.height * bgNewH/bgOldH);
  cW = crosshair.width;
  
  //  set intro
  intro = loadImage("intro.jpg");
  intro.resize(displayWidth, displayHeight);
}
 
void draw() {
  background(intro);
  if(started) {
//  delay(3000);
  background(bg);
  showDuck((int)random(0, displayWidth-duck.width), (int)random(0, displayHeight-duck.height), (int)random(700, 1500));
//  text(val, 100, 100);
  }
}
 
void mouseClicked() {
  if(!started) {
    started = true;
  }
}
 
void showDuck(int x, int y, int len) {
  int start = millis();
  image(duck, x, y);
  delay(len);
}
 
void showCrosshair(char c) {
  int x = 0;
  int y = 0;
  
  switch(c) {
    case 'a':
      x = displayWidth/2 - cW*2;
      y = 0;
      break;
    case 'b':
      x = displayWidth/2 - cW/2;
      y = 0;
      break;
    case 'c':
      x = displayWidth/2 + cW;
      y = 0;
      break;
    case 'd':
      x = displayWidth/2 - cW*2;
      y = displayHeight / 2;
      break;
    case 'e':
      x = displayWidth/2;
      y = displayHeight / 2;
      break;
    case 'f':
      x = displayWidth/2 + cW;
      y = displayHeight / 2;
      break;
    default:
      break;
  }
  
  image(crosshair, x, y);
}
 
void serialEvent(Serial p) {
  val = port.readChar();
  showCrosshair(val);
}
dh.JPG
photo.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.