Arduino executing unwanted code during startup

Hi all I need some help and I'm fairly new to the Arduino world.

Basically, I need to detect a signal from a momentary switch and then put a LED on for 5 sec, every time a push at the button is detected. (This led is in real life a water pump that turns on and off for 5 seconds - but for simplicity, we will use an LED)

Everything works fine when the Arduino is running however I need to be able to turn the power on / off for the Arduino once in a while to save my battery usage. this is where my problem begins.

Every time I reset/boot my Arduino the LED runs for 5 sec and then stops. In other words it runs the code without the button being pushed. How can I avoid this ?
If it is because of floating pins I must be doing something wrong, because I am using pull-down resistors. (see attached picture).

image

Pull Down resistor: 10K ohm
Resistor before LED: 330 ohm

Here is my code:


int LEDPin=30; // Signal to LED
int ButtonPin=26; // input for reading on momentary switch 
int buttonRead; // momentary switch value (0=LOW) (1=HIGH)  


unsigned long timingPeriod;
unsigned long ElapsedTime;


void setup() {
 
Serial.begin(9600);

pinMode(ButtonPin, INPUT);

pinMode(LEDPin,INPUT);


}

void loop() {

//----------------------------------------------------------------//

buttonRead=digitalRead(ButtonPin);
Serial.println(buttonRead);

if (buttonRead==0)
   digitalWrite (LEDPin,LOW);

else if (buttonRead==1)

   timingPeriod = millis();
      ElapsedTime = (millis() - timingPeriod);
      
      if (ElapsedTime <= 5000)
      digitalWrite (LEDPin,HIGH);

      else 
      digitalWrite (LEDPin,LOW);

//---------------------------------------------------------------//

}

I really hope some code-WIZ can help me :slight_smile:

THANKS

That's ... unusual.

That did not help :frowning:, but thanks!

At startup, timingPeriod is zero. So for the first five seconds elapsed time is less than 5000, which causes the LED to be turned on.

You need another variable that tracks whether the button has actually been pressed. A boolean will do.

Edit: Or just put a delay(5001) in Setup.

But it made a difference, right? :wink:

1 Like

Thanks wildbill,

The delay works !

However how would you make a variable that keeps track of the button status?,

is that not what "int ButtonPin=26; // input for reading on momentary switch" does?

Can you tell me how you would do it ?`

Thank you so much !

or:

"else if (buttonRead==1)"

During the first five seconds, you need to know whether the button was pressed. Not whether it IS pressed, but whether it has been. When you detect a press, you set timingPeriod. You can use that as an indication. In your if that checks elapsed time, also check that timingPeriod is not zero.

@mhndk, your topic has been moved to a more suitable location on the forum.

Is this the only function of the Arduino?
This function can be achieved much faster, easier, and cheaper using a delay relay module with a 555 timer on it.
Should only cost a dollar or two.

int LEDPin=30; // Signal to LED
int ButtonPin=26; // input for reading on momentary switch 
int buttonRead; // momentary switch value (0=LOW) (1=HIGH)  


unsigned long timingPeriod;
unsigned long ElapsedTime;


void setup() {
 
Serial.begin(9600);

pinMode(ButtonPin, INPUT);

pinMode(LEDPin,OUTPUT);  // <-- You are OUTPUTING a high, don't use INPUT


}

void loop() {

//----------------------------------------------------------------//

buttonRead=digitalRead(ButtonPin);
Serial.println(buttonRead);

if (buttonRead==0)
// While not needed for a single line, IT does make it easier to read
{ 
   digitalWrite (LEDPin,LOW);
}
else if (buttonRead==1)
// Absolutely needed, or only the first line would be run.
{
   timingPeriod = millis();
      ElapsedTime = (millis() - timingPeriod);
      
      if (ElapsedTime <= 5000)
{
      digitalWrite (LEDPin,HIGH);
}
      else 
{
      digitalWrite (LEDPin,LOW);
}
//---------------------------------------------------------------//
}
}

Saves on a resistor for the LED? :face_with_raised_eyebrow:

1 Like

Hi missdrew.

When I use the code you have done the LED is only on while the momentary switch is pushed down?

The LED should be on for 5 sec, after a push on the switch

OK Try this

int LEDPin = 30;
int ButtonPin = 26;
int buttonRead;

unsigned long timeNow;
unsigned long ElapsedTime = 5000;

void setup()
{
  Serial.begin(9600);
  pinMode(ButtonPin, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  buttonRead = digitalRead(ButtonPin);
  Serial.println(buttonRead);

  if (buttonRead == LOW)
  {
    digitalWrite (LEDPin, HIGH);
    timeNow = millis();
  }
  else
  {
    if (millis() - timeNow >= ElapsedTime)
    {
      digitalWrite (LEDPin, LOW);
    }
  }
}

The button needs wired to GND, without the pull down resistor. This will keep the LED on for 5 seconds after the button is released.