Go Down

Topic: pwm with delay (Read 740 times) previous topic - next topic

TeslaIaint

Code: [Select]
int led = 3;
int i = 0;
long interval = 10;
long previousMillis = 0;
int lastcheck = 0;

void setup()
{
 Serial.begin (9600);
 pinMode (led, OUTPUT);
}
void loop()
{
 unsigned long currentMillis = millis();
for (int i=0;i<=255;i++)
{
 if (millis() - lastcheck > interval)
 {
   lastcheck = millis();
   analogWrite (led, i);
 }
 Serial.println (i);
}
}


The above code works as intended. It resets the led to 0 every time through the loop. The following code works in a different way. Once the led reaches 255, it stays there constantly lit. As I said a few times, I'm trying to learn different ways of implementing a delay without "delay()." That's why it "wasn't sticking."
Code: [Select]

int led = 3;
int i = 0;
long interval = 10;
long previousMillis = 0;

void setup()
{
 Serial.begin (9600);
 pinMode (led, OUTPUT);
}
void loop()
{
 unsigned long currentMillis = millis();
 if (currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;
   analogWrite (led, i++);
   i = constrain (i,0,255);
   Serial.println (i);
 }
// analogWrite (led, 0);
 //Serial.println (i);
}









Nick Gammon

Your first example in the previous post looks OK to me. Every 10 mS you are increasing the brightness by 1.

TeslaIaint

Thanks, Nick. That's what I'm trying to do. In my previous post I said that the first bit example of code that I modified from arrch was working correctly. It isn't. I doesn't increment every 10ms; It increments as fast as it can. The "delay" functions aren't working.  I'll play with it a bit and see if I can get it to work. I know I shouldn't worry about it as the second posted code in my previous post works, but I just like to try to understand things. Maybe there will be a situation where I could modify the code into something useful.

Nick Gammon

You had duplicate declarations if "i" there. This is a cut-down version:

Code: [Select]

int LED = 3;
unsigned long interval = 100;
unsigned long lastcheck = 0;

void setup()
 {
 }  // end of setup

void loop()
 {
 for (int i = 0; i <= 255; i++)
   {
   if (millis() - lastcheck > interval)
     {
     lastcheck = millis();
     analogWrite (LED, i);
     }  // end if time up
   }  // end of for
 }  // end of loop


Although I must confess that on my test board at least it isn't quite doing what I expect. The duty cycle seems to be jumping in a strange way.

Nick Gammon

Ach! I have it! The loops are the wrong way around. In your code you are adding to i, even if you don't send that value to the LED. In other words, you only send the updated "i" at intervals of "interval". Hence it jumps in brightness in large gaps.

This fades up nicely, relying on the byte value in "brightness" to wrap around at 255:

Code: [Select]

const int LED = 3;
const unsigned long interval = 10;
unsigned long lastcheck = 0;
byte brightness;

void setup()
 {
 }  // end of setup

void loop()
 {
 if (millis() - lastcheck > interval)
   {
   lastcheck = millis();
   analogWrite (LED, brightness++);
   }  // end if time up
 }  // end of loop
 

Go Up