Friday, June 12, 2015

Mobile Game Controller Using Python

This post is to show how to communicate between mobile and computer, and its one such project that combines a lot of stuffs from Wifi commmunication to keyboard user input. This is can be achieved through python and kivy and along with some other packages. When I surfed the net I saw people doing some gesture controlled powerpoint presentation which involves an accelerometer.I thought of doing something with the accelerometer not just for controlling presentations but for controlling games. Since I did not have an accelerometer I used my phone's accelerometer to control the game.I designed an app using kivy which collects the phone's accelerometer data and sends it to the computer, the computer processes the information received and generates key input events
If you would like to know how to use your mobile's accelerometer and transfer its data to your computer you can read this.

Lets move on. !!

First of all this project requires:

  • Python 2.7 in your computer.
  • QPython on your mobile or you can package the app and intsall it in your mobile. I just directly coded in my mobile itself.
  • Win32api for your computer 
  • For simplicity download PyUserInput
  • And finally a mobile having an accelerometer
Link for Downloading Python 2.7.

Link for Downloading Win32api. Download the one that suits your python version if it displays an error message during installation then you have downloaded the wrong version.
I used Kivy for creating the app. My friend Raamakrishnan created a windows phone app for the same. Refer the link below to for the code..

The app looks like this:



The code has been posted in the github 
#IIEE

Tuesday, June 2, 2015

Interfacing Arduino using Python

We know that Arduino IDE has inbuilt serial monitor. Using this serial monitor we can communicate with arduino and make arduino behave as we wanted. This serial monitor can be even built using python. This post is to show how to install PySerial and use it with arduino.

First of all download Python and install it (Python 2.7 recommended).

Python 2.7 Download

 Just by installing Python we cannot start programming. A script editor is also needed. There is a list of script editors available here. I use Geany.

List of Script Editors Python

The next step is to download Pyserial and  install it.

Pyserial Download

Download the tar.gz file and extract it. Then open the command prompt try typing python if you get a error message then follow these steps.

  • Right click Computer-Properties
  • Click change settings on the right hand side
  • Go to Advanced tab. Click Environment variables.
  • Double click Path and after a semicolon specify the path where python is installed
                       
  • Now restart the computer for the settings to get applied.
  • After restarting, open the command prompt and try typing python
  • Python terminal should open.
  • Now go to the extracted folder in the command prompt and type 
                                      python setup.py install

Installation will take place...

Now lets write a code to demonstrate the working of Pyserial.
This program switches ON and OFF the inbuilt led on the Arduino board.

Python Code:
 import serial  
 ser=serial.Serial("COM31",9600)   #start a serial connection  
 a='0'  
 while a=='1' or a=='0':  
      a=raw_input()               #get the input  
      if a=='1' or a=='0':  
           ser.write(a)             #write to serial  
           print ser.readline()       #read a line from the serial  
      else:  
           break  


Arduino code:

  #include<Serial.h>  
    void setup()  
    {  
     Serial.begin(9600);  
     pinMode(13,OUTPUT);  
    }  
    char a;  
    void loop()  
    {  
     if(Serial.available())  
     {  
      a=Serial.read();  
      if(a=='1')  
      {  
       digitalWrite(13,HIGH);  
       Serial.write("ON\n");  
      }  
      else if(a=='0')  
      {  
       digitalWrite(13,LOW);  
       Serial.write("OFF\n");  
      }  
     }  
    }  
  

The result will look like this:



#IIE