Photocell Controller

I decided to use photocell sensors as controllers for my game!

There are 2 sensors. By hovering your hand over the sensor, blocking enough light on the left sensor makes the character jump and blocking the right sensor will make the character move forward. If you die and end up on the game over screen, placing your hand over the left sensor will also serve as playing again. The controls are still kind of finicky but they work nevertheless.

Setup:

2015-11-15 19.34.02

 

 

 

 

 

 

 

Diagram:

photocell controller_bb

 

 

 

 

 

 

 

 

Code:

Arduino

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int myByte = Serial.read();
  Serial.print(analogRead(A0)-200);
  Serial.print(",");
  Serial.println(analogRead(A1)-200);
}

Processing

void serialEvent(Serial myPort) {
  //playerPositionX = map(myPort.read(), 0 , 1000 ,0 , width);
  String v = myPort.readString();
  println(v);
  float vals[] = float(v.split(","));
  float x = vals[0];
  float z = vals[1];
  if (x > 50) {
    moveRight = true;
  } else if (x < 40) {
    moveRight = false;
  }
  if (gameOver) {
    if (z > 50) {
      gameOver = false; 
      player = new Player();
      background = new Background();
      obstacle = new Obstacle();
      score = 0;
      moveRight = false;
      playerPositionX = 50;
    }
  } else {
    if (z > 50) {
      if (!play) {
        play = true;
      } else {
        player.jump();
      }
    }
  }
  println("playerPositionX: " + x);
  //println("playerPositionZ: " + z);
  myPort.write('x');
}

 

Leave a Reply