Go Down

Topic: Of Millis, Fade and Stay ON or Off (Read 200 times) previous topic - next topic

Polliwog

Hello Everyone.  I am new here and new to Arduino programming.  I am experimenting with millis() to better understand it.  I found a sketch at The Bald Engineer to fade an LED up and then down. I think I understand this pretty much.  I also read over the Tute here.
 I thought I might try altering the code so the led will fade in, then stay on a few seconds, then turn off. However, now I get nothing.  Could someone please take a look at the this code and tell me where I am going wrong.
Code: [Select]
#define UP 0 
#define DOWN 1
 
const int minPWM = 0;
const int maxPWM = 255;
 
byte fadeDirection = UP;
 
int fadeValue = 0;
 
byte fadeIncrement = 5;
 
unsigned long previousFadeMillis;
unsigned long previousMillis;
int fadeInterval = 50;
int onInterval = 3000;


void setup() {
  analogWrite(pwmLED, fadeValue);
}
 
void doTheFade(unsigned long thisMillis) {
 
     if (thisMillis - previousFadeMillis >= fadeInterval) {
   
     if (fadeDirection == UP) {
         fadeValue = fadeValue + fadeIncrement; 
     if (fadeValue >= maxPWM) {
 }             
 }       
          fadeValue = maxPWM;
      if (pwmLED, maxPWM)  {
 }      analogWrite(pwmLED, HIGH);
      if (thisMillis - previousMillis >= onInterval) {
 }     analogWrite(pwmLED, LOW);
 } 
 
    previousMillis = thisMillis;
    previousFadeMillis = thisMillis;
  }

 
void loop() {
                                                          // get the current time, for this time around loop
  unsigned long currentMillis = millis();                    // all millis() timer checks will use this time stamp
   
  doTheFade(currentMillis);
}
 

larryd

Use CTRL T ( or CMD T )  to format your code.

What is this ?
if (pwmLED, maxPWM)


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Delta_G

#2
Aug 21, 2019, 04:21 am Last Edit: Aug 21, 2019, 04:22 am by Delta_G
You've got braces all over the place in the doTheFade function.  Press control-T in the IDE to autoformat it and line up the blocks and you should be able to see where your logic wen't wrong. 

Code: [Select]
if (pwmLED, maxPWM)

What's the comma doing?  Did you mean ==?

(Ninja posted by @larryd  at least we agree)
|| | ||| | || | ||  ~Woodstock

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

Polliwog

Thanks
That was a mistake.  I changed it to;   if (pwmLED  == maxPWM)   However, I am still getting nothing on the LED.

Delta_G

Thanks
That was a mistake.  I changed it to;   if (pwmLED  == maxPWM)   However, I am still getting nothing on the LED.
Yeah, you still got braces all over the place.  It matters where you put those. 
|| | ||| | || | ||  ~Woodstock

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

Polliwog

Well guys,  still not working.  Here is the corrected code.  Still does nothing.

Code: [Select]
// Example Fading LED with analogWrite and millis()
// See baldengineer.com/fading-led-analogwrite-millis-example.html for more information
// Created by James Lewis

#define UP 0
#define DOWN 1

int pwmLED = 5;

const int minPWM = 0;
const int maxPWM = 255;

byte fadeDirection = UP;

int fadeValue = 0;

byte fadeIncrement = 5;

unsigned long previousFadeMillis;
unsigned long previousMillis;
int fadeInterval = 50;
int onInterval = 3000;


void setup() {
  analogWrite(pwmLED, fadeValue);
}

void doTheFade(unsigned long thisMillis) {

  if (thisMillis - previousFadeMillis >= fadeInterval) {

    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement;
      if (fadeValue >= maxPWM) {
         fadeValue = maxPWM;
     }
    }
    if (pwmLED == maxPWM)  {
       analogWrite(pwmLED, HIGH);
    }
    if (thisMillis - previousMillis >= onInterval) {
        analogWrite(pwmLED, LOW);
 
  }
  previousMillis = thisMillis;
  previousFadeMillis = thisMillis;
}
}

larryd

#6
Aug 22, 2019, 01:28 am Last Edit: Aug 22, 2019, 01:29 am by larryd
Hint

int pwmLED = 5;

. . .

if (pwmLED == maxPWM)




No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Delta_G

So 5 can't equal 255?  Seems like we've already talked about that in this thread. 
|| | ||| | || | ||  ~Woodstock

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

Polliwog

Thank you.  A little help sometimes is all is needed

Polliwog

Well,  I did all the corrections suggestions, still see nothing but a dimly lit LED haunting me.  Thanks for all your help.  It back t the tutorial boards for me.

larryd

Show us your latest incarnation.



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Polliwog

Show us your latest incarnation.
Here it is
Code: [Select]
// Example Fading LED with analogWrite and millis()
// See baldengineer.com/fading-led-analogwrite-millis-example.html for more information
// Created by James Lewis

#define UP 0
#define DOWN 1

int pwmLED = 3;

const int minPWM = 0;
const int maxPWM = 255;

byte fadeDirection = UP;

int fadeValue = 0;

byte fadeIncrement = 5;
unsigned long thisMillis2;
unsigned long previousFadeMillis;
unsigned long previousMillis;
int fadeInterval = 50;
int onInterval = 3000;


void setup() {
  analogWrite(pwmLED, fadeValue);
}

void doTheFade(unsigned long thisMillis) {

  if (thisMillis - previousFadeMillis >= fadeInterval) {

    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement;
      if (fadeValue >= maxPWM) {
         fadeValue = maxPWM;
         
       if (fadeValue == maxPWM) {     
         analogWrite(pwmLED, HIGH);
       } 
     }
    }
    if (pwmLED == HIGH)  {
       
        if (thisMillis - previousMillis >= onInterval) {
     
        analogWrite(pwmLED, LOW);
   
        }
  previousMillis = thisMillis2;
  previousFadeMillis = thisMillis;
    }
  }
}
void loop() {
  // get the current time, for this time around loop
  unsigned long currentMillis = millis();                    // all millis() timer checks will use this time stamp

  doTheFade(currentMillis);
}

larryd

You have:
int pwmLED = 3;

You then you have:
if (pwmLED == HIGH)

You essentially have:
if (pwmLED == HIGH)  or with numbers  if (3 == HIGH)

Stop, and read the above over and over until you understand the problem ;) .


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

TheMemberFormerlyKnownAsAWOL

#13
Aug 22, 2019, 07:04 pm Last Edit: Aug 24, 2019, 08:19 pm by TheMemberFormerlyKnownAsAWOL
Code: [Select]
    if (fadeValue == maxPWM) {   
         analogWrite(pwmLED, HIGH);

That's not very bright.
(Pun intended)

Polliwog

Hey guys.
My original idea was to fade in a red LED, then hold it high for a for a while.  After it has been high for a bit , then fade in a yellow to make orange.  Then fade out the red to make yellow only for a time.  Then continue with green and blue to simulate a rainbow. To me, it's quite daunting, after learning how to fade without delay. 

Thing is, I am getting really frustrated now so I think the best thing is to leave it and go on to some other projects for a while.

Thanks everyone.


Go Up