It’s all started with a small game from the dark ages called “Mario”, right from the time of being a tiny little guy jumping on ducks to save my princess till being a masculine handsome Prince roaming in Persia (Prince of Persia) fighting against darkness to save my world behind I have been a great fan of playing video games and I grew up playing them. But they do get bored up sometimes and I feel less involved into it. Today, the advanced gaming consoles enables virtual gaming and helps us to feel the game a lot more better than a keyboard or a mouse can do.
Being an Arduino Enthusiast I tired playing the famous game called “Angry Birds” using keyboard & mouse and decided to build my own Game Controller using a Flex Sensor and a Potentiometer. When the flex sensor is pulled the bird on the catapult will also be pulled and you can use the potentiometer to set the direction in which it should be launched. Then when you release the flex sensor the bird will be launched. I pretty much enjoyed doing it, so if you are up to building something very similar then this tutorial will be a helpful. This tutorial will also be helpful in Controlling Mouse Cursor using Potentiometer.
Software and Hardware Requirements:
- Arduino IDE
- Processing IDE
- Angry Birds Game on Computer
- Arduino (Any Version)
- Flex Sensor
- 47K ohm Resistor
- Connecting Wires
The Arduino Board reads the values from the Potentiometer and Flex Senor and Transmits them to the Laptop/PC through USB COM port via normal Serial.write() function. We then read this information using Processing and control the mouse cursor using the Robot class in Java which is supported by processing IDE. We have programmed the processing IDE in such a way that when the Flex sensor is pulled a mouse click will be made and based on how much it is pulled the mouse pointer will move in X direction. Then based on the value from potentiometer we will move the mouse cursor in Y direction, this way we can set the direction in which bird should be launched.
The circuit for playing the Angry Bird using Flex Sensor and Potentiometer is easy.
We have simple connected a potentiometer and a flex sensor to the Analog inputs (A0,A1) of the Arduino. The output of the Flex sensor is also pulled down using a 47K pull down resistor.
You can directly connect it on breadboard or solder them to a Perf board and assemble it on a gloves or something to make it more creative. I have simply used a breadboard to do my connections as shown below:
The complete Arduino code is given at the end of the tutorial. Few important lines are explained below.
We initialize the program to work with 9600 baud rate and start reading the values from Flex sensor and Potentiometer. As we know serial.write() function can send only one byte of data at a time. Since One byte is 8 bits and 2^8 = 256. We will be able to send values from 0 to 256. So we have to compress the values of Flex sensor output and Potentiometer Output into 0 to 256.
To do that we use the map() function in Arduino. All the values from the flex sensor are converted from 5 to 100, so when we bend the sensor it will increment from 5 and when released it will go back to 5. To mention the mouse clicks the value 1 and 0 is used. When 1 is sent the mouse is pressed and when 0 is sent the mouse is released.