Delay function stopped working without any change

Hi, my delay function stopped at once without doing anything, using and Arduino Uno.

I took this code from an youtube tutorial and it worked fine until next day... just starting the arduino and everything runs as normal exept the delay function does not work anymore.

Installed the IDE again, reset the board, tried some other code like timer=0, using microseconds..

Anybody experienced a problem with the board without changing anything?

//set pin numbers
const int ledPin = 2;
const int buttonPin1 = 4;
//variables will change
int buttonState1 = 0;
int timer = 0; //added myself, just a try

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

void loop() {

  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {
  digitalWrite(ledPin, HIGH);
  Serial.println("LED ON 10 Detik");
  delay(100);
   
  }

else {

    digitalWrite(ledPin, LOW);
    Serial.println("LED OFF -------");
    }
}

What do you expect this program to do?

Welcome to the forum

What makes you think that the delay() function is not working ?

Have you got a pulldown resistor on pin 4 keeping it LOW when the button is not pressed ?

Try this - connect button between pin 4 and GND

//set pin numbers
const byte ledPin = 2; // Use byte for pin numbers, half the memory of int type
const byte buttonPin1 = 4;
//variables will change
int buttonState1 = 0;
int timer = 0; //added myself, just a try

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

void loop() {

  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW) {
  digitalWrite(ledPin, HIGH);
  Serial.println("LED ON 10 Detik");
  delay(1000);
  }

else {
    digitalWrite(ledPin, LOW);
    Serial.println("LED OFF -------");
    }
}
1 Like

Thankyou, i might think something happens to the hardware of the board because i did not change anything in between the problem occurred.

I tried a pulldown resistor yes but thats interrupting the fet module that i operate with is, and by the way the circuit switches as it should only without the delay

It is a light sensor module dat gives a signal (button) which switches a fet module (led).

You still have not answered

I thought i answered like below, do i miss something?

You have not said what the code should do and how the problem manifests itself

Load the blink sketch from the IDE examples and try that. (The one with delay() of course, not blinkwithoutdelay)

I see, well i use a light sensor module which is the ‘button’ and a fet module that is mentioned as ‘led’, so when the light module gives a signal the fet has to switch for a specific time (delay), in this case mentioned 100 miliseconds

Print the value of buttonState1 after you read the input. What do you see in the Serial monitor ?

Did you load and test blink yet, @ufodeluxe

What do you expect to see with a delay of 100 milli seconds?

Try delay(1000) for funzies.

The code in #4 also had that... I wonder if OP tried it yet.

1 Like

We don't even know if the LED light at all! Could be as simple as OP has mounted it in reverse!

1 Like

Thanks, i loaded the blink sketch and that one functions normally...

yes i tried but that does not change anything

You mean with a pullup resistor? that causes the fet module stops working

This is my first project, 'Print the value of buttonState1 after you read the input', it's not clear how to implement this... The serial monitor shows 'led on 10 detik' repeating