nyu_torch.gif

Blog

Physical Computing 9.19.19

Button Switching between 2 LEDs

Working on the labs this week was very satisfying. We began work with the Arduino Nano. The first thing to know about this is that it is EXTREMELY hard to plug into the breadboard for the first time, so make sure that you have time to work it in there before you need to get started.

The first lab with the button to switch between the two lights went well. I had troubles initially because I only partially plugged my Arduino into the breadboard. I must’ve hit some halfway-type of plug-in because I was able to upload my initial code to the Arduino, but my second wouldn’t upload. When the second upload reached about 20 minutes in, I looked at the Arduino to realize that it wasn’t fully connected. SO MAKE SURE THAT YOUR ARDUINO IS FULLY PLUGGED IN!

Video of an Arduino on a breadboard connected to a button that switches between a red and a yellow LED when pressed.

Potentiometer controlling LED Brightness

Once I got that Arduino fully plugged in, things worked (obviously) a lot smoother. The next lab was to use a Potentiometer in an analog slot to control the brightness on an LED.

Video of an Arduino Nano connected to a breadboard that has a potentiometer controlling the brightness of a red LED.

Using the ideas from these two labs, I thought that it would be fun to create a game that worked on these fundamentals.

Rock Paper Scissors (FAIL)

The idea here was that I would take readings from two potentiometers, translate the readings from each into one of 6 values (rock1,, rock2 paper1, paper2, scissors1, or scissors2), create if/else statements to set conditions for winning and have it all hooked up to a button that, when pressed, lights up one of 3 LEDs to indicate where player 1 won, player 2 won or if there was a tie.

Close up of Arduino plugged into breadboard for RPS game

Close up of Arduino plugged into breadboard for RPS game

Another angle of the Arduino setup for the RPS game. There are two potentiometers, a button and 3 LEDs (two blue and one yellow) connected as well.

Another angle of the Arduino setup for the RPS game. There are two potentiometers, a button and 3 LEDs (two blue and one yellow) connected as well.

I ran the code from Labs 2 and 3 (mentioned above) to ensure that all of the components ran as expected. I was able to get the lights to respond to each potentiometer, and then when I changed the code I was able to get them all respond to the button. I was confident that I had everything set up on the board the way that it needed to be.

While writing the code, I figured that I would start by using Serial.println to test the values that I was getting from the potentiometers. This is where I realized that my logic was flawed. When getting a raw read of the signal coming from the potentiometer, I saw that I was getting a range of values from 0-1023. So I figured that if I break that range down into groups of 341 (1023/3), then I can designate each group to a value of either rock, paper, or scissors.

A screenshot of the code for the RPS game showing that the values from the potentiometer were split into 3 groups, rock, paper and scissors.

A screenshot of the code for the RPS game showing that the values from the potentiometer were split into 3 groups, rock, paper and scissors.

The problem, I theorize, is that the signal from the potentiometer isn’t stable enough to give a read that will allow the game to work. When I have the potentiometer on the lowest setting (0-341), it’s fine and shows a constant reading for scissors. The problem is that when I go into the higher ranges designated for paper and rock (341-682 and 682-1023, respectively), the reading bounces from what it’s supposed to be and scissors.

Serial Print values from the RPS code, note how there is never a constant Paper or Rock reading.

I think that if I were to continue this project, I would need something to smooth the signal coming out from the potentiometer, or I would need to use buttons instead. Either way, I feel that this was a great exercise and a successful failure in that I was able to push the limits of my knowledge.