sensor does not trigger during the delay time.

I have a program that lights up an LED for a certain second so i have let's say a lot of delays in my code. Most of them are around 5-6 second delays. Now, i want to activate my code for my bump sensor so that when it is activated, it lights up the other set of LEDs. The code for the sensor works however, my problem is it would only work if you hold on pressing the sensor switch for a long time because it let the delays to finish up (about 5 seconds) before it realizes that the sensor is activated. Due to this, on random sensor activation, it might not trigger. This is an extract of my code:

void loop() { 
  int Out = digitalRead(2);
  int value; 
  if (Out == 1) {
    LED1();
  } 
  
 if (Out == 0) {
    LED2();
  }

  delay(5000);      
  LED1();
 
}
void LED1() {
  int Out = digitalRead(2);
  digitalWrite(M1, LOW);
  digitalWrite(M2, HIGH);
  analogWrite(E1, 255);
  analogWrite(E2, 255);
  delay(time);
  time += delaytime;

i have several variables that are not stated here bu you get my point. If out is 1 means its not activated. So initially, LED1() is executing however when i click the sensor (just a click) , it doesnt work because it's still executing all the commands in LED1() so in order for LED2() to work, i have to hold the sensor for a long time. LED1 and LED2 have several delays which is not stated on the code.

You need to read and play with the blink without delay tutorial.
But above all, you need to understand it.

i have no idea how am i going to implement that on mine. the blinkwithoutdelay is a bit complicated already for just a simple blinking command. I'm not quite sure how to do that on my code because my LED patterns are very complex they have diff delays each time and they increase too, so i cant just use those previousmillis - currentmillis or whatsoever. And I'm not that smart to make a very reasonable algorithm

the blinkwithoutdelay is a bit complicated already for just a simple blinking command

I counted yesterday.
It has precisely seven functional expressions (lines of code)
Three of those could easily be taken out.

It is complicated for a blinking command, but the underlying principle is powerful and extendable, which is why you should take time to understand it.

my LED patterns are very complex they have diff delays each time and they increase too, so i cant just use those previousmillis - currentmillis or whatsoever

That is a false assumption.

Whatever you do, you can't use delay(), it effectively kills everything whilst its within the delay period. Its just a matter of using millis() within the main loop and keeping variables of the same unsigned long type to keep track of what you what doing when then a few 'if then' constructs.

Think, look at your watch, see if its time to take action, if not go and do something else for a while and then look at your watch again.

delay is like setting your alarm and going to sleep.......

Your own version of blinkWithoutDelay is the best way to go. But until you learn the usage of it, try using an interrupt. Like....

attachInterrupt (1, breakout, RISING);       ///// Add this some where in your code, you will have to decide depending on your code

void breakout(){           //////////  Add this outside of the main loop
Out = 0;
}

When a button is pressed on pin 3, it set out to 0. So depending on where you put it(the attachInterupt), and if you redefine Out to soon(if you change the value of Out again before you use it), it should work. It looks like you have quite a bit of cleaning up to do just from what you posted. Cant really help without all the code.

http://arduino.cc/en/Reference/AttachInterrupt

@tnovak:
And you've tested that code, have you?

@AWOL

I do not know what i was thinking when i woke up this morning.... I have changed it a little, and yes tested it now. Works great in my code, and other projects I have used it in. Dont know how it will work for him, he will just have to try and see.

I don't mean for him to use what i posted, but take the idea and make it work for him.

So depending on where you put it, and if you redefine Out to soon, it should work

Could you try Google translation on that sentence, please?

Can you also explain how this allows the OP to break out of their delays?

One way off the top of my head is to split up the delays. This is just an fast exapmle.

while (thisdelay < 500 && Out != 0){
delay(10);
thisdelay++;
}

This would delay for 5 seconds but check if Out has been changed every 10 millis

I added pointers to the line you wanted translated, and these are just ideas not a complete answer

pluggy:
...
Think, look at your watch, see if its time to take action, if not go and do something else for a while and then look at your watch again.

delay is like setting your alarm and going to sleep.......
...

I love it, Pluggy, and I'm gonna steal it for my site. (It was an Open Source comment, I hope..) :slight_smile:

tnovak, your idea may improve responsiveness, but still leaves massive amounts of dead processor time.
Blink without delay, every time.

@Groove

I do agree a BlinkWithoutDelay approach is the best. But im thinking back to when i first starting coding. I wouldn't have a clue how to change BWD into something could use. So i would tried other approaches that i could understand to get it todo what i want. Then refine it more and more until HUH i think i can do this "difficult" coding after all.

I was just putting another option out there, so people could learn step by step instead of trying to Run(blinkwithoutdely) before learning how to Walk(the basics of programming). Or maybe even learn how to Jump(attachInterrupt) just because. Even though my goal isn't to Jump, something i learn while Jumping might enable me to learn how to Run.

I was simply trying not to just say, "its easy, just learn BlinkWithoutDelay".

What we need is a really good example of the "blink without delay" approach in which LOOP has sections that check the time and act accordingly.

I wonder if anyone has such an example that we could organize for the Newbies.

These issues are not at all obvious to beginners, or even to people who have written significant Arduino applications without encountering the problems.

It's easy for those of us who have dealt with this for years to grumble about naivety of newbies, but we really ought to do something meaningful to set them on the right path.

Any ideas on how to do this??

Terry King,

You pretty much said what i was trying to say. Thank You.

Also Ive noticed a lot of the examples we refer people to use, could be cleaned up and "streamlined" themselves. BWD is one of them.

I think BWD is just intimidating to most people because of the length of code when you glance at it.

if(millis() - previousMillis > interval) {
    previousMillis = millis();
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
}

Take out the obvious stuff and its not nearly as intimidating. (For the critics, Yes I do realize previousMillis() might not be the same as if you were to use a variable to set it)

I love it, Pluggy, and I'm gonna steal it for my site. (It was an Open Source comment, I hope..) smiley

I can't claim it to even be original, I think I lifted it from Grumpy Mike or one of the others I hold in high regard.........

Here's my simpler to understand 'blink without delay' (or at least I think it is)

/*
 
 Stephen's Blink
 
 */

int ledPin =  13; // LED connected to digital pin 13
unsigned long time;
unsigned long secloop = 1000; // 1 second delay
boolean liteon = false;


// The setup() method runs once, when the sketch starts

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);     
  Serial.begin(115200);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                     
{ 
  time = millis();  // looks at watch.....
  
  if (time >= secloop){   // decides if its time to do something or not
    secloop = time + 1000;  // increments next check by 1 second
    if (liteon){ 
      digitalWrite(ledPin, HIGH);  
      liteon = false;
    }   //  turn it on and mark it for off next time
    else
    {
      digitalWrite(ledPin, LOW); 
      liteon = true;
    };  //  turn it off and mark it for on next time
    
  };
}

Thanks for all suggestions. I have managed to do that without using delay. A very long code though.