Week 9: Analog & Digital

For this week’s assignment, I initially thought of doing a stoplight or a guessing game, but when I searched around on this website, I saw that many people have already done those concepts. I therefore decided to put a little spin on things by making a stoplight-esque guessing game. Since the potentiometer reminded me of a shower temperature dial, I made a temperature guessing game called “Swampy’s Shower”, where you help a little bathing crocodile get his desired water temperature.

I started out by assembling the basics: a blue, yellow, and red LED connected to three different pins, a digital switch to start and restart the game, and an analog potentiometer to serve as our shower dial. This was fairly straightforward, but it took so many wires. For the “interface” of the game, I used a piece of cardboard that I would slot into the breadboard to label the controls / signals. The one in the picture below is an initial version that I revised in order to also have instructions for the switch and knob.

No description available.

I had the greatest difficulty with making the digital switch because I plugged some things wrong, so I kept getting random values. Thankfully, Google and Youtube saved the day, and I got it working.

From there, writing the code was also fairly straightforward because I built my program off the Analog Switch program from class. I used a simple if else statement to compare the dial entry vs a randomly generated temperature. The switch, when pressed, starts the game. The game ends when the yellow pin flashes (when the temperature is just right!) and you can restart it again with the switch.

  if (sensorValue == randNumber) {
    Serial.println("JUST RIGHT");
    digitalWrite(yellowPin, HIGH); //just right
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);
    delay(2000);
    start = false;
  } else if (sensorValue > randNumber) {
    digitalWrite(yellowPin, LOW);
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, HIGH); //too hot
  } else if (sensorValue < randNumber) {
    digitalWrite(yellowPin, LOW);
    digitalWrite(bluePin, HIGH); //too cold
    digitalWrite(redPin, LOW);
  }

} else { //turn everything off
    digitalWrite(yellowPin, LOW);
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);
}

I tried having the LEDs flash (e.g. if the temperature is closer, the light would flash faster) but it was more distracting and annoying (and kind of made the game too easy too), so I decided to keep things simple and have the blue light be on if it was too cold and the red light be on if it was too hot. The only things I might improve would be more shower functionality (a way to turn water on? drain or fill the tub?) or maybe making the brightness of the bulb an indicator of the closeness of the answer.

No description available.

I’m very happy with my final product. Watch it here:

 

Leave a Reply