Adding a delay to the code

Hi, lately I've been working on a project to control the brightness of an LED strip. So the LEDs are connected to pin 3 on the Arduino and the pushbuttons are connected to pins 7 & 8 on the Arduino and are pressed when shorted to ground. Pin 4 on the Arduino is set to be an input pin in a way that it only allows the LEDs to be lit when voltage is applied to the pin (pin 4), or in other words when pin 4 is HIGH, and turns off the LEDs when voltage is removed from that pin or when pin 4 is LOW. So pin 4 is connected through an 11 kilo ohm resistor to ground so it becomes normally in a LOW state and then any voltage applied to pin 4 lights up the LEDs. My question is that I want my LEDs to light up 100 milliseconds after pin 4 receives voltage, but after adding a simple delay function before the analogueWrite of pin 4, it works on the proper timing but for some reason I lose the ability to control the brightness of the LEDs. So can someone please tell me what I'm doing wrong as I am still a complete beginner to Arduino programming.

Here's my code so far: (I've tried adding the delay function right before the line "analogWrite(LED, brightness);" and that messed up my brightness control)

int brightness = 100;

//const byte LED       = 11;
//const byte maximum   = 10;
//const byte decrement = 8;
//const byte increment = 9;

const byte LED       = 3;
const byte maximum   = 4;
const byte decrement = 7;
const byte increment = 8;

void setup()
{
  pinMode(LED, OUTPUT);             // Set pin 3 to be in output mode
  pinMode(decrement, INPUT_PULLUP); // Set pin 7 to be in input mode
  pinMode(increment, INPUT_PULLUP); // Set pin 8 to be in input mode
  pinMode(maximum, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(decrement) == LOW)
  {
    brightness--;                  // If button on pin 7 is pressed, reduce brightness
  }

  if (brightness < 50)
  {
    brightness = 50;                // Don't let brightness drop below zero
  }

  if (digitalRead(increment) == LOW)
  {
    brightness++;                  // If button on pin 8 is pressed, increase brightness
  }

  if (brightness > 255)
  {
    brightness = 255;              // Don't let brightness get above 255
  }

  if (digitalRead(maximum) == HIGH)       //if pin 4 is HIGH
    {
    analogWrite(LED, brightness); // Set pin 3 to the new brightness level
  }
  else
  {
    analogWrite(LED, 0);          //else turn it off
  }

  delay(10);                      // Wait a short time, just to slow things down a little
}

You've got an external pull-down resistor attached to pin 4 yet you've configured it as a pullup internally, there's going to be some confusion.

Correct, so any ideas what shall I do to fix it?

According to your text and your code you want pin 4 active high which means idle low. If this is the case then the external pulldown is correct. Remove the pinMode statement for pin4. By default all pins are set to input so you don't need any pinMode declarations unless you're setting the pullup or configuring the pin for output (or changing from output to input for the pedants).

Hello again, so I just tried it and it did work but now what's happening is that with this 100 millisecond delay added the pushbuttons for the brightness control, pins 7 and 8, take a very long time to increase the brightness from 0 to 255 and vice versa; it seems that that 100 millisecond delay is interfering on the delay of the pushbuttons. The delay of the pushbuttons is only 10 milliseconds and is the last line of my code. Can you please tell me how can I isolate the delay of the push buttons from the delay of the LEDs turning on? I really appreciate your help.

Here's my code:

int brightness = 100;

//const byte LED       = 11;
//const byte maximum   = 10;
//const byte decrement = 8;
//const byte increment = 9;

const byte LED       = 3;
const byte maximum   = 4;
const byte decrement = 7;
const byte increment = 8;

void setup()
{
  pinMode(LED, OUTPUT);             // Set pin 3 to be in output mode
  pinMode(decrement, INPUT_PULLUP); // Set pin 7 to be in input mode
  pinMode(increment, INPUT_PULLUP); // Set pin 8 to be in input mode
 
}

void loop()
{
  if (digitalRead(decrement) == LOW)
  {
    brightness--;                  // If button on pin 7 is pressed, reduce brightness
  }

  if (brightness < 0)
  {
    brightness = 0;                // Don't let brightness drop below zero
  }

  if (digitalRead(increment) == LOW)
  {
    brightness++;                  // If button on pin 8 is pressed, increase brightness
  }

  if (brightness > 255)
  {
    brightness = 255;              // Don't let brightness get above 255
  }

  if (digitalRead(maximum) == HIGH)       //if pin 4 is HIGH
    {delay(100);
    analogWrite(LED, brightness); // Set pin 3 to the new brightness level
  }
  else
  {
    analogWrite(LED, 0);          //else turn it off
  }

  delay(10);                      // Wait a short time, just to slow things down a little
}

Read this

Use millis() with a while loop or a state machine. That way you can check the buttons while the LED's to nothing.

