Delay in loop is slowing everything else

Hello all!

So I have this in my void loop:

void loop(void)
{ 
  analogWrite(PWMPin, analogRead(lightPin)/0.5); 
  
  
  delay(2000);

The thing is that I also have two pushbuttons that i want to use. These are now affected by this delay also. Is there any way around this?

I read something about using millis() but the example i was shown was with a button. I am a newbie at this so any tips on this matter would be greatly appreciated.

Hello,

Look the Blink Without Delay example :wink:

And read this too.

This demo several things at a time is an extended example of BWoD. It might make the concept clearer.

...R

guix:
Hello,

Look the Blink Without Delay example :wink:

And read this too.

Just finnished reading up on BlinkwithoutDelay. I tried the code and it worked!

First I copied the exact code but then it ended up turing itself on and off every second.

I then removed this part and it worked just as i wanted to. Thanks alot :slight_smile:

 // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

Dert: // if the LED is off turn it on and vice-versa:    if (ledState == LOW)      ledState = HIGH;    else      ledState = LOW;

ledState =!ledState;

does exactly the same thing in just 1 line of code.

const int ledPin =6; // PWM
int brightness = 0;
int fadeAmount = 5;
int counterStrobe =0;
static int counterFade =0;
static int strobeFlag=1;
int fadeFlag =1;
static int counter=0;
int ledState = LOW;
long int previousMillis =0;
const long interval = 30;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{

  if(counterStrobe ==0){
    while(strobeFlag!=0){
      unsigned long currentMillis=millis();
      if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;
        if(ledState==LOW)
          ledState = HIGH;
        else
          ledState = LOW;
        digitalWrite(ledPin,ledState);
        counterStrobe++;

        if(counterStrobe >=20)
        {

          strobeFlag=0;

        }
      }
    }
  } 
  else
  {

    for(counterFade;counterFade<50;counterFade++)
    {    
      analogWrite(ledPin, brightness);
      brightness = brightness + fadeAmount;
      if(brightness ==0 || brightness == 255)
        delay(30);

      counter=counter+1;

    }
    counterStrobe=0; 
    strobeFlag=1;  
    counterFade=0;
  }

}

Here simple code ; You can use try it once. it has both fading & flashing effect

if(brightness ==0 || brightness == 255)
        delay(30);

:astonished:

Dert: Hello all!

So I have this in my void loop:

void loop(void)
{ 
  analogWrite(PWMPin, analogRead(lightPin)/0.5); 
  
  
  delay(2000);

The thing is that I also have two pushbuttons that i want to use. These are now affected by this delay also. Is there any way around this?

I read something about using millis() but the example i was shown was with a button. I am a newbie at this so any tips on this matter would be greatly appreciated.

Have a look at the simpletimer library - makes the use of millis very easy for a beginner

Dert: analogWrite(PWMPin, analogRead(lightPin)/0.5);

Are you sure you want to do that? You are passing a float value from 0.0 to 2046.0 to a function that takes an integer value from 0 to 255.