lab 4 – game controller


I used my mushroom sensor box from the last lab, the process of making it including videos of the working circuit and its schematic can be found here

Using my mushroom sensor box from the last lab, I started by making sure that my Arduino nano was getting clean data, then using serial.Write() I could see what values I was getting in the p5.serialcontrol app. I used the starter code given to see if my value was going into the p5 web editor.

After making sure that I was getting one value in, I changed my code so it was using serial.print() instead, and had to convert the ascii characters to numbers by using serial.readLine() in the p5.js web editor.

I then used the split function in the p5.js web editor to get the two input values, and changed the draw function so the potentiometer moved the ball up and down, and the fsr changed the opacity of the ball. I flipped the output values of the fsr so that when it wasn’t being pressed it had a resting value of 255, and when pressed it went down to 0. This aligned with alpha values better.

Since my sensors were hooked up and working, I started working on my game. Because my main sensor was a potentiometer, I thought I could make a spin on flappy bird and have a character controlled by a potentiometer to go through gates, instead of just a space bar. I followed the coding train’s tutorial on flappy bird and the dinosaur chrome game to make this. I started by laying out the basic shapes and making sure that I had everything linked correctly.

I then found some images online of a club penguin character and background and used the preload function to add them in.

I then continued to follow the tutorial and added in the double bars and added collision interaction so the game ended when the character ran into one of the bars.

I then needed to add something for the fsr to do, so I decided to add in another collision obstacle but this time as a puffle, which is a pet in club penguin, and make it so when the character ran over it and you pressed the fsr at the same time it started keeping another score in the top right.

Finished product!!
// include the neopixel library
#include <Adafruit_NeoPixel.h>
int ledPin = 3; // the pin the LED data line is connected to
int numLED = 6; // number of LEDs you are controlling
// call the constructor
Adafruit_NeoPixel leds(numLED, ledPin, NEO_GRB + NEO_KHZ800);
int sensorPinPot = A0; // pin the pot is attached to
int sensorPinFlex = A1; // pin the flex is attatched to
int analogValPot; // variable to hold analog pot value
int analogValFlex; // variable to hold analog flex value
unsigned long off; // variable for turning the LEDs off
void setup() {
// start communication with the LEDs
leds.begin();
// it's necessary to call show() to write the values to the lights
// in this case, right after begin(), it just insures that they are all off
leds.show();
// set the color for 'off'
off = leds.Color(0, 0, 0);
Serial.begin(9600);
}
void loop() {
// 1024 values mapped to 5 LEDs means that we could assign
// 204 values to each pixel to correspond to each value from the sensor
// read the value on the analog input pin
analogValPot = analogRead(sensorPinPot);
delay(1);
analogValFlex = analogRead(sensorPinFlex);
analogValFlex = map(analogValFlex, 150, 210, 255, 0);
// analogValPot = map(analogValPot, 0, 1023, 0, 255);
Serial.print(analogValPot);
Serial.print(",");
Serial.println(analogValFlex);
//getting data from the read values
int mappedVal; // local var to map the brightness
int mapVal;
leds.fill(off); // blank the LEDs, but don't write it
// Serial.println(analogValPot);
if (analogValPot < 150) { // if the value is in this range
mappedVal = map(analogValPot, 0, 150, 0, 255); // map the value to 0-255
leds.setPixelColor(0, mappedVal, 0, mappedVal); // write the pixel color
} else if (analogValPot >= 151 && analogValPot <= 350) { // repeat for each pixel
leds.setPixelColor(0, 255, 0, 255); // set all previous pixels as on
mappedVal = map(analogValPot, 151, 350, 0, 255);
leds.setPixelColor(1, mappedVal, 0, mappedVal);
} else if (analogValPot >= 351 && analogValPot <= 560) {
leds.setPixelColor(0, 255, 0, 255);
leds.setPixelColor(1, 255, 0, 255);
mappedVal = map(analogValPot, 351, 560, 0, 255);
leds.setPixelColor(2, mappedVal, 0, mappedVal);
} else if (analogValPot >= 561 && analogValPot <= 755) {
leds.setPixelColor(0, 255, 0, 255);
leds.setPixelColor(1, 255, 0, 255);
leds.setPixelColor(2, 255, 0, 255);
mappedVal = map(analogValPot, 561, 755, 0, 255);
leds.setPixelColor(3, mappedVal, 0, mappedVal);
} else if (analogValPot >= 756 && analogValPot <= 900) {
leds.setPixelColor(0, 255, 0, 255);
leds.setPixelColor(1, 255, 0, 255);
leds.setPixelColor(2, 255, 0, 255);
leds.setPixelColor(3, 255, 0, 255);
mappedVal = map(analogValPot, 756, 900, 0, 255);
leds.setPixelColor(4, mappedVal, 0, mappedVal);
} else if (analogValPot >= 901) {
leds.setPixelColor(0, 255, 0, 255);
leds.setPixelColor(1, 255, 0, 255);
leds.setPixelColor(2, 255, 0, 255);
leds.setPixelColor(3, 255, 0, 255);
leds.setPixelColor(4, 255, 0, 255);
mappedVal = map(analogValPot, 901, 1023, 0, 255);
leds.setPixelColor(5, mappedVal, 0, mappedVal);
}
leds.show(); // show the color
delay(2);
// if(analogValPot >= 820 && analogValFlex > 180) {
// leds.setPixelColor(0, 255, 0, 0);
// leds.setPixelColor(1, 255, 0, 0);
// leds.setPixelColor(2, 255, 0, 0);
// leds.setPixelColor(3, 255, 0, 0);
// leds.setPixelColor(4, 255, 0, 0);
// leds.setPixelColor(5, 255, 0, 0);
// }
// else if(analogValPot >= 820 && analogValFlex < 120) {
// leds.setPixelColor(0, 0, 0, 255);
// leds.setPixelColor(1, 0, 0, 255);
// leds.setPixelColor(2, 0, 0, 255);
// leds.setPixelColor(3, 0, 0, 255);
// leds.setPixelColor(4, 0, 0, 255);
// leds.setPixelColor(5, 0, 0, 255);
// }
// leds.show();
// delay(2);
}
view raw gameController hosted with ❤ by GitHub

p5.js code:

https://editor.p5js.org/emmykova/sketches/xpB5Xi5vk

Leave a comment