Serial Communication: A Joystick to my OOP Game

For this week’s assignment I built upon the assignment I created two weeks ago for the Object Oriented Programming game. I added a joystick to control the square’s movement, as well as a potentiometer so the user can choose a color for the square throughout the game.

It was challenging to figure out how to send the information from the Arduino to Processing, but I am a lot more confident now in understanding how the Arduino sends information to Processing and how Processing uses this information.

The user uses the potentiometer attached to a breadboard, while they can use the joystick to control the customized square’s movement.

Here is a video of the joystick and potentiometer in action:

Below is the code for the Processing sketch:

import processing.serial.*;
Serial myPort;
int xPos, yPos;
int xMove;
int bgValue;

float monsterSpotY = random(300, 560);
float monsterSpotX = random(200, 560);
float monsterRadius = 40;
int gravity = 1;
int coloramt = 0;

class Player { 
  float x;
  float y;
  float up;
  float down;
  float left;
  float right;
  float speed;
  float maxJump;
  float side;
  float prevY;
  Player() {
    x = 0;
    y = 580;
    up = 0;
    down = 0;
    left = 0;
    right = 0; 
    speed = 4;
    maxJump = 400;
    side = 20;
    prevY = 1;
  }    
  void display() {
    fill(255, 100, finalbgValue);
    rect(x, y, side, side);
  }    
  void update() {
    x = x + (xPos) * speed;
    if (prevY == 1) {
      gravity = 1;
      y = y + (yPos + gravity) * speed;
    }
    //prevY = y;
    println(prevY);
    println("ypos is " + yPos);
    if (x <= 10) {
      if (y >= height - 10) {
        x = 10;
        y = height - 10;
      } 
      else if (y < height - 10) {
        x = 10;
      }  
    } else if (x >= 590) { 
       if (y >= height - 10) {
        x = 590;
        y = height - 10;
       } else if (y < height - 10) {
        x = 590;
      }   
      } 
     else if  (y <= 0) {
      y = 0;
    } else if (y >= 590) {
      y = 590;
      prevY = 1;
    } 
    if (y <= 480) {
      prevY = 2;
    }  
    if (prevY == 2) {
      gravity = 3;
      y = y + (yPos + gravity) * speed;
    }   
  }
} 
class Monster {
  float x;
  float y;
  float radius;
  float ySpeed;  
  float monsterSpotY;
  float monsterSpotX; 
  color monsterColor;
  Monster() {

    radius = monsterRadius;
    ySpeed = random(2, 6);
    monsterSpotX = random(0, width);
    monsterSpotY = random(0, 100);
  }    
  void show() {
    //monsterColor = color(map(monsterSpotY, 0, height, 0, 255), map(monsterSpotY, 0, height, 100, 150), map(monsterSpotY, 0, height, 150, 255));      

    fill(coloramt);
    noStroke();
    ellipse(monsterSpotX, monsterSpotY, monsterRadius, monsterRadius);
  }
  void fall() {
    monsterSpotY = monsterSpotY + ySpeed;
    if (monsterSpotY > height) {
      monsterSpotY = random(-200, 300);
      monsterSpotX = random(0, width);
    }
  }
  void run() {
    fall();
    show();
  }
}
int counter = 0;
int gameState = 0;
int finalbgValue;
Player myPlayer;
Monster myMonsters[];
void setup() {
  size(600, 600);
  myPlayer = new Player();
  int monsterCount = 100;
  myMonsters = new Monster[monsterCount];
  for (int i = 0; i < monsterCount; i++) {
    myMonsters[i] = new Monster();
  }
  printArray(Serial.list());  
  String portname=Serial.list()[69];
  println(portname);
  myPort = new Serial(this, portname, 9600);

  //myPort.bufferUntil('\n');
}    

void draw() {
  background(255);
  
  // configuration state; user can select color. 
  if (gameState == 0) {
    textAlign(CENTER);
    fill(0);
    text("Color your hero!", width / 2, 200);
    fill(255, 100, bgValue);
    rectMode(CENTER);
    stroke(0);
    rect(width / 2, 290, 120, 120);
    fill(100);
    rect(width / 2, 395, 140, 30);
    fill(0);
    text("Click here to play!", width / 2, 400);
    println(mouseX);
    println(mouseY);
    if (mousePressed == true && mouseX > 230 && mouseX < 370 && mouseY > 383 && mouseY < 414) {
      gameState = 1;
      finalbgValue = bgValue;
    }
  } else if (gameState == 1) {
    myPlayer.display();
    myPlayer.update();    
    gameOver();
    fill(0);
    text(counter, width - 40, 30);
    int counterIncrement = counter /7;
    for (int i = 0; i < counterIncrement / 20; i+= 1) {
      myMonsters[i].run();
      if (i == counterIncrement / 20) {
        i = 0; 
        println(i);
      }
    }  
    counter++;
  } else if (gameState == 2) {
    textAlign(CENTER);
    background(255);
    text("You Died! Your score is: " + counter, width/2, height / 2);
    text("Click here to play again", width / 2, height / 2 + 30);
    if (mousePressed == true && mouseX > 230 && mouseX < 360 && mouseY > 320 && mouseY < 340) {
      gameState = 1;
      counter = 0;
      myPort.stop(); 
      // ^ myport clears serial before running setup again
      setup();
    }
  }
}  


void serialEvent(Serial myPort) {
  String temp=myPort.readStringUntil('\n');
  temp=trim(temp);
  if (temp != null) {
    int values[]=int(split(temp, ','));
    //split string of inputs by commas and end at line break "/n"
    if (values.length == 3) {
      xPos=(int)(values[0]);
      yPos=(int)(values[1]);
      bgValue = (int)(values[2]);
      xPos =(int) map(xPos, 0, 1023, -3, 3);
      yPos =(int) map(yPos, 0, 765, 3, -3);
      bgValue = (int) map(bgValue, 0, 1023, 0, 255);
    }
  }
}

void gameOver() {
  for (int i = 0; i < 30; i++) {
    if (myPlayer.x + 20  > myMonsters[i].monsterSpotX - 20 && myPlayer.x - 20 < myMonsters[i].monsterSpotX && myPlayer.y - 20 < myMonsters[i].monsterSpotY && myPlayer.y + 20 > myMonsters[i].monsterSpotY - 10) {
      gameState = 2;
    } else {
    }
  }
}

Below is the code for the Arduino:

void setup()
{
  Serial.begin(9600); 
  pinMode(4, INPUT); 
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A0, INPUT);

}

void loop() {
  int xValue = analogRead(A1);
  int yValue = analogRead(A2);
  int sensor = analogRead(A0);
   
  Serial.print(xValue);
  Serial.print(",");
  Serial.print(yValue);
  Serial.print(",");
  Serial.println(sensor);
}

 

Leave a Reply