Go Down

Topic: How to Blink LEDs and Dim them at the same time using a Potentiometer - delay() (Read 322 times) previous topic - next topic

FloatingWatcher

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:

Code: [Select]
//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.

UKHeliBob

Quote
I know delay() blocks code and prevents consecutive code from being run during the time period.
There is your answer
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

Make your delays shorter, but use more of them.
Inbetween the delays, do more stuff.

At some point, you'll realise that the delays are so short, they may as well not be there at all.

FloatingWatcher

Make your delays shorter, but use more of them.
I between the delays, do more stuff.

At some point, you'll realise that the delays are so short, they may as well not be there at all.
I'm putting "analogWrite(L1, potPinValue/4);" in the same block as the digitalWrite blink sequences, but it seems to just overwrite anyway.

Can you go into abit more depth?  Preferably with an example please?

FloatingWatcher

There is your answer
So is there no way at all to have the Potentiometer dim the LEDs whilst they blink?  The potentiometer is not exactly a "timed" event... yet it still just overwrites everything I do.

Could you enlighten me please?

Delta_G

So is there no way at all to have the Potentiometer dim the LEDs whilst they blink?  The potentiometer is not exactly a "timed" event... yet it still just overwrites everything I do.

Could you enlighten me please?
What do you mean about it overwriting?  You never use the value you read from it.  You read a value, store it in a variable and then never use that variable for anything.  You have to actually write some code for the pot to do anything.  If you want what you describe then there should be an analogWrite in there somewhere. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

UKHeliBob

So is there no way at all to have the Potentiometer dim the LEDs whilst they blink?  The potentiometer is not exactly a "timed" event... yet it still just overwrites everything I do.

Could you enlighten me please?
Of course you can have a pot dim the LEDs whilst they blink.  Using millis() for timing would allow you to read the pot frequenty to update the brightness of the LEDs
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

FloatingWatcher

What do you mean about it overwriting?  You never use the value you read from it.  You read a value, store it in a variable and then never use that variable for anything.  You have to actually write some code for the pot to do anything.  If you want what you describe then there should be an analogWrite in there somewhere. 
I mention in a reply that I used analogWrite(LED, potentiometervalue); within the same code block as the blink pattern.  All it did was overwrite the blinking of the LED I was dimming/brightening.

UKHeliBob

Start with the BlinkWithoutDelay example. 

Read the pot each time through loop().  Change the program so that the value derived from analgRead() is written to the LED on each pass through loop() when the LED state indicates that it is turned on, otherwise write 0 to it.  Use analogWrite() instead of digitalWrite() to control the LED and use a pin that is PWM enabled. 
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

evanmars

I mention in a reply that I used analogWrite(LED, potentiometervalue); within the same code block as the blink pattern.  All it did was overwrite the blinking of the LED I was dimming/brightening.
Show that code.

Delta_G

I mention in a reply that I used analogWrite(LED, potentiometervalue); within the same code block as the blink pattern.  All it did was overwrite the blinking of the LED I was dimming/brightening.
So you have some code that you want help with.  But instead of posting that code you posted some other code that you didn't need help with.  How smart is that?   Coding is a game for people that think.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

FloatingWatcher

How smart is that?   Coding is a game for people that think.
If everyone was good at coding, do you think there would be a help forum?  For the record, I'm a Plasma Physicist (PhD) as well as I'm a simulations expert (Comsol and Opera3D).  I'm crap at programming code though.  Its nothing about my intelligence, I'm not a natural programmer nor do I consistently program.

Code: [Select]
//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(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)
  {
 analogWrite(L1, potentiometervalue/4);
 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);

  }
             
}


^ This is what I was referring to.  I put an analogWrite in the same code block as the blink patterns, but it overwrites the blinking of the LED that I want to dim.  Turning the potentiometer does dim/brighten it, but it no longer blinks.  The pins are all on PWM.

UKHeliBob

Let's look at some of your code

Code: [Select]

int L1 = 23;

From the pin number I assume that you are using a Mega
Pin 23 on a Mega is not a PWM pin so can only output a digital (HIGH or LOW) signal

Code: [Select]

 analogWrite(L1, potentiometervalue/4);
 digitalWrite(L1,1);

Here you use the analogWrite() function to write a value to a non PWM pin so that is not going to work as expected.  Then you write 1 (ie HIGH ) to the same pin using digitalWrite()  Is that likely to work ?  The pin, even if it were a PWM pin, cannot do 2 things at the same time.

