Stap 9: codering
Code met Arduino programmering software. De volgende code is voor referentie. Figuur 9.1 laat de opdracht van elke knop op de PS2 controller pad zien.
---------------------------------------------------------------------------------------------------------------
#include
voor v1.6, kunt u deze bibliotheek downloaden van http://pcsx2.net/download/releases.html
#include
PS2X ps2x; PS2 Controller klasse maken
int fout = 0; byte type = 0; byte trillen = 0; Servo s1, s2, s3; int p = 20;
VOID Setup {}
Serial.begin(57600);
S1.attach(6); S2.attach(5); S3.attach(3);
fout = ps2x.config_gamepad (13,11,10,12, true, true);
if(Error == 0) {}
Serial.println ("gevonden Controller, geconfigureerd succesvol");
} else {}
Serial.println("Error!");
}
}
void loop {}
if(Error == 1) keren;
ps2x.read_gamepad (false, trillen);
if(ps2x. Button(PSB_PAD_UP)) {}
S2.write(45); S3.write(45); delay(500);
S2.write(20); S3.write(20); Serial.println("run!");
}
if(ps2x. Button(PSB_PAD_RIGHT)) {}
S1.write(36); S2.write(39); S3.write(33); delay(500);
S1.write(20); S2.write(20); S3.write(20); Serial.Print ("rechts afslaan!");
}
if(ps2x. Button(PSB_PAD_LEFT)) {}
S1.write(36); S2.write(32); S3.write(39); delay(500);
S1.write(20); S2.write(20); S3.write(20); Serial.Print ("Turn Left!");
}
Als (ps2x. NewButtonState()) {}
if(ps2x. Button(PSB_L2))
{s1.write(20); s2.write(20); s3.write(20); Serial.println ("All gestopt!"); }
if(ps2x. Button(PSB_GREEN))
{s1.write(32); Serial.println("Floating!"); }
}
delay(50);
}
---------------------------------------------------------------------------------------------------------------