const int PIN_RED = 2;
const int PIN_YELLOW = 3;
const int PIN_GREEN = 4;
const int PIN_HORIZ = A0;
const int PIN_VERT = A1;
const int PIN_SEL = 7;
void setup() {
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_YELLOW, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_SEL, INPUT_PULLUP); // joystick button
}
void loop() {
int x = analogRead(PIN_HORIZ); // 0-1023, center ~512
int y = analogRead(PIN_VERT); // 0-1023, center ~512
bool pressed = (digitalRead(PIN_SEL) == LOW);
// Push button -> all LEDs on
if (pressed) {
digitalWrite(PIN_RED, HIGH);
digitalWrite(PIN_YELLOW, HIGH);
digitalWrite(PIN_GREEN, HIGH);
return;
}
// Left/right -> Red or Green
digitalWrite(PIN_RED, x < 400); // joystick left
digitalWrite(PIN_GREEN, x > 600); // joystick right
// Up -> Yellow
digitalWrite(PIN_YELLOW, y < 400); // joystick up
}