Pages: [1]   Go Down
Author Topic: Interfacing my USB gamepad using python script  (Read 945 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys I am new to arduino but i have a project and I must complete it ASAP. For my project i want to interface my arduino using USB gamepad. I have interfaced but i have a problem in the data that my gamepad sends to arduino. What kind of data does it send to arduino??? If I could know that then it would be very helpful for me. I am attaching my code to indicate where my problem is
Code:
//A simple program to use one button of gamepad to turn on the LED and another button to turn it off
int ledPin;
int Byte;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0)
  {
    if(Byte = Serial.readBytes()== //What should i write here) // <-- this line is my problem. What type of data does arduino read?
   {
     digitalWrite(ledPin, HIGH);
   }
   else if(Byte = Serial.readBytes() == //What should i write here) // <-- this line is my problem. What type of data does arduino read?
   {
     digitalWrite(ledPin, LOW);
   }
  }
}

And my second problem is how do i fixed the position of a servo. Like if a press a particular button in my gamepad for 5 sec it will move suppose 30 degrees and remain there fixed again if i press that button again for 5 sec it will move 30 degrees again.
These are my problems Thank You smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
 
Code:
if(Byte = Serial.readBytes()== //What should i write here)

That line doesn't really make sense. The correct way of doing that would look more like this:

Code:
byte comparedValue = 25 ;
byte otherValue = 30 ;

if(Serial.available() > 0)
  {
    Byte = Serial.read() ;

    if( Byte == comparedValue )
      digitalWrite( ledPin, HIGH ) ;
    else if( Byte == otherValue )
      digitalWrite( ledPin, LOW ) ;
  }
If you want to use Serial.readBytes then you should do something like this:

Code:

char buffer[7] ;
int bytesRead ;

bytesRead = Serial.readBytes( buffer, 7 ) ;


This will read multiple bytes at once.
« Last Edit: March 28, 2013, 03:21:03 pm by jiblets » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, make sure that you actually specify a value for the variable 'ledPin'. The 'pinMode' statement in your setup function is useless unless you specify which pin you want the led to be on.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33987
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How have you got your USB game pad wired up?
USB does not send out serial data, it is a complex protocol.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Jiblets your code is possible for only one LED light. What if i have multiples LEDs ? That's why i wanted to know is there any kind of data type that i should write so that i can control on specific button to control one LED. I have seen
http://nrqm.ca/mechatronics-lab-guide/lab-guide-using-the-gamepad/ here that USB's each button has a byte number and i tired to use that number in Serial.read = "x(byte number)" but it wasn't working smiley-sad

@Grumpy Mike Yes my USB is wired to my PC and the gamepad can communicate with arduino through PC using python script. You are also right about the protocol. They are indeed very complex but thanks to python it made it easier smiley-grin
Logged

Pages: [1]   Go Up
Jump to: