Go Down

Topic: Interfacing my USB gamepad using python script (Read 1 time) previous topic - next topic

syd_xp

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: [Select]

//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 :)

jiblets

#1
Mar 28, 2013, 09:09 pm Last Edit: Mar 28, 2013, 09:21 pm by jiblets Reason: 1
Quote

Code: [Select]
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: [Select]

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: [Select]


char buffer[7] ;
int bytesRead ;

bytesRead = Serial.readBytes( buffer, 7 ) ;



This will read multiple bytes at once.

jiblets

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.

Grumpy_Mike

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

syd_xp

@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 :(

@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 :D

Go Up