Guitar Hero on a budget

For this weeks project I wanted to play with Arduino and create a really low budget version of the famous guitar hero game.

In the game, player has to press a button of the color corresponding to the note which is currently at the strum line. In my game I introduced 3 buttons Red, Green and Blue, which work on the 3 paths of the corresponding colors. When a note falls down to the bottom of the screen the user needs to press the button. Notes appear in random time intervals.

The hardest part of this weeks assignment was picking right time windows for the button reads. Calibrating that to the Arduino module took me quite some time.

Here is my Arduino setup:

Here is me playing the game:

And at last my code:

import processing.serial.*;

Serial myPort;
Note[] notes = new Note[5];
int counter = 0;
int[] positions = {100, 250, 400};
int points = 0;

void setup() {
  size(500, 400);
  frameRate(30);
  textSize(30);
  String pName = Serial.list()[2];
  myPort = new Serial(this, pName, 9600);
}

void draw() {
  drawBackground();
  if (frameCount%30 == 0) {
    addNote();
  }
  drawNotes();
}

void serialEvent(Serial myPort) {
  String msg = myPort.readStringUntil('\n');
  msg = trim(msg);
  if (msg != null) {
    int btnVals[]=int(split(msg, ';'));
    if (btnVals.length == 3) {
      if (btnVals[0] == 1) {
        checkNote("red");
      }
      if (btnVals[1] == 1) {
        checkNote("green");
      }
      if (btnVals[2] == 1) {
        checkNote("blue");
      }
    }
  }
  myPort.write('0');
}

void checkNote(String col) {
  int xPos = 0;
  switch (col) {
  case "red":
    xPos = 100;
    break;
  case "green":
    xPos = 250;
    break;
  case "blue":
    xPos = 400;
    break;
  }

  for (int i=0; i<notes.length; i++) {
    if (notes[i] != null) {
      if (notes[i].x == xPos && notes[i].y > 350) {
        notes[i] = null;
        points+=1;
      }
    }
  }
}

void drawBackground() {
  background(30);
  noStroke();  
  fill(90, 0, 0);
  rect(50, 0, 100, 400);

  fill(10, 90, 0);
  rect(200, 0, 100, 400);

  fill(0, 20, 90);
  rect(350, 0, 100, 400);

  fill(80);
  rect(0, 350, 500, 50);

  fill(255);
  text(points, 5, 30);
}

void drawNotes() {
  if (notes != null) {
    for (int i=0; i<notes.length; i++) {
      if (notes[i] != null) {
        notes[i].drawNote();
        if (notes[i].y > height) {
          notes[i] = null;
          points = 0;
        }
      }
    }
  }
}

void addNote() {
  notes[counter] = new Note(positions[int(random(3))]);
  counter = (counter+1)%5;
}

class Note { 
  int x;
  int y = 0;
  Note (int xPos) {  
    x = xPos;
  } 
  void drawNote() {
    y += 5;
    switch(x) {
    case 100:
      stroke(255, 0, 0);
      break;
    case 250:
      stroke(0, 255, 0);
      break;
    case 400:
      stroke(0, 0, 255);
      break;
    }

    strokeWeight(50);
    point(x, y);
    strokeWeight(10);
    stroke(255);
    ellipse(x, y, 20, 20);
  }
}
const int redBtn = 4;
const int greenBtn = 3;
const int blueBtn = 2;
 
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(redBtn, INPUT);
  pinMode(greenBtn, INPUT);
  pinMode(blueBtn, INPUT);
}
 
void loop() {
  if (Serial.available() > 0) {
    char inByte = Serial.read();
    
    int red = digitalRead(redBtn);
    int green = digitalRead(greenBtn);
    int blue = digitalRead(blueBtn);
    
    Serial.print(red);
    Serial.print(';');
    Serial.print(green);
    Serial.print(';');
    Serial.println(blue);
  }
}

 

One thought on “Guitar Hero on a budget”

Leave a Reply