Space invaders with motorized feedback

timothykc's picture

 

My homework is a video game in which a potentiometer and force sensor are used to control a space invaders like scenario.
When the game ends the motor turns to provide a jarring physical feedback. (kindof)
 
Game code was found online, and adapted to work with arduino board input/output.
 
/* Physical Pixel
 
 An example of using the Arduino board to receive data from the 
 computer.  In this case, the Arduino boards turns on an LED when
 it receives the character 'H', and turns off the LED when it
 receives the character 'L'.
 
 The data can be sent from the Arduino serial monitor, or another
 program like Processing (see code below), Flash (via a serial-net
 proxy), PD, or Max/MSP.
 
 The circuit:
 * LED connected from digital pin 13 to ground
 
 created 2006
 by David A. Mellis
 modified 14 Apr 2009
 by Tom Igoe and Scott Fitzgerald
 
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/PhysicalPixel
 
 MODIFIED BY TIMOTHY CHAROENYING OCT 2011 TO INCORPORTATE INPUT AND ADDITIONAL OUTPUT.
 */
 
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into
int motorPin = 9; // select the pin for the Motor
int val = 0;
int speakerPin = 7; // select pin for the piezo buzzer
 
const int potPin = A0;      // sensor to control pot
const int secondPin = A5;    // sensor to control 2nd
const int thirdPin = A2;     // sensor to control 3rd
 
 
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(speakerPin, OUTPUT); // declare piezo pin as OUTPUT
 
}
 
void loop() {
 
  Serial.print(analogRead(potPin));
  Serial.print(",");
  Serial.print(analogRead(secondPin));
  Serial.print(",");
  Serial.println(analogRead(thirdPin));
  
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
 
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
      analogWrite(motorPin, 0);
    }
     if (incomingByte == 'D') {
    digitalWrite(speakerPin, HIGH);
    delay(100);
    digitalWrite(speakerPin, LOW);
     }
     if (incomingByte == 'E') {
      analogWrite(motorPin, 254);
    digitalWrite(speakerPin, LOW);
    }
    if (incomingByte == 'X') {
      analogWrite(motorPin, 0);
    }
     if (incomingByte == 'B') {
        tone (speakerPin,200, 10);
        delay(1);
    }
    
 
     }
}
 
 
/* Original Video Game Code 
Nov 04, 1007  http://codelikezell.com/create-a-sick-game-with-processing/
 
 
Modifications by Timothy Charoenying OCT 2011 to change controls to potentiometer and force sensor. 
I also added sound, LED, and motorized feedback
 
*/
 
import processing.serial.*; 
 
float potValue = 0;        // pot
float secondValue = 0;      // 2nd
float thirdValue = 0;       // 3rd
 
PFont fontA;
  int sphereDiameter = 10;
  boolean shoot = false;
  Serial port; 
  int blast = 0;
  int randx()
  {
    return int(random(600));
  }
  
  int[] sphereXCoords = { randx(), randx(), randx(), randx(), randx() };
  int[] sphereYCoords = { 0, 0, 0, 0, 0 };
  
  void setup()
  {
    size(600,620);
    println(Serial.list()); 
    port = new Serial(this, Serial.list()[0], 9600); 
 
  }
  
  void draw()
  {
    background(0);
    fill(color(0,255,0));
    stroke(color(0,255,0));
    triangle(potValue-8, 580, potValue+8, 580, potValue, 565);
    fill(color(255, 0,0));
    stroke(color(255,0,0));
  
    if(shoot==true)
    {
      float z = potValue;
      int a = (int) z;
      sphereKiller(a);
      shoot = false;
    }
    shootem ();
    sphereDropper();
    warning();  
    gameEnder();  
  }
  
  void shootem ()
  {
   float b = secondValue;
   int blast = (int)b;
   if (blast < 50) {
    shoot = true;
  }
   else {
    shoot = false;
   } 
  }  
  void sphereDropper()
  {  
    stroke(255);
    fill(255);
    for (int i=0; i<5; i++)
    {
      ellipse(sphereXCoords[i], sphereYCoords[i]++,
              sphereDiameter, sphereDiameter);
    }
  }
  
  void sphereKiller(int shotX)
  {
    boolean hit = false;
    for (int i = 0; i < 5; i++)
    {
      if((shotX >= (sphereXCoords[i]-sphereDiameter/2)) && 
         (shotX <= (sphereXCoords[i]+sphereDiameter/2)))
      {  
        hit = true;
        line(potValue, 565, potValue, sphereYCoords[i]);
        ellipse(sphereXCoords[i], sphereYCoords[i],
                sphereDiameter+25, sphereDiameter+25);
        sphereXCoords[i] = randx();
        sphereYCoords[i] = 0;
        port.write('B'); 
        port.write('H'); 
        delay(2);      
        port.write('L'); 
 
      }    
    }
  
    if(hit == false)
    {
      line(potValue, 565, potValue, 0);
    }  
  
  }
  
  void warning()
  {
    for (int i=0; i< 5; i++)
    {
      if(sphereYCoords[i]>400)
      {
        fill(color(255,0,0));
        port.write('H'); 
        port.write('D'); 
      }
    }  
    
  }
 
  void gameEnder()
  {
    for (int i=0; i< 5; i++)
    {
      if(sphereYCoords[i]==600)
      {
        fill(color(255,255,255));
        background(255);
        port.write('E'); 
        delay(200);
        port.write('X'); 
        noLoop();
      }
    }  
    
  }
 
void serialEvent(Serial port) { 
 // get the ASCII string:
 String inString = port.readStringUntil('\n');
 
 if (inString != null) {
 // trim off any whitespace:
 inString = trim(inString);
 // split the string on the commas and convert the 
 // resulting substrings into an integer array:
 int [] value = int (split(inString, ","));
 // if the array has at least three elements, you know
 // you got the whole thing.  Put the numbers in the
 // color variables:
 if (value.length >=3) {
 // map them to the range 0-255:
 potValue = map(value[0], 0, 1023, 0, 600);
 secondValue = map(value[1], 0, 1023, 0, 255);
 thirdValue = map(value[2], 0, 1023, 0, 255);
 }
 }
 }
0
Your rating: None