Hello Everyone,
This Controller can be used as gaming controller for playing games. I was thinking to make something from MPU6050 and then I decided to make this project.
So Let's see how it works
In this project ardiono uno reads the button state of the throttle button, brake button and the data provide by the MPU6050 sensor then prints a character on the serial monitor and then a python program reads the character to simulate a keyboard input to a program(Game).
So How to make this thing ?
Step 1Initially you have to do some basic wiring to connect the arduino with the mpu6050 and two Buttons one for throttle another for brake.
Now you have to set the hardware on bicycle so that it feels more real(you may skip this or install where you want)
Install the MPU6050 sensor tightly. place its Z axis similar to my setup(you may change but then you have do some changes in the code)
Step 3
Now in the Software portion, I am using a library <MPU6050_tockn.h> and <wire.h>.
Now I am using some key code for every motion.
Event Action
If throttle button is pressed write 'w' on the Serial port
if brake button is pressed write 's' on the Serial port
if right position observed write 'd' on the Serial port
if left position is observed write 'a' on the Serial port
if throttle button is pressed and right position observed write 'e' on the Serial port
If throttle button is pressed and left position is observed write 'q' on the Serial port
if brake button is pressed and right position observed write 'c' on the Serial port
if brake button is pressed and left position observed write 'z' on the Serial port
so for doing this we have to use the following code
if(mpu6050.getAngleZ()>25 && digitalRead(A0))
Serial.println('q');
else if(mpu6050.getAngleZ()<-25 && digitalRead(A0))
Serial.println('e');
else if(mpu6050.getAngleZ()>25 && digitalRead(A1))
Serial.println('z');
else if(mpu6050.getAngleZ()<-25 && digitalRead(A1))
Serial.println('c');
else if(digitalRead(A0))
Serial.println('w');
else if(digitalRead(A1))
Serial.println('s');
else if(mpu6050.getAngleZ()>25)
Serial.println('a');
else if(mpu6050.getAngleZ()<-25)
Serial.println('d');
Note - if you are using another axis then you can use mpu6050.getAngleX() or mpu6050.getAngleY().
mpu6050.getAngleZ()>25
here I will consider the left move when the mpu6050.getAngleZ()
gives value greater than 25(threshold point), you can change and set you own threshold point, for finding your own threshold point you can use this example code of getAngle to check the value your sensor at every position then you can select Sthe value at your desired position.
Till now we are getting our value at serial port, now we will scan the serial port using python and then simulate the input, in this I will give two python code one of them generate input using pyautogui But sometime this will not work on Direct X games so I will also give the second python code(port2.py) that will hopefully work, So try both the python codes (port1.py and port2.py) one by one and see which works. you can also select an notepad after running the python program an if you see it prints w on notepad on pressing the throttle button or giving any other moves, then everything is fine from this hardware and arduino side.
I have given the complete code below. [Note- inside python program change port name to your arduino port, for me this is 'COM3']
Step 4- running it
1st- At first connect the arduino open serial monitor if everything is working fine you are seeing the correct output in serial monitor then close the serial monitor
2nd - open the game
3rd - run the python script go to the gaming window
4th - Enjoy, now you are done
Now see how this work on real-time I am using this in asphalt 8 - AirborneSo Hope you Like it.
Comments
Please log in or sign up to comment.