IR Servo Pointer Quiz Game

by 968988 in Circuits > Arduino

164 Views, 1 Favorites, 0 Comments

IR Servo Pointer Quiz Game

WhatsApp Image 2022-06-19 at 9.42.55 PM.jpeg

These days, playing quizzes has become boring. That's why I came up with an idea to make a tool called IR Servo Quiz Pointer game that not only will make the game more interesting but will make it more exciting as well. In addition, the main benefit of this tool is that you can use it anytime with your friends or family. Moreover, this tool is easy to use, all you have to do is just come up with some questions that you feel are hard or easy based on the game level and then ask your opponents. If the answer turns correct, the servo arm will point towards the green led, and it will start blinking, and the opponent will be rewarded with a point. But if the answer turns wrong, the servo arm will point towards the red led, and it will start blinking with a buzzer, and the opponent will lose one point. Interesting isn't it? So let's see how this circuit is built.

Components Needed

Screenshot 2022-06-18 at 1.35.20 PM.png

Wiring Diagram

Screenshot 2022-06-19 at 5.51.02 PM.png
Hello World.jpeg
Hello there.jpeg

Now talking about the connections -

Starting with the connections, first, connect the 5V pin of the Arduino with the positive lane of the breadboard and the ground pin with the negative lane of the breadboard. After you are done then connect these components: -

  • Connecting 7 Segment Display and 2 pushbuttons with the Arduino -

The pins that I have used to connect the 7 segment display with the Arduino are: -

  • a - Pin 11
  • b - Pin 10
  • c - Pin 7
  • d - Pin 8
  • e - Pin 9
  • f - Pin 12
  • g - Pin 13

After you are done connecting the pins with the Arduino then connect the 330 Ohms resistors on both sides as you can see in the image above with the negative lane on the breadboard.

Moving on to the two pushbuttons, now before connecting the pushbuttons with the Arduino first, connect the terminal one of both the pushbuttons with the positive lane on the breadboard. Then connect the second terminal with the 330 ohms resistors which are connected with the negative lane on the breadboard. After you are done then simply connect the fourth terminal with these pins: -

  • PointUp - Pin A0
  • PointDown - Pin A1


  • Connecting IR sensor and Potentiometer with the Arduino -

For the connections of the IR sensor, I have first connected the ground with the negative lane and power with the positive lane of the breadboard and, then simply connected the out pin with pin 5 on the Arduino.

Note - You can use any IR remote to operate the IR sensor.

Moving on to the connections of the potentiometer, first connect terminal one with the positive lane and terminal two with the negative lane of the breadboard. And then, simply connect the wiper pin with the A5 pin on the Arduino.

  • Connecting two LEDs with the Arduino -

The pins that I used to connect the Green Led are: -

  • Anode of the Green Led - Pin 4
  • Cathode of the Green Led - Terminal 1 of the Resistor which is connected to the negative lane of the breadboard.

The pins that I used to connect the Red Led are: -

  • Anode of the Red Led - Pin 3
  • Cathode of the Red Led - Terminal 1 of the Resistor which is connected to the negative lane of the breadboard.


  • Connecting Servo Motor and Buzzer with the Arduino -

To connect the servo motor with the Arduino, first, connect the power with the positive lane and the ground with the negative lane of the breadboard. Then, simply connect the signal pin with pin 6 of the Arduino.

Last but not the least, connect the positive pin of the buzzer with pin 2 of the Arduino and then connect the negative pin with the negative lane of the breadboard.

Code Part 1 - Declaring Variables

Screenshot 2022-06-19 at 9.51.35 PM.png

Now that the wiring is done we can now start writing the code. Now talking about writing the code, one of the main steps comes in is declaring and initializing the variables, so that the Arduino would know which component needs to be operated. Here are some variables that I have declared for all the components I am using -

First, declare the libraries for the IR sensor and Servo Motor. You can easily install the IR remote library from here. And, the Servo motor library you can easily find in the Arduino IDE software by first clicking on the open tool, then going to the libraries, there you can find the servo library.

  • a,b,c,d,e,f,g variables - are declared for the pins of the 7 segment display.
  • receiverpin variable is declared for the IR sensor.
  • GreenLed is declared for the Green Led.
  • RedLed is declared for the Red Led.
  • wiper pin is declared for the potentiometer.
  • buzzer has been declared with the same name for the buzzer.
  • PointUp variable is declared for the first pushbutton.
  • PointDown variable is declared for the second pushbutton.

Other than these variables, I have defined some user-defined variables as well. These are -

  • delayvalue - which will be used for the potentiometer to adjust the blinking rate later in this program.
  • sGameMessage - which will be used to show the initial message on the serial monitor.
  • counter - which will be used as an argument to show the digits on the 7 segment display.
  • pointgain - which will be used to store the digitialRead values of the PointUp button.
  • pointloose - which will be used to store the digitalRead values of the PointDown button.

lastpointgain and lastpointloose variables are the random variables that will be used later in this program as if conditional statements to change the values on 7 segment display.

Now that we are done declaring and initializing all the variables, let's get onto the other part of the code.

Code Part 2 - Void Functions for the Digits

Screenshot 2022-06-17 at 7.15.14 PM.png

