Pages: 1 [2]   Go Down
Author Topic: Only Execute Once  (Read 2192 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

for those interested, this is my main loop.

Code:
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;
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25827
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

"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.

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink )
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

... 
Code:
  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:
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:
for (int x=0; x<1000; x++)
  {
    delay(1);
  }

That is the same as:

Code:
delay (1000);

Why make it more complicated?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thank you Nick!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Sydney, Australia
Offline Offline
Full Member
***
Karma: 3
Posts: 230
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Is life really that serious...??!

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2]   Go Up
Jump to: