where/how to place "blink without delay"

Have started on some code where I would like to have an LED blink while in the delay sections.

Wondering if possible to do.

I’ve included it in different places like after the delay starts, or before,and in the “loop” section but it doesn’t work as it does with the original “blink without delay”. LED cycles on and off solid every second cycle of the on and off times.

I’ve included just the basic idea of the code but perhaps you will need to see more.

void loop(){
//if LDR is < a certain value
//do this();
//else
//do that();
}

void this(){
//digitalWrite(relay1,HIGH);
//delay(timeOne);
//digitalWrite(relay1,LOW);
//delay(timeTwo);

}

void that(){
//digitalWrite(relay1,HIGH);
//delay(timeThree);
//digitalWrite(relay1,LOW);
//delay(timeFour);

}

The point of blink without delay is to show you how to do things WITHOUT USING DELAY.

Mark

The delay is for other things....just need the blink without delay to show the main delay is running.

You want a Blink Without Delay, inside a delay(), is that right?

Well the whole problem with delay() is that it, well, delays. So by definition, nothing happens inside a delay(), not even a Blink Without Delay.

What I think you should be doing, is to level up from using delay() to Blink Without Delay for what you refer to as your "main delay" in the first place. Then you could run another Blink Without Delay to show that Main Blink Without Delay is running.... if it's not obvious from the output that it's running anyway.

You may find this video useful, along with Robin2's ideas here.

You need to remove ALL delays and implement a state machine.

Here is another take on things. http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

ok...I'll take a look at suggestions. Tried the millis approach but it gets rather complex quickly with the many states. Thanks ....

If you just want to delay while blinking an LED try this function:-

// Blinky delay - By Grumpy Mike

void setup(){
  
}

void loop(){
  blinky_delay(13, 4000); // blink LED on pin 13 during the 4 second delay
}

void blinky_delay( byte led, unsigned long duration){
  unsigned long base_time;
  pinMode(led, OUTPUT);
  base_time = millis();
  while( millis() - base_time < duration){
    if(millis() & 0x200UL) digitalWrite(led, HIGH); else digitalWrite(led, LOW);
  }
  digitalWrite(led, LOW);
}

bluejets: Tried the millis approach but it gets rather complex quickly with the many states

It can, yes, but if you implement a state machine as suggested by GM on his myzen site, or by Nick Gammon it keeps things ordered.

This code several things at a time is an extended demo of BWoD. It may help you to grab the concept.

...R

Robin2: This code several things at a time is an extended demo of BWoD. It may help you to grab the concept.

...R

I already linked to that in Reply3....

Mind you, if you just want a blinking delay - in seconds - and nothing else happening while you delay, you just construct a function:

void blinky(int total) { // time now in seconds
  int tt = 0;
  while(tt < total) {
    digitalWrite (13, HIGH);
    delay(500);
    digitalWrite (13, LOW);
    delay(500);
    tt += 1; // or whatever the sum of the blink time is.
  }
}

JimboZA: I already linked to that in Reply3....

Much appreciated and many apologies for missing your link.

...R

Have been studying the "several things at the same time" and starting to apply it to the present application, albeit in small amounts at a time. My arrangement as I said previously, contains 2 periods, each has an interval and duration which are covered in the example. However the periods themselves are seperated, not by a time base but rather an amount of daylight being present or not.

Therefore I am assuming I will have to do an extra calculation at the beginning of each period to bring the so called "previousPeriod" up to the present time.

Any comment on this....Thanks...

However the periods themselves are seperated, not by a time base but rather an amount of daylight being present or not.

No idea what you mean. In the code it is time. What you do in that time is up to you. As you haven't posted any relevant code or said exactly what you want to do it is hard to help.

bluejets: Therefore I am assuming I will have to do an extra calculation at the beginning of each period to bring the so called "previousPeriod" up to the present time.

Can only comment if you post your code.

...R

Haven't written it yet because of my query... however..... if daylight(determined by LDR sensor) run pump for 1 hour then pause for 15 minutes ....repeat over and over (this I can cover with your several things etc.) until LDR sensor determines it is night.

Then run same pump for 30 minutes then pause for 30 minutes...repeat over and over until LDR sensor determines it is day again and then repeat first sequence.

During pause periods, flash an LED to show it is timing out.

Haven't written it yet

Then write something because I am not sure what you are asking. We don't write code for you.

bluejets:
if daylight(determined by LDR sensor) run pump for 1 hour then pause for 15 minutes …repeat over and over
(this I can cover with your several things etc.)
until LDR sensor determines it is night.

That sounds about right. You need a variable that records the “state” of daylight as determined by the LDR.

As @Grumpy_Mike says, write some code. While it is silly to start writing code without any thought it is equally unproductive to try to figure out the whole thing before trying anything. The strength of the Arduino is the ability to learn by doing.

