Τετάρτη 3 Φεβρουαρίου 2010

2ος τρόπος ελέγχου 2d κίνησης με arduino

Εγκαθιστώντας το Python (http://www.python.org/)και τα modules SendKeys kai Serial, στέλνει σήμα το Arduino και ελέγχει τα πλήκτρα του keyboard. Πρέπει το Python να λειτουργεί στο background.

Το δοκιμάσαμε σε ένα παιχνίδι αλλά όπως και με τον προηγούμενο τρόπο λειτουργεί μόνο στο menu και όταν ξεκινά το παιχνίδι παύει να λειτουργεί. Δεν έχουμε βρεί ακόμη την αιτία.

Python code:
import serial
import SendKeys
import os
ser = serial.Serial('COM5', 9600)
#os.system('start "C:\Program Files\StepMania\StepMania.exe"')
LButton = False;
RButton = False;
UButton = False;
DButton = False;
BButton = False;
SButton = False;
while 1:
x=ser.read()
if x == 'l':
LButton = not LButton
if LButton:SendKeys.key_down(39)
else: SendKeys.key_up(39)
elif x == 'r':
RButton = not RButton
if RButton:SendKeys.key_down(37)
else: SendKeys.key_up(37)
elif x == 'u':
UButton = not UButton
if UButton:SendKeys.key_down(40)
else: SendKeys.key_up(40)
elif x == 'd':
DButton = not DButton
if DButton:SendKeys.key_down(38)
else: SendKeys.key_up(38)
elif x == 's':
SButton = not SButton
if SButton:SendKeys.key_down(13)
else: SendKeys.key_up(13)
elif x == 'b':
BButton = not BButton
if BButton:SendKeys.key_down(8)
else: SendKeys.key_up(8)
else:
pass


ser.close()


Arduino code:
const int leftpin = 2;
const int rightpin = 3;
const int uppin = 5;
const int downpin = 4;
const int backpin = 6;
const int startpin = 7;

byte leftButton = LOW;
byte rightButton = LOW;
byte upButton = LOW;
byte downButton = LOW;
byte backButton = LOW;
byte startButton = LOW;

byte buttonRead = LOW;
void setup()
{
pinMode(leftpin, INPUT);
pinMode(rightpin, INPUT);
pinMode(uppin, INPUT);
pinMode(downpin, INPUT);
pinMode(backpin, INPUT);
pinMode(startpin, INPUT);
Serial.begin(9600);
}

void loop()
{
buttonRead = digitalRead(leftpin);
if(buttonRead != leftButton)
{
leftButton = buttonRead;
Serial.print('l');
}
buttonRead = digitalRead(rightpin);
if(buttonRead != rightButton)
{
rightButton = buttonRead;
Serial.print('r');
}
buttonRead = digitalRead(uppin);
if(buttonRead != upButton)
{
upButton = buttonRead;
Serial.print('u');
}
buttonRead = digitalRead(downpin);
if(buttonRead != downButton)
{
downButton = buttonRead;
Serial.print('d');
}
buttonRead = digitalRead(startpin);
if(buttonRead != startButton)
{
startButton = buttonRead;
Serial.print('s');
}
buttonRead = digitalRead(backpin);
if(buttonRead != backButton)
{
backButton = buttonRead;
Serial.print('b');
}
delay(10); // allow some time for the Serial data to be sent
}

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου