top of page

Lab 4: Serial Game

In this lab, you will use serial communication to send multiple values between Arduino and another software program*. P5JS is suggested but you are welcome to use Unity, MaxMSP, Processing, or another program that can work with Arduino. Regardless of the platform you choose, you must create something interactive with both inputs and outputs that has either visual or auditory output (or both) based on sensor data.

Setting up P5

First, I downloaded the P5 serial control app. I found my arduino as one of the things that was connected and selected it. Then, I created a circuit on a breadboard with a potentiometer and a photoresistor as analog inputs, and two LEDS as outputs. Then I set up my serial values to be read in, separated by a single comma. Then, I opened the P5 web editor and wrote inside the serial event function to parse through each line as it came in, basically to read in the information from the arduino.

IMG_2066_edited.jpg

Circuit diagram

IMG_2056.HEIC
IMG_2058.HEIC

My circuit with a potentiometer, photoresistor and two LEDS (bright blue and red)

My ardiuno code. Reading in the data from the analog inputs, then writing to the LEDS based off of P5.

After I got the ardiuno code and P5 to link up, I could see the serial values printing on the P5 web editor. Based off of those values that I was getting, I wrote my code to reflect that. At first, I tested everything out by making various values of an ellipse change based off of the potentiometer values and the photoresistor values.

​

Then I decided on a simple game to make. I liked using ellipses, so I used those for the parts of my game. The idea was to move around an ellipse right and left by the potentiometer, and up and down by the photoresistor. Then, by moving the ellipse, the goal was to match it up with other ellipses on the screen by moving it around. After one ellipse was touched, it would disappear and another one would show up. You win the game by hitting the last ellipse. During the game, the red LED is on. Once you win the game, a winning message appears, and the bright blue LED turns on. Code-wise, I wrote the letter 'A' for a win, and 'B' for anything else to my arduino.

All of my code for the game was done within the draw function. I thought about making a separate function, but that wasn't necessary and made everything a little more complicated. I used a counter so that it was easy to move onto the next ellipse, and finally when the game was won.

The footage on the left is me controlling the game, and the video on the right is the screen recording of whats happening in the P5 web editor.

This is a video of both me controlling the game, as well as the actual game on the P5 web editor. 

(I accidentally switched up my resistor for the red LED, so I replaced it with a 200 Ohms resistor afterwards and it became much brighter)

© 2023 by Meganne Jimenez. Proudly created with Wix.com

bottom of page