![](https://static.wixstatic.com/media/a7e1ad_6796409e8e294f69a289e5d5d03676a1~mv2.png/v1/fill/w_504,h_403,al_c,q_85,enc_auto/a7e1ad_6796409e8e294f69a289e5d5d03676a1~mv2.png)
"In this lab, you will use serial communication to send multiple values between Arduino and another software program*, 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.".
The first thing I did was install the p5 Serial Control app and create an account for the p5 web editor. I used the P5 serial template that was provided to get started. I made sure to fill in the port name to match the one in the Serial Control. Once the port opened I moved on to my Arduino circuit and code.
The circuit has two inputs, a potentiometer and another variable resistor; I used a slide potentiometer. The output for the circuit is two different colored LEDs.
I connected the slide potentiometer to a 10k ohm fixed resistor and then connected the slider to pin A1. I used my potentiometer from the last lab and connected it to pin A0. Each LED has a 100 ohm resistor
V= I*R ---->. R=V/I -----> R= Vs-Vf/I -----> 5-2-2/.02 ----> 50 ohms
The red LED is connected to pin ~10 and the green LED is connected to pin ~9
![](https://static.wixstatic.com/media/a7e1ad_3a794e88a9b642a3b34555d183b13932~mv2_d_2149_2573_s_2.jpg/v1/fill/w_980,h_1173,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/a7e1ad_3a794e88a9b642a3b34555d183b13932~mv2_d_2149_2573_s_2.jpg)
The pictures below show the completed circuit. The next step was to write the Arduino Code.
The code below sends the data from the potentiometer and slider as an ASCII string to P5. I needed to make sure that the data was formatted correctly before moving on. When my numbers in the serial monitor looked something like this : 1023,1023
I then wrote the code that accepted data from P5 to Arduino. I knew that I wanted a red LED to turn on if the user loses the game, and a green LED if the user wins the game. If the incoming byte is a capital 'R' , then red light turns on. If the incoming byte is a capital 'G', then the green light turns on.
I will also include a Link to my p5 code: https://editor.p5js.org/FernV/sketches/jBQv2bDdr
The serial game I created is a combination between golf and a maze game. The first thing I did was create every individual rectangle/wall at different locations. There is a starting and end point, the white ball can only reach the end through one route. I knew I needed if statements but I wasn't sure how to set them up. I found this p5 code ( :https://editor.p5js.org/kchung/sketches/Bya-96Xz4) for a game that also involved avoiding obstacles and their code helped me create my own if statements. I created many if statements, one for each rectangle/wall in the game. The if statements check if the coordinates of the white ball are the same or very close to the coordinates of a rectangle/wall. The potentiometer controls the y-axis /height and it moves the white ball up and down. The slider controls the x-axis/width and it moves the white ball left and right.
If the white ball hits a wall, then the red light turns on, signaling that you lose, once you hit a wall, even if you reach the end, the green light won't turn on. If the ball makes it through the maze without hitting a wall and it reaches the black ball/hole, then the green light will turn on, which signals that you win. .
Below I included two videos that show losing/winning
Losing Scenario:
Winning Scenario: