Skip to content

Instantly share code, notes, and snippets.

@cfaulkingham
Created October 11, 2011 01:25
Show Gist options
  • Save cfaulkingham/1277044 to your computer and use it in GitHub Desktop.
Save cfaulkingham/1277044 to your computer and use it in GitHub Desktop.
Code for adding joystick support to the Gameduino Frogger game.
class Controller {
public:
void begin() {
byte i;
for (i = 3; i < 7; i++) {
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}
prev = 0;
}
byte read() {
int x = analogRead(0);
int y = analogRead(1);
byte r = 0;
if (!digitalRead(5) || y==0)
r |= CONTROL_DOWN;
if (!digitalRead(4) || y==1023)
r |= CONTROL_UP;
if (!digitalRead(6) || x==0)
r |= CONTROL_LEFT;
if (!digitalRead(3) || x==1023)
r |= CONTROL_RIGHT;
byte edge = r & ~prev;
prev = r;
return edge;
}
private:
byte prev;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment