const int pinX = A0; // ось X джойстика
const int pinY = A1; // ось Y джойстика
const int pinButton = 2;
int lastButton = LOW; //предыдущее состояние кнопки
int curButton = LOW; //Текущее состояние кнопки
const int leds[] = {11, 10, 9, 6, 5, 3};
const int indexLedMax = 5;
const int indexLedMin = 0;
int indexLedActive = 0;
const int ledBrightMax = 250;
const int ledBrightMin = 0;
int ledBright = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (int i = 0; i < indexLedMax + 1; ++i)
{
pinMode(leds[i], OUTPUT);
}
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
pinMode(pinButton, INPUT);
}
int debounce (int last)
{
int current = digitalRead(pinButton);
if (last != current) //если состояние изменилось
{
delay(5);
current = digitalRead(pinButton);
}
return current;
}
void changeLedActive(const int& x)
{
Serial.println(indexLedActive);
if (x < 127)
{
++indexLedActive;
if (indexLedActive > indexLedMax)
{
indexLedActive = indexLedMax;
}
}
else if (x > 127)
{
--indexLedActive;
if (indexLedActive < indexLedMin)
{
indexLedActive = indexLedMin;
}
}
}
void changeLedBright(const int& y)
{
if (y > 127)
{
ledBright += 10;
if (ledBright > ledBrightMax)
{
ledBright = ledBrightMax;
}
}
else if (y < 127)
{
ledBright -= 10;
if (ledBright < ledBrightMin)
{
ledBright = ledBrightMin;
}
}
}
void loop() {
// put your main code here, to run repeatedly:
int x = analogRead(pinX);
int y = analogRead(pinY);
x = map(x, 0, 1023, 0, 255);
y = map(y, 0, 1023, 0, 255);
changeLedActive(x);
changeLedBright(y);
curButton = debounce (lastButton);
if (lastButton == HIGH && curButton == LOW) //условие нажатия
{
indexLedActive = indexLedMin;
}
lastButton = curButton;
for (int i = 0; i < indexLedMax + 1; ++i)
{
if (indexLedActive == i)
{
analogWrite(leds[indexLedActive], ledBright);
}
else
{
analogWrite(leds[i], ledBrightMin);
}
}
delay(100);
}