Here is a good explanation of how to use it: - (It is the same link as DKWatson's)
https://forum.arduino.cc/index.php?topic=503368.0

Hello again, so I've read those post and they really were helpful and informative but the problem is that I can't figure out how can I implement this into my code so can someone help me with this please or if there is a way to isolate the delay functions from each other.

Here's my code so far:

int brightness = 100;

//const byte LED       = 11;
//const byte maximum   = 10;
//const byte decrement = 8;
//const byte increment = 9;

const byte LED       = 3;
const byte maximum   = 4;
const byte decrement = 7;
const byte increment = 8;

void setup()
{
  pinMode(LED, OUTPUT);             // Set pin 3 to be in output mode
  pinMode(decrement, INPUT_PULLUP); // Set pin 7 to be in input mode
  pinMode(increment, INPUT_PULLUP); // Set pin 8 to be in input mode
 
}

void loop()
{
  if (digitalRead(decrement) == LOW)
  {
    brightness--;                  // If button on pin 7 is pressed, reduce brightness
  }

  if (brightness < 0)
  {
    brightness = 0;                // Don't let brightness drop below zero
  }

  if (digitalRead(increment) == LOW)
  {
    brightness++;                  // If button on pin 8 is pressed, increase brightness
  }

  if (brightness > 255)
  {
    brightness = 255;              // Don't let brightness get above 255
  }

  if (digitalRead(maximum) == HIGH)       //if pin 4 is HIGH
    {
    analogWrite(LED, brightness); // Set pin 3 to the new brightness level
  }
  else
  {
    analogWrite(LED, 0);          //else turn it off
  }

  delay(10);                      // Wait a short time, just to slow things down a little
}[code]

I can't figure out how can I implement this into my code

First you need to be clear on what you want to happen.

What should happen if the up/down buttons are pressed during the "delay" period ?

Should the timing period be ended early and restarted with the new brightness value ? Should the the timing period be ended early and restarted if the up/down buttons are pressed when they brightness is already at its upper or lower limits ?

Actually with the 100 millisecond delay that I'm trying to add, I do not want the brightness control to be active during that delay, like I don't mind if pressing the pushbuttons, within that delay timing, doesn't do anything. I'm trying to add a delay function that won't affect the delay timing of my pushbuttons.

I can't see where you want to add this 100ms delay or why you would add it.

The general principle of an Arduino program is to read all the inputs and check all the timers thousands of times per second. If an input has changed from what it was last time you looked or a time is up then you do something.

I do not want the brightness control to be active during that delay,

I'm trying to add a delay function that won't affect the delay timing of my pushbuttons.

These 2 statements are mutually exclusive. If the brightness control is not to be active during the delay() then the pushbuttons will be affected

If you only want the delay to occur when pin 4 is HIGH then move the delay() to immediately after you write the brightness to the LED

Hi, so I've changed my code to

int brightness = 100;

//const byte LED       = 11;
//const byte maximum   = 10;
//const byte decrement = 8;
//const byte increment = 9;

const byte LED       = 3;
const byte maximum   = 4;
const byte decrement = 7;
const byte increment = 8;

void setup()
{
  pinMode(LED, OUTPUT);             // Set pin 3 to be in output mode
  pinMode(decrement, INPUT_PULLUP); // Set pin 7 to be in input mode
  pinMode(increment, INPUT_PULLUP); // Set pin 8 to be in input mode
 
}

void loop()
{
  if (digitalRead(decrement) == LOW)
  {
    brightness--;                  // If button on pin 7 is pressed, reduce brightness
  }

  if (brightness < 0)
  {
    brightness = 0;                // Don't let brightness drop below zero
  }

  if (digitalRead(increment) == LOW)
  {
    brightness++;                  // If button on pin 8 is pressed, increase brightness
  }

  if (brightness > 255)
  {
    brightness = 255;              // Don't let brightness get above 255
  }

  if (digitalRead(maximum) == HIGH)       //if pin 4 is HIGH
    {
    analogWrite(LED, brightness); // Set pin 3 to the new brightness level
    delay(100);
  }
  else
  {
    analogWrite(LED, 0);          //else turn it off
  }

  delay(10);                      // Wait a short time, just to slow things down a little
}[code]


But it still didn't make the LEDs light up 100 milliseconds after pin 4 gets HIGH and it still interfered with my brightness control.

Hi, so I've changed my code to

Does it do what you want ?

Not really, it doesn't delay right after pin 4 is HIGH and it also affects my brightness control with that delay.

it doesn't delay right after pin 4 is HIGH

I bet it does but 100 milliseconds is not long in human terms. As an experiment change it to 1 second so that the delay is more obvious

it also affects my brightness control with that delay.

But earlier you said

I do not want the brightness control to be active during that delay,

I think that the problem is that you have not fully described your requirements. For instance, do you want the delay to happen only when the brightness is changed ?

The application in which my LEDs are functioning in unfortunately makes the 100 milliseconds pretty obvious but what I meant was that I don't mind wheather my brightness control is active or not during that 100 millisecond delay since as you said, 100 milliseconds is really nothing in human terms, but all I want is a 100 millisecond delay right before the LEDs light up when pin 4 is HIGH and I don't want that 100 millisecond delay to also delay the time it take the change the brightness when the pushbuttons are pressed.
When I don't add that delay, the pushbuttons respond within their 10 millisecond delay and do change the brightness within their appropriate timings, but when I add that 100 millisecond delay it does make the LEDs light up 100 milliseconds after pin 4 gets HIGH, which is exactly what I want, but is also make the push buttons also delay 100 milliseconds extra from their 10 millisecond timing meaning that when I press the pushbutton, I gotta press and hold it for a very time time to change the brightness from 0 to 255 and vice versa. Is their a way to make the delay of the LEDs lighting up (the 100 millisecond delay) independent from the 10 millisecond delay of the pushbuttons?

Any suggestions?

Not really, it doesn't delay right after pin 4 is HIGH and it also affects my brightness control with that delay.

What is this referring to? We need more good descriptive info if you want any help.

So basically you want it to wait for 100ms before it starts to change the brightness?
Is that correct?
If so, try using a 100ms debounce. That should do it.

else if {
Serial.print("I have no idea what you want")
}