Guess Who? : Analog and Digital LEDs

For this week’s assignment, I struggled a lot to come up with an idea, because I’ve never really had to think creatively in this medium (this case, arduino and LEDs), so it took a lot of brainstorming to conclude on an idea. I finally settled on a guessing game. It is a game you play wherein the code chooses a random bulb on the board, and you, as a player, have to guess which bulb it is. If you guess right, you win!

I used a potentiometer to get the analog input and a push-button for the digital input. I settled on five blue LEDs in total and the player has to guess which LED is the secret LED. There are green and red LEDs on the board that will indicate whether the chosen LED is correct. The choice is decided by rotating the knob on the potentiometer. And the choice is finally sealed in by pressing the push button.

Also, I wanted to put in some breather time in between two games and also when the game begins. I really liked the idea of showing patterns through LEDs, so I decided to make a pattern through the blue LEDs at the beginning of the game to settle in, and between games when whether the player is right or wrong is indicated.

Making the circuit was easier than I had anticipated. I imagined the absolute worst scenarios when I decided on the idea, but it helped me seal the concepts learned in class, so it went along better than expected 🙂

One problem I faced was the placement of the different elements on the board because at first I put in all the elements on one end of the board and it became very crowded, so I changed the placement and put in the potentiometer in the middle. While it can be troublesome to move in between the wires, it is intuitive in relation to the 5 LEDs surrounding it because the mapping is quite obvious. 

Here’s the circuit:

For the code itself, I divided the game into 4 different parts and worked on them one at a time, tested it, and then integrated it with whatever I had done until before and tested again.

The parts where:

    1. the patterns by the 5 blue LEDs
    2. potentiometer mapping to the 5 LEDs
    3. sealing the deal with the switch and checking whether the guess was right (and lighting the corresponding LED)
    4. Resetting the game to restart

Serial.print helped a lot with error correction. I had some trouble with the pushbutton and the resetting of the game. So, in addition to setting the numbers for mapping the knob value to the corresponding LED, the logs helped with the switch error too. The error I had was related to delays in the pattern function. While the pattern was still going on, the checking for the next guess would proceed, and this would cause the green/red bulb to light again. But this was a premature checking step as the player hadn’t chosen their next guess yet. So, I changed the code to make sure that all the variables were reset as soon as the push button was pressed and the pattern was called at the end of the game rather than executing the loop I had for the pattern at the beginning of the game (since it was too late by then). This solved all problems. 

Something I hadn’t realized before was that with analog input, I could also use the same for digital input too. While we did learn this in class, it hadn’t properly set in my mind until I did that with the code. I used digitalWrite for the 5 LED bulbs with the patterns in the beginning and analogWrite for the same bulbs when guessing the number. 

Here’s a demo:

Here’s the code: 

const int lightVis = 75;
const int num = 7;
int lightPins[7] = {2,3,4,5,6,10,11};
const int switchPin = 8;
const int knob = A0;
bool startPat = false; 
int pins[5]={1,3,4,5,6};
int randNumber = pins[random(5)]; 
bool gameOver = false;
bool prevButtonState = false;



void setup() {
  Serial.begin(9600); 
  for(int i=0;i<num;i++)
  {
    pinMode(lightPins[i],OUTPUT);
  }
  pinMode(switchPin,INPUT);
}


void loop() {
  bool currentState = digitalRead(switchPin);

  //pattern
  if( startPat == false){
  for(int i=0; i<=5; i++) {
     pattern();
     startPat = true;
  }
  }

  //green and red lights are now OFF
  digitalWrite(lightPins[2], LOW);
  digitalWrite(lightPins[0], LOW);

 //mapping knob values to corresponding LED 
  int knobValue = analogRead(knob);
  int ledNum;
  if ((knobValue>0 && knobValue<435) || knobValue<0)
    ledNum=6;
  if (knobValue>435 && knobValue<700)
    ledNum=5;
  if (knobValue>700 && knobValue<755)
    ledNum=4;
  if (knobValue>755 && knobValue<838)
    ledNum=3;
  if (knobValue>838)
    ledNum=1;
  
  
  Serial.print("Random Value: ");
  Serial.print(randNumber);
  Serial.print(" Knob Value: ");
  Serial.print(knobValue);
  Serial.print(" Mapped Value:");
  Serial.print(mappedValue);
  Serial.print(" ");
  Serial.print(currentState);
  Serial.print(" ");
  Serial.println(prevButtonState);

  analogWrite(lightPins[ledNum],254);
  for(int i=0; i<num; i++) {
     if(i!=ledNum){
        analogWrite(lightPins[i],0);
     }
  }
  
  
 
  if (currentState != prevButtonState && currentState==HIGH) {
    gameOver=true;
    if(ledNum==randNumber){
      digitalWrite(lightPins[0], HIGH);
      digitalWrite(lightPins[2], LOW);
    }
    else{
      digitalWrite(lightPins[2], HIGH);
      digitalWrite(lightPins[0], LOW);

    }
    prevButtonState = false;
    currentState=false;
  }
  
 

  if(gameOver)
  { 
    
    randNumber = pins[random(5)]; 
    gameOver = false;
    for(int i=0; i<=5; i++) {
     pattern();
    
  }
     
  }
  
}


void pattern(){ 
    for(int i=0; i<num; i++) {
      if(lightPins[i]!=2 && lightPins[i]!=4)
      {
        digitalWrite(lightPins[i],HIGH);
        delay(lightVis);
        digitalWrite(lightPins[i],LOW);
      }
 
    }
    
    for(int i=num-1; i>=0; i--) {
      if(lightPins[i]!=2 && lightPins[i]!=4)
      {
        digitalWrite(lightPins[i],HIGH);
        delay(lightVis);
        digitalWrite(lightPins[i],LOW);
      }
    }
}   

This will be a fun replacement for heads or tails. I’ve used heads/tails toss to make a lot of arbitrary decisions before, but I’m thinking that in situations wherein I’m leaning against a particular choice, this would be a great way to make a decision as there’s only a 1/5th chance of getting it right! But if you do guess right, even probability isn’t on your side ://

References:

https://create.arduino.cc/projecthub/thingerbits/9-led-patterns-with-arduino-277bf1

 

Leave a Reply