Go Down

Topic: Only Execute Once (Read 2616 times) previous topic - next topic

Xenia2

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. 


Xenia2

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().



wildbill

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?

Xenia2

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

Nick Gammon


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


Other than the one I wrote? In what way did that not work?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Xenia2

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

Code: [Select]

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()
  ........



Xenia2

for those interested, this is my main loop.

Code: [Select]

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;
  }
}

AWOL

Code: [Select]
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
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

wildbill


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  ;) )

Nick Gammon


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

... 
Code: [Select]
  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:

Code: [Select]
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.

Quote
Code: [Select]
for (int x=0; x<1000; x++)
  {
    delay(1);
  }


That is the same as:

Code: [Select]
delay (1000);

Why make it more complicated?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Xenia2

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

Thank you Nick!

Xenia2

Code: [Select]

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.

pocketscience

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.
Is life really that serious...??!

wildbill

^^ 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?

Xenia2

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

Go Up