Hi, I have an Arduino UNO and I'm trying to create a LED wave that will change its pattern when a push button is pressed (at pin 12), when it's pressed it will increase the mode variable and use the pattern for that value, but when I try to reset it to 1 (after reaching more than 3) it just skips 1 and goth to 2
I'm using a push button on DPIN 12
And I'm using a variable resistor on APIN 0 to get the delay time
And this is my code (for a led wave), but whenever mode reaches 3 and try to reset it to 1 it just skips to 2
const byte firstPin = 2;
const byte lastPin = 9;
byte mode = 1;
int delayValue;
byte currentPin = firstPin;
bool dir = true;
void setup()
{
Serial.begin(300);
pinMode(12, INPUT);
for (byte i = firstPin; i <= lastPin; i++)
{
pinMode(i, OUTPUT);
}
}
void loop()
{
if (digitalRead(12) == HIGH)
{
mode++;
if (mode > 3)
{
mode = 1;
}
Serial.println(mode);
currentPin = firstPin;
dir = true;
for (byte i = firstPin; i <= lastPin; i++)
{
digitalWrite(i, LOW);
}
}
delayValue = analogRead(0) + 100;
if (mode == 1)
{
if (dir)
{
digitalWrite(currentPin, HIGH);
}
else
{
digitalWrite(currentPin, LOW);
}
}
else if (mode == 2)
{
digitalWrite(currentPin, HIGH);
delay(delayValue);
digitalWrite(currentPin, LOW);
}
else if (mode == 3)
{
if (dir)
{
digitalWrite(currentPin, HIGH);
digitalWrite(lastPin - currentPin + firstPin, HIGH);
}
else
{
digitalWrite(currentPin, LOW);
digitalWrite(lastPin - currentPin + firstPin, LOW);
}
delay(delayValue);
}
currentPin++;
if (currentPin > lastPin)
{
currentPin = firstPin;
dir = !dir;
if (mode == 1)
{
delay(delayValue);
}
}
}