I appreciate that you are not a programmer but surely getting your PhD required logical thinking and it is illogical to expect the pin to do 2 different things at the same time even if it were capable of doing either of them.

Suppose that I told you to turn the dimmer on a lamp to half brightness then a couple of microseconds later to turn the lamp off.  Would you be surprised if it were not on at half brightness ?

You will get plenty of help here but you must be prepared to listen to advice.  You appear to know that the best way to do what you want involves using millis() for timing but you don't seem to like that answer.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Delta_G

If everyone was good at coding, do you think there would be a help forum?  For the record, I'm a Plasma Physicist (PhD) as well as I'm a simulations expert (Comsol and Opera3D).  I'm crap at programming code though.  Its nothing about my intelligence, I'm not a natural programmer nor do I program.
It's not about your programming skill.  You don't need to be a programmer to understand that if you want help with a piece of code then you need to show the piece you need help with.  Any idiot could understand that. 

When you have a problem with your car do you take your friends car to the mechanic?  No, you bring the car that has the problem.  That's not something you need to be good at programming to understand.  That just takes basic intelligence. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

FloatingWatcher

Let's look at some of your code

Code: [Select]

int L1 = 23;

From the pin number I assume that you are using a Mega
Pin 23 on a Mega is not a PWM pin so can only output a digital (HIGH or LOW) signal

Code: [Select]

 analogWrite(L1, potentiometervalue/4);
 digitalWrite(L1,1);

Here you use the analogWrite() function to write a value to a non PWM pin so that is not going to work as expected.  Then you write 1 (ie HIGH ) to the same pin using digitalWrite()  Is that likely to work ?  The pin, even if it were a PWM pin, cannot do 2 things at the same time.

I appreciate that you are not a programmer but surely getting your PhD required logical thinking and it is illogical to expect the pin to do 2 different things at the same time even if it were capable of doing either of them.

Suppose that I told you to turn the dimmer on a lamp to half brightness then a couple of microseconds later to turn the lamp off.  Would you be surprised if it were not on at half brightness ?

You will get plenty of help here but you must be prepared to listen to advice.  You appear to know that the best way to do what you want involves using millis() for timing but you don't seem to like that answer.
"I appreciate that you are not a programmer but surely getting your PhD required logical thinking and it is illogical to expect the pin to do 2 different things at the same time even if it were capable of doing either of them."

Please don't do this.  I came here to learn and to upskill.  Not to have my intelligence questioned.  I'm using a Teensy 3.5.  Pin 23 is PWM.  (https://images.app.goo.gl/79FXxfvJM8qXARD98).  And I was questioning not whether it can do 2 things at the same time, but if it can do it one after the other.  Why can't I spend a few milliseconds modulating the power to the pin, then have it blink as normal a few milliseconds later?  So my mind is telling me that the problem is that its in the loop and it KEEPS modulating, thereby overwriting the blink part on that LED.  Is there a way to not have this happen?  I can do it with millis() yes, is it possible to do it with delay() however horrible the code may be?  That is a learning experience for me and I'd like to have it.  I'm a hobbyist, this isn't school work so I have time to learn every nook and cranny.

"Suppose that I told you to turn the dimmer on a lamp to half brightness then a couple of microseconds later to turn the lamp off.  Would you be surprised if it were not on at half brightness ?"

Can the LED not blink at a lower PWM value that I set with the Potentiometer though?  I can already fade an LED using a pot, so surely I can use a pot to set a PWM value to a lower value as it blinks?  I appreciate that the code I wrote just turns it off, but its supposed to come back on again in a few ms at a lower power.

"You appear to know that the best way to do what you want involves using millis() for timing but you don't seem to like that answer."

Like I mentioned, I wrote a code that blinked lights using millis() and I used use a pot to dim them or turn off the lights 1 by 1 whilst others were still blinking.  So yes I know I can use millis().  But I'm doing a youtube series where I start electricity, electronics and coding from scratch.  I'm 4 videos in and I haven't talked about millis().  The last video was using delays(), a potentiometer and fading an LED.  I wanted to capitalize on that by building Christmas Lights and showing people how use a pushbutton to change the patterns on the Christmas Lights.  I've done this.  But I thought it would be interesting to show how you can dim the lights too whilst blinking.

If I introduce Millis() in the video, I've jumped without explaining why its better to use millis().  The christmas light presents an example where delay() can be used, but due to its clunkiness and difficulty, why we will use millis() from here on out.  I had that lesson in mind both to cement the learning to myself, and to teach others.

That is why I'm intent on seeing if delay() can be used in this situation.

Go Up