int oldx, oldy;
bool isMovedX = false;
bool isMovedY = false;
int led_pin[] = {11, 10, 9, 6, 5, 3};
int active_pin = 0;
int active_bright = 250;
int lim = 25;
int lastButton = LOW;
int currButton = LOW;
void setup() {
pinMode(led_pin[active_pin], OUTPUT);
digitalWrite(led_pin[active_pin], HIGH);
for(int i = 1; i < 6; i++)
{
pinMode(led_pin[i], OUTPUT);
digitalWrite(led_pin[i], LOW);
}
pinMode(2, INPUT_PULLUP);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}
void loop() {
int x = analogRead(A1);
int y = analogRead(A2);
isMovedX = (((x < 512 - lim) || (x > 512 + lim)) && (x != oldx)) ? true : false;
isMovedY = (((y < 512 - lim) || (y > 512 + lim)) && (y != oldy)) ? true : false;
if(isMovedX)
{
LedShift(x);
}
if(isMovedY)
{
Brightness(y);
}
PressButton();
oldx = x;
oldy = y;
delay(100);
}
void PressButton()
{
static unsigned long timer;
if(timer + 50 > millis()) return;
currButton = digitalRead(2);
if(currButton == LOW && lastButton == HIGH)
{
digitalWrite(led_pin[active_pin], LOW);
active_pin = 0;
analogWrite(led_pin[active_pin], active_bright);
}
lastButton = currButton;
timer = millis();
}
void Brightness (int y)
{
if((y > 512) && (active_bright < 241))
{
active_bright += 10;
analogWrite(led_pin[active_pin], active_bright);
}
if((y < 512) && (active_bright > 9))
{
active_bright -= 10;
analogWrite(led_pin[active_pin], active_bright);
}
}
void LedShift(int x)
{
if((x > 512) && (active_pin > 0))
{
digitalWrite(led_pin[active_pin], LOW);
analogWrite(led_pin[active_pin - 1], active_bright);
active_pin--;
}
if((x < 512) && (active_pin < 5))
{
digitalWrite(led_pin[active_pin], LOW);
analogWrite(led_pin[active_pin + 1], active_bright);
active_pin++;
}
}