At this point, I'd just like an answer so I can memorize it. I've been stuck on this for 2 days now trying every variation. My code is to make Christmas lights. I have here a shortened version with just 3 of the lights and only 1 pattern for proof of concept:
//LED Patterns
int L1 = 23;
int L2 = 22;
int L3 = 21;
int buttonPin = 39; //the number of the pushbutton pin
int potPin = 38;
int de=50; // delay time
int p=0; // variable for pattem
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
pinMode(L4, OUTPUT);
pinMode(L5, OUTPUT);
pinMode(L6, OUTPUT);
pinMode(L7, OUTPUT);
pinMode(L8, OUTPUT);
pinMode(L9, OUTPUT);
pinMode(L10, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(potPin, INPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println(potPin);
buttonState = digitalRead(buttonPin);
int potPinValue = analogRead(potPin);
if (buttonState == HIGH)
{
p++;
delay(100);
}
if(p==1)
{
digitalWrite(L1,1);
digitalWrite(L2,0);
digitalWrite(L3,0);
delay(de);
digitalWrite(L1,0);
digitalWrite(L2,1);
digitalWrite(L3,0);
delay(de);
digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,1);
delay(de);
}
}
As you can tell, 3 lights blink one after the other using delay(). p is a variable that increases everytime I push the button and it takes 100ms to switch to the next pattern. But I have not included the other patterns here so that we can focus on getting the potentiometer to work. I'd like to also dim/brighten them using a potentiometer whilst they are blinking. How can this be done?
Please don't tell me not to use delay() and to use state/millis() instead. I know this already, but for my own learning I want to see that delay() can be used and why it shouldn't be used afterwards. I know delay() blocks code and prevents consecutive code from being run during the time period. But I want to know whether it can be done on this code, so I can write a discussion on why it shouldn't be used and why millis/blink without delay is much better.
Many thanks for all your help and tutelage.