Search code examples
c++arduinodelayfade

Arduino delay only in one part of the code


Hello i am new to stackoverflow. I have a problem

    if (brightness2 == 0 || brightness2 == 255) {
    fadeAmount2 = -fadeAmount2 ; 
  }  
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);

and

Serial.println((byte)tempC);   
  delay(1000);

the second delay affects the first and the led fade is much more slow. My question is how can I apply a delay to only one part of the code and another to another. Hope you understood what i mean. Thanks in advance.


Solution

  • You can't. But you can count to 33:

    static int cnt = 0;
    
    if (brightness2 == 0 || brightness2 == 255) {
        fadeAmount2 = -fadeAmount2 ; 
    }
    
    delay(30);
    cnt = (cnt + 1) % 33;
    if (cnt == 0) {
        Serial.println((byte)tempC);
    }
    

    This will make the fading work as fast as before, and the printing will fire every once in 990 milliseconds.