Now getting on to displaying the digits on the 7 segment display, you will need to define some void functions for each of the digits from 0 to 9 the way I did as you can see in the image above. Here, you can use this link to learn about the 7 segment display and can check which pin needs to be HIGH and LOW to display the particular digit on the 7 segment display.

For example - In my code to display digit 0, I created a void zero function and put all the pins except g at low to display the digit.

void zero() // Display digit zero

{

 digitalWrite(a,LOW);

 digitalWrite(b,LOW);

 digitalWrite(c,LOW);

 digitalWrite(d,LOW);

 digitalWrite(e,LOW);

 digitalWrite(f,LOW);

 digitalWrite(g,HIGH);

}

You can perform this process for the other digits as well by creating more void functions just the way I did for the void zero(). After you are done with making all the void() for all other digits let's move on to the next step of the code.

Code Part 3 - Void Setup()

Screenshot 2022-06-17 at 7.36.26 PM.png

Now talking about the void setup(), this is one of the most important parts of the code because in this function you will set all the components that you are using either as INPUT or OUTPUT. For example, I have created a for loop to set all the pins of the 7 segment display as OUTPUT. In the same way, you can do that for the other components as well such as LED. Moreover, as you can see in the code I have not defined every component as OUTPUT because some of the components such as pushbuttons don't receive OUTPUT. I have set them as INPUT as they will send the signal to the LEDs and make them turn on and off. I will show you how to perform that later in this program. Now let's move on to another part of the code.

Code Part 4 - Void Changescore()

Screenshot 2022-06-18 at 1.43.58 PM.png

Now talking about the Void Changescore(), this is also one of the most important parts of the code as this function will help us display the score on the 7 segment display. This is a user-defined function and it takes an argument as x which basically means that based on what input we will send from the pushbuttons to this function it will change the score. Furthermore, I have also used switch statements as you can see in the image above, which will help us to set the state of pins as per the respective number. I have made 9 switch cases where I have called all the void functions of the digits we made earlier which basically means that based on the input, the particular void function will get invoked. Now that we are done let's move on to the next and most important part of the code.

Code Part 5 - Void Loop()

Screenshot 2022-06-18 at 2.37.35 PM.png

Now if you were following me till now you might have noticed where is the void loop()? So here it is. Now moving on to the Void Loop(), this is where our main code will take place which will run in a continuous loop. Just like other functions, this is also one of the most important parts of the entire code.

Now talking about the statements I have added inside the Void Loop() let's start with the while loop(). Firstly, this while loop() is completely optional, I just have added this so that whenever any user starts playing this game for the first time, they will know how to use this tool. This statement will only print once on the serial monitor at the very start of the game. And to accomplish this, basically what I have done is I used a sGameMessage variable inside the while loop and while it is equal to zero the Serial.println will print the statements on the serial monitor. And to print these statements only once, I used sGameMessage++ so that it will increment and the condition inside the while loop becomes wrong and our program exits from the while loop.

Now moving on to the pushbuttons - PointUp and PointDown: -

Firstly, write the condition for the PointUp button which basically will do is whenever the button gets pressed and if it turns HIGH, it will start incrementing the digits. And, to achieve this I have created another IF condition inside the IF condition and used the counter variable inside it which we defined at the very start of the program, and made it equal to 9. This means that the max we can increment the digits is 9. And to display it on the 7 segment display I have called the Changescore(x) and replaced the x argument with the counter so it can display the digits.

In the same way, I did for the PointDown button as well. I also wrote similar conditions for the PointDown button as well which basically will do is whenever the button gets pressed and if it turns HIGH, it will start decrementing the digits. And, to achieve this just like the way for the PointUp button, I again created IF condition statements and inside it this time I have made the counter variable equal to 0. Which basically, means the max it can decrement is 0. And then again, we will call the Changescore() with the counter as an argument inside it so it can display the digits.

Now talking about the IR remote, I have used 1,2,3, and 4 buttons on the IR remote and it is totally optional. You can use any buttons on the IR remote but first, you have to decode some HEX values for the button you would like to use. For example:-

For button 1, the HEX values I got after decoding were 0XFD08F7. Then the same I did for the other three buttons as well. If you don't know how how to decode the signals from the IR remote, just simply click here.

Note - It will differ depending on the IR remote you are using.

After that, you are done with decoding the signals, let's move to the next part of the code where we will use this IR remote.

Now in this part of the Code, you will again make some IF-ELSE-IF statements. For example, for the button 1, I made: -

if (results.value == 0XFD08F7) // If the results.value equals to 0XFD08F7 then

  {

   myservo.write(90); // move the servo arm to 90 degrees

   digitalWrite(GreenLed,HIGH); // turn GreenLed on

   delay(delayvalue); // Adjust the delayvalue using potentiometer to adjust the blinking affect

   digitalWrite(GreenLed,LOW); // again turn GreenLed on 

   delay(delayvalue); // Added the delay value to maintain a delay after every blink

  }

Note - If you see the servo arm condition, I have rotated the arm to 90 degrees. It will differ for different servo motors. You can adjust it based on what you think will be the best for your LEDs.

In the same way, make some more IF-ELSE-IF statements for the other buttons as well. I have added the file of my code as well so that you can take a look at it if you have any problems. Finally, the circuit built is completed.