Week 9 – Analog and Digital

Concept

When thinking about this weeks assignment I wanted to make something that is interactive and also fun to use. I was thinking for a while and then I decided to make a game where we play against the Arduino. The game is simple, the Arduino thinks of a random number and we need to try to guess it. But out of all numbers how would we guess which one Arduino thought of. Well by turning the potentiometer left or right we are adjusting the number that is the representation of our guess which we lock in by clicking on the switch. The closer we are to the number Arduino thought of the brighter does the blue LED become and when we think we got the answer we press on the switch and we either get a green or a red light from the LEDs, depending on if our answer is correct or not.

Implementation

The design, as mentioned above uses a potentiometer to change change the value of the choice number the user will select. A pushbutton switch is used to lock in the answer and get feedback from the LEDs. A blue LED is used as an indicator on how close we are getting to the answer and the red and green ones are used to give feedback if the answer is correct or not. The design also uses one 10k ohm resistor for the button and three 330 ohm resistors for the LEDs.

Her is the schematic of the circuit.

Code I am proud of
int potValue = analogRead(potPin);           // 0–1023
int guess = map(potValue, 0, 1023, 0, 100);  // Convert to 0–100 range

int difference = abs(guess - targetNumber);     // Distance from correct
int brightness = map(difference, 100, 0, 0, 255); // Closer = brighter
brightness = constrain(brightness, 0, 255);
analogWrite(blueLEDPin, brightness);

This part of the code took some time to get right. I used the map function we recently used in class to make the LED brighter the closer we got to the correct number.

Challenges and future improvements

I again faced challenge with the cable management. The breadboard sometimes feels too small for all of the cables, sensors and resistors to fit and a lot of time when I would finish plugging a resistor in I would accidently unplug a cable with my hands. This is something I definitely want to work on for future projects. As for this design specifically, I would like to implement a reset button so the whole device doesn’t need to be reset every time a guess is made, also some audio queues would probably be a nice addition to the whole concept. Maybe also including a screen from which the user could see what numbers they are choosing would bring the game to a whole another level. Overall working with Arduinos is very fun and I enjoy it even more with every project we do.

Leave a Reply