Only Execute Once

My replacement laptop just came in, so I got a chance to test the code.

I have to find another way to get the LEDs to flash in the main loop only once at startup. The flashing LEDs inside the setup() did not work the way I wanted. I would like the LEDs to flash every one second for six seconds.

I showed you how to execute something once in the main loop earlier. Refer to that... and then actually have a go at writing some code. If you have specific problems then I'm sure we can assist, but we're not going to write your code for you - well I'm not anyway...

What you are asking is trivial in the extreme - you need to a) keep track of how many times you've flashed the LED, and b) keep track of the 1 second spacing...

G.

^ Oh I am not expecting you the write the code :slight_smile: I was just looking for examples or how to get started.

Xenia2:
My replacement laptop just came in, so I got a chance to test the code.

I have to find another way to get the LEDs to flash in the main loop only once at startup. The flashing LEDs inside the setup() did not work the way I wanted. I would like the LEDs to flash every one second for six seconds.

What was the code you used to do this? What did it do? What did you want to be different?

This is just enhancement to my current code, which monitor the water level, if water fall below the sensor it will turn on the pump.

Right now every six seconds it read the sensor value, if LOW turn on the pump (yellow LED on), after six seconds if HIGH turn off the pump (green LED on). So at startup it will wait six seconds before anything happens, after six second one of the LED turn on (either pump on or pump off state). I wanted to take that only intial six seconds and have the all LEDs to flash before one of the LED remain solid on.

It was suggested that I can get the LED to flash inside setup(), but I dont see how to get its flash without inside a main loop().

A large number of people on the forum could very easily make helpful suggestions, but seeing your code is crucial to those suggestions making sense.

Also, why do you want a six second delay when the program starts? Is it a desired feature, or a side effect of how loop is structured?

Because after pump turn on, I wanted its to remain on for 6 more seconds...just a buffer to water level.

Xenia2:
I was just looking for examples or how to get started.

Other than the one I wrote? In what way did that not work?

Nick, below is what I took it from your example. It stay on for 1 second and off, its doesn't flash.

int red = 8;        // LED red
int green = 4;      // LED green
int amber = 2;      // LED amber

void setup()
{
  .....
  
  pinMode(red, OUTPUT);             // set the digital pin 81 as output
  pinMode(green, OUTPUT);          // set the digital pin 4 as output
  pinMode(amber, OUTPUT);          // set the digital pin 2 as output
  
  digitalWrite(green, HIGH);
  for (int x=0; x<1000; x++)
  {
    delay(1);
  }
  digitalWrite(green, LOW);
  for (int x=0; x<1000; x++)
  {
    delay(1);
  }
  
}
  
void loop() 
  ........

for those interested, this is my main loop.

void loop()
{
  relay_on_time = millis();
  if (relay_on_time >= (loopTime + 6000))
  { 
    val = digitalRead(inPin);
    if (val == HIGH)
    {
      digitalWrite(green, LOW);
      digitalWrite(amber, HIGH);                //amber LED on
      digitalWrite(relayPin, HIGH);
      if ( 0 == shutDownTime)                 // <<< changed
        shutDownTime = relay_on_time + 30000; // <<< changed
    }
    if (val == LOW)
    {
      digitalWrite(amber, LOW);              // amber LED off
      digitalWrite(green, HIGH);              // green LED on
      digitalWrite (relayPin, LOW);
      shutDownTime = 0;                       // <<< changed
    }
    if ( shutDownTime && shutDownTime <= relay_on_time )      // <<< changed
    {                                         // <<< changed
      digitalWrite(amber, LOW);
      digitalWrite(green, LOW);                // green LED off
      digitalWrite(relayPin, LOW);           // <<< changed
      while(1)                               // <<< changed
      {                                      // <<< changed
        // wait for reset                    // <<< changed
        
        digitalWrite(red, HIGH);              // red LED on, wait for reset
      }                                      // <<< changed
    }                                         // <<< changed
    loopTime = relay_on_time;
  }
}
val = digitalRead(inPin);
    if (val == HIGH)
    {
      digitalWrite(green, LOW);
      digitalWrite(amber, HIGH);                //amber LED on
      digitalWrite(relayPin, HIGH);
      if ( 0 == shutDownTime)                 // <<< changed
        shutDownTime = relay_on_time + 30000; // <<< changed
    }
    if (val == LOW)

If "val" isn't HIGH as a result of a digitalRead, it is unlikely to be anything other than LOW, so retesting it is a bit of a waste of time.
A simple "else" would suffice

Xenia2:
Nick, below is what I took it from your example. It stay on for 1 second and off, its doesn't flash.

Nick's example does exactly what you want as is, if you increase the delays, no need to make the adaption of it (that broke it :wink: )

Xenia2:
Nick, below is what I took it from your example. It stay on for 1 second and off, its doesn't flash.

...

  digitalWrite(green, HIGH);

for (int x=0; x<1000; x++)
  {
    delay(1);
  }

Well you took out the part that flashed it, didn't you?

How about:

for (byte i = 0; i < 6; i++)
  {
  digitalWrite (green, HIGH);
  delay (1000);
  digitalWrite (green, LOW);
  delay (1000);
  }

That's on for 1 second, then off for 1 second. Tweak the delay as desired.

for (int x=0; x<1000; x++)

{
    delay(1);
  }

That is the same as:

delay (1000);

Why make it more complicated?

Ok, its work. I have to exam how this code works now :slight_smile:

Thank you Nick!

for (byte i = 0; i < 6; i++)
  {
  digitalWrite (green, HIGH);
  delay (1000);
  digitalWrite (green, LOW);
  delay (1000);
  }

I haven't try it yet, but just curious will the flashing still work if I use mills() instead of delay()? Of course the coding structure will change too.

Anything's possible, so you could re-write that to use millis() instead of delay(), but why would you want to?? You'd have to spin in a tight loop waiting for millis() to go beyond a previously recorded value from millis(), which is basically what delay() does...

G.

^^ This. Or to put it another way, using millis is useful when you want your code to do other things during the delay, which is often the case and can give the illusion of doing many things at once. Here, you have no such need, so why complicate your code?

Just keep an open option if I want to expand the program to do something else in future :slight_smile: