Introduction: Python Powered Arduino Games
Well, first of all, thanks for viewing my instructable.
Have you been interested in making games with Arduino?
any types your imagination can develop?
So this Instructable is for you
Step 1: Sharpen Your Tools
For starting, we would need the following programs
- Arduino Ide (Doesn't matte r the version)
- Python (2.7)
- Some code editor (I used Sublime Text)
- Serial library for python
And some other materials
- An Arduino or clone (the one you want)
- A cable for connecting the Arduino to your computer
- A led or other electronics components( because it depends on how do you want to make your game)
Step 2: Starting the Magic... :)
First of all, open your code editor and copy paste the code that is on the bottom, I will explain it to you and also I would explain how you could edit it:
(I didn't put the code here, but I put the file because the format was going to break and you were going to get an error)
Download the file and put it on your preference of text editor (I used Sublime Text)
Attachments
Step 3: Explaining the Code
Well, the code starts with
import serial<br>from Tkinter import *
This part is when you start to import the library
-Import Serial is where you are importing the library to communicate with Arduino
-Tkinter is the library that detects KeyPresess
try: arduino = serial.Serial( 'Put your Arduino port here, i will show you later', 9600 ) except: print "Cannot conect to the port"
here is where the python code tries to connect to the port where the Arduino is connected and if it doesn't find the port of the Arduino it would print "Cannot connect to the port"
root = Tk()
This is where the Tkinter makes a window for it to record the keystrokes
<p>Pressed4 = False</p><p>def P1(event): print "Pressed 1" Pressed4 = True print Pressed4</p>
the first line is creating a boolean named Pressed4 and it assigns the false statement to it, the line that has def P1(event): is a line of the Tkinter library that creates an event to run it later, as you can see the event is to print "Pressed 1" and the is to convert Pressed4 into true and it print Pressed4 again
root.bind_all("", P1)<br>root.mainloop()
the first line is where it detects if the key release of key 1 make function P1 (the function that we make above)
the last line is just the main loop of Tkinter.
Step 4: The Arduino Code!!!
Now that you have coded your python script is time to put the code on the Arduino:
void setup()
void loop() { if(Serial.available()) { char in = Serial.read(); if(in == '1'){ Serial.print(Number 1 pressed); //If the python script sends a 1 we print a number 1
{ Serial.begin(9600);//we start the serial }
} if(in == '2'){ Serial.print(Number 2 pressed);
} if(in == '3'){ Serial.print(Number 3 pressed);
} if(in == '4'){ Serial.print(Number 4 pressed); }}}
Step 5: Modify the Code for Yorself
Now that I explain the coded to you, you are ready for making that game that you have wished all your life.
IDEAS
- You could add some other functions like "P1" and make that if one key is pressed print something or send a serial data to your Arduino to it make more things like turn ON a led
- add more keypresses
- Add more libraries to it to be more interactive
- Program the Arduino, if Serial.read = 1 or whatever you want, make Arduino make something, you could make the python script send serial values to the Arduino by adding, arduino.write('1')
Step 6: Conclusion
Well, that was all and I wish you have like my tutorial and sorry for my English and if relay like this too much I wish you could vote for me in the contest.
Thanks!!! :)
If you have some type of troublrshiting, questions or request feel free to write a comment.