int xPin = A0;
int yPin = A1;
int buttonPin = 2;
int xVal;
int yVal;
int buttonState;
int upLed = 10;
int downLed = 9;
int leftLed = 11;
int rightLed = 5;
int upBrightness = 0;
int downBrightness = 0;
int leftBrightness = 0;
int rightBrightness = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode (xPin, INPUT);
pinMode (yPin, INPUT);
pinMode (buttonPin, INPUT_PULLUP);
pinMode (upLed, OUTPUT);
pinMode (downLed, OUTPUT);
pinMode (leftLed, OUTPUT);
pinMode (rightLed, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
xVal = analogRead (xPin);
Serial.print ("xVal = ");
Serial.println (xVal);
yVal = analogRead (yPin);
Serial.print ("yVal = ");
Serial.println (yVal);
buttonState = digitalRead (buttonPin);
upBrightness = map(yVal, 513, 1023, 0, 255);
downBrightness = map(yVal, 511, 0, 0, 255);
leftBrightness = map(xVal, 513, 1023, 0,255);
rightBrightness = map(xVal, 511, 0, 0, 255);
if (yVal >= 513){
analogWrite(upLed, upBrightness);
}
if (yVal <= 511){
analogWrite(downLed, downBrightness);
}
if (xVal >= 513){
analogWrite(leftLed, leftBrightness);
}
if (xVal <= 511){
analogWrite(rightLed, rightBrightness);
}
if (buttonState == LOW){
digitalWrite (upLed, HIGH);
digitalWrite (downLed, HIGH);
digitalWrite (leftLed, HIGH);
digitalWrite (rightLed, HIGH);
}
if (buttonState == HIGH){
digitalWrite (upLed, LOW);
digitalWrite (downLed, LOW);
digitalWrite (leftLed, LOW);
digitalWrite (rightLed, LOW);
}
}