And most programs are a collection of small parts that are each known to work on their own.

…R

bluejets:
Haven’t written it yet because of my query

As others say, you have to start somewhere.

So, if we write your requirement here using the Arduino IDE, we have:

long const minute = 60000UL;
long const blinkmin = 60UL;

void loop() {
  if(daylight()) {
    digitalWrite(pump, HIGH);
    delay(60UL * minute);
    digitalWrite(pump, LOW);
    blinky(15UL * blinkmin);
  } 
  else {
    digitalWrite(pump, HIGH);
    delay(30UL * minute);
    digitalWrite(pump, LOW);
    blinky(30UL * blinkmin);
  }
}

This code works but I want to incorporate other things such as a blinking LED when it is in a “delay”.

As I was told before and realise now, nothing happens in any delay so I am trying to get a similar arrangement with the “several things at the same time” approach and rewrite my working arrangement with Millis().

At first glance it might appear to be a straightforward"conversion" of the “several things at the same time” but…
as I said earlier, the 2 periods in MY code are seperated by the day/night which is not time based.

So I was simply asking if my thoughts about having to do some extra updating of “previousMillis” because of this would be correct.
I am not expecting anyone to write the code as someone suggested, in fact I would much rather do it all myself.

//pump control
//pump connected via low voltage relay to 240V outlet.
//relay operates via NPN transistor from pin 12.
//pump will run in 2 different modes, Day and Night.
//an LDR sensor used as a light level detector....above 600 value is daytime......below 400 level is nighttime
//sensor values converted thus.....0.0049V per unit resulting in a level somewhere between 0 and 1023.
//a voltage divider is setup with LDR to supply rail and 10K resistor to ground, input to pin A0 from centretap.
//400*0.0049=1.96V.........600*0.0049=2.94V


int delayLed=13;                //flash when pump is on delay/or interval cycle
int sensorPin=A0;              //pin for LDR input
long int sensorValue=0;        //value of LDR input
int dayLed=10;                    //indicator LED for daytime
int nightLed=9;                    //indicator LED for nighttime
int pumpPin=12;                  //drive via transistor and relay to pump
byte pumpPin_state=LOW;          //state of the pump either running "HIGH" or interval "LOW"
int sensorPower=7;              //power sensor for TESTING ONLY
unsigned long dayOn=6*1000UL;        //1*1000UL TEST ONLY(6 seconds)....change to 60*60*100UL (60 minutes) for running
unsigned long dayInterval=6*1000UL;    //1*1000UL TEST ONLY(3 seconds)...change to 15*60*1000UL (15 minutes) for running
unsigned long nightOn=3*1000UL;      //4*1000UL TEST ONLY(4seconds).... change to 30*60*1000UL (30 minutes) for running
unsigned long nightInterval=3*1000UL;  //4*1000UL TEST ONLY(4 seconds)....change to 30*60*1000UL(30 minutes) for running
int delayLed_A=8;


void setup(){
  pinMode(delayLed_A,OUTPUT);
  pinMode(delayLed,OUTPUT);
  pinMode (dayLed,OUTPUT);
  pinMode (nightLed,OUTPUT);
  pinMode (pumpPin,OUTPUT);
  pinMode (sensorPower,OUTPUT);  //rem out after TESTING COMPLETE
}


void loop(){
  digitalWrite(sensorPower,HIGH); //rem out after TESTING ONLY 
  sensorValue = analogRead (sensorPin);
  if (sensorValue < 400){    //1.96V for night .....input is 0.0049v per unit...range from 0 to 1023
    nightRun();
  }
  else if(sensorValue > 600){  //2.94V for day .... 2.94/0.0049 = 600
  dayRun();
  }

digitalWrite(dayLed,LOW);        //turns off day LED if sensor changes
digitalWrite(nightLed,LOW);      //turns off night LED if sensor changes
}


void nightRun(){
  digitalWrite(nightLed,HIGH);
  //delay(1000);                //probably rem out later
  if (sensorValue<400){
    digitalWrite(pumpPin,HIGH);
    delay(nightOn);
    digitalWrite(pumpPin,LOW);
    digitalWrite(delayLed_A,HIGH);
    delay(nightInterval);
    digitalWrite(delayLed_A,LOW);
 
    }
}



void dayRun(){
  digitalWrite(dayLed,HIGH);
  //delay (1000);              //probably rem out later
    if(sensorValue>600){
    digitalWrite(pumpPin,HIGH);
    delay(dayOn);
    digitalWrite(pumpPin,LOW);
    digitalWrite(delayLed_A,HIGH);
    delay(dayInterval);
    digitalWrite(delayLed_A,LOW);
    }
}

The “state” of the LDR has been a good suggestion I had not thought about…thanks Robin2