Author Topic: How to interrupt the 'delay' function?  (Read 7743 times)

Hants

  • Newbie
  • *
  • Posts: 14
How to interrupt the 'delay' function?
« on: April 15, 2013, 06:44:20 AM »
Hello everybody!

Im a complete newbie in electronics, however Martins videos were so interesting, that I've started tinkering a bit.

Im working on my first 'serious' project: turning assistant lights. Idea is straightforward - when the headlights are on and an indicator is on - appropriate foglight goes on for 5 seconds.

So far sketch looks like that:

int fogA = 8;
int fogB = 7;
int IndLeft = 4;
int IndRight = 2;


void setup()
{
 pinMode(fogA, OUTPUT);
 pinMode(fogB, OUTPUT);
 pinMode(IndLeft, INPUT);
 pinMode(IndRight, INPUT);
}

void loop()
{
  if (digitalRead(IndLeft) == HIGH)
  {
    digitalWrite(fogA, HIGH);
    delay(5000);
   
  }
   else
    {
     digitalWrite(fogA,LOW);
    } 
     
     if (digitalRead(IndRight) == HIGH)
  {
    digitalWrite(fogB, HIGH);
    delay(5000);
  }
   else
    {
     digitalWrite(fogB,LOW);
    }
}


but there is a problem: if I swap indicators quickly - the former one stays on for 5 seconds.
I want arduino to interrupt  digitalWrite(fogB, HIGH); delay(5000); procedure.

Any ideas?

MJLorton

  • Administrator
  • Hero Member
  • *****
  • Posts: 816
Re: How to interrupt the 'delay' function?
« Reply #1 on: April 16, 2013, 04:48:35 AM »
Hello Hants,

Welcome aboard and hats off to you for jumping in with this project. My head is not in the best place right now to assist but I'm sure someone else will provide some guidance soon.

Cheers,
Martin.
Play, discover, learn and enjoy! (and don't be scared to make mistakes along the way!)

dr_p

  • Jr. Member
  • **
  • Posts: 64
Re: How to interrupt the 'delay' function?
« Reply #2 on: April 16, 2013, 07:36:56 AM »
whell, how about replacing

Code: [Select]
delay(5000);
with

Code: [Select]
i=0;
while(not_condition_for_reverse_turning && i<100)
         {
         delay(50);
         i++;
         }

beware that the extra code causes extra delay, but it's probably an imperceptible difference

Hants

  • Newbie
  • *
  • Posts: 14
Re: How to interrupt the 'delay' function?
« Reply #3 on: April 17, 2013, 07:54:39 AM »
Dr_P - thank you very much for input. I will try this immidiately!

Martin - hope you are getting better after your op.

Hants

  • Newbie
  • *
  • Posts: 14
Re: How to interrupt the 'delay' function?
« Reply #4 on: April 17, 2013, 08:18:06 AM »
Well... It doesnt work.

Left LED stays on idefinitely  :'(


int fogA = 8;
int fogB = 7;
int IndLeft = 4;
int IndRight = 2;
int i=0;


void setup()
{
 pinMode(fogA, OUTPUT);
 pinMode(fogB, OUTPUT);
 pinMode(IndLeft, INPUT);
 pinMode(IndRight, INPUT);
}

void loop()
{
  if (digitalRead(IndLeft) == HIGH)
  {
    digitalWrite(fogA, HIGH);
    i=0;
    while(IndRight && i<100);
         {
         delay(50);
         i++;
         }
   
     
   
   
   
   
   
  }
   else
    {
     digitalWrite(fogA,LOW);
    } 
     
     if (digitalRead(IndRight) == HIGH)
  {
    digitalWrite(fogB, HIGH);
    delay(5000);
  }
   else
    {
     digitalWrite(fogB,LOW);
    }
}

dr_p

  • Jr. Member
  • **
  • Posts: 64
Re: How to interrupt the 'delay' function?
« Reply #5 on: April 17, 2013, 09:55:25 AM »
I don't know about all that logic, but this looks suspicious:

    while(IndRight && i<100);

First, you have a semicolon at the end, so the while cycle ends there, it never executes the delay cycle. At least that's how C++ works and this looks like C++.
Second, instead of IndRight==1 you have to check that digitalRead(IndRight)==1, so I think the like should look like this:

Quote
while(digitalRead(IndRight) && i<100)

Hants

  • Newbie
  • *
  • Posts: 14
Re: How to interrupt the 'delay' function?
« Reply #6 on: April 17, 2013, 10:09:43 AM »
Thank you!

Sadly - now it works as a simple switch. LED is off when I depress the button. However if I press the other switch simultanously, then the LED goes on for described period.

We are getting closer :-)
« Last Edit: April 17, 2013, 10:15:43 AM by Hants »

dr_p

  • Jr. Member
  • **
  • Posts: 64
Re: How to interrupt the 'delay' function?
« Reply #7 on: April 18, 2013, 02:53:13 AM »
Replace the pins definitions, I used indR and fogR as they seem more intuitive

Quote
while()            //main loop
      {
      i=100;                       // counter initialized with "done" value
      if(digitalRead(indR))                  
            {
            i=0;                  //if indicators were reversed, the cycle needs to be reset to full 5s
            while(digitalRead(indR) && i<100)
                  {
                  i++;
                  digitalWrite(fogR, HIGH);
                  digitalWrite(fogL, LOW);
                  delay(50);
                  }
            }
      if(digitalRead(indL))                  //same as right side
            {
            i=0;
            while(digitalRead(indL) && i<100)
                  {
                  i++;
                  digitalWrite(fogR, LOW);
                  digitalWrite(fogL, HIGH);
                  delay(50);
                  }
            }
      if(!digitalRead(indR) && !digitalRead(indL))      // no indicators currently on
            if(i==100)         //neither indicators were on OR the 5s cycle is over
                  {
                  digitalWrite(fogR, LOW);
                  digitalWrite(fogL, LOW);
                  }
            else                  //one or both indicators were on for less than 5s
                  while(!digitalRead(indR) && !digitalRead(indL) && i<100)      //but not any more
                        {
                        i++;               //finish the 5s cycle in the last foglight configuration
                        delay(50);
                        }
      }

Hants

  • Newbie
  • *
  • Posts: 14
Re: How to interrupt the 'delay' function?
« Reply #8 on: April 18, 2013, 04:36:44 AM »
Dr_P Thank you very much for your time and attention!

As we are now on the very high level of programming - well beyond my ability to understand or correct this sketch, I must relay on you and ask a final question:

What: 

              error: expected initializer before 'while'

                                                                        - means?

dr_p

  • Jr. Member
  • **
  • Posts: 64
Re: How to interrupt the 'delay' function?
« Reply #9 on: April 18, 2013, 06:58:29 AM »
What line is the error at?

I see you use "void loop()" without a way of repeating it forever. It does so by definition maybe?! I'm not sure what variety of C you are using.

Maybe try to eliminate my first while() and it's two brackets.

Also, try deleting comments, maybe they are interpreted as commands.

Again, I have no experience with Arduino so I don't know how things work there, if there is an IDE with maybe it's hybrid programming language.

If it were me, I would build an analog circuit, a simple delay timer, and connect as "inputs" the wires going to the signaling bulbs. And output would be a MOSFET to directly drive the fog light.
The "logic" is every time your signalling bulb is on, the corresponding fog light comes on for 2-3 seconds. That's it.

As far as I can understand, you have it on a breadboard, or the Arduino board, so even if your program will work flawlessly, the job is half done. You have to interface with everything and that can prove to be difficult for a beginner. You end up using a lot of analog parts, so why not go analog all the way?

I think the circuit below should work. You'll need two, of course. :) Anyone feel like approving the schematic?


Mr Eastwood

  • Sr. Member
  • ****
  • Posts: 271
Re: How to interrupt the 'delay' function?
« Reply #10 on: April 19, 2013, 11:16:13 AM »
maybe this?

Code: [Select]
bool isOn = false;
int fCount = 0;

void loop() {
if (digitalRead(IndLeft) == HIGH) {
if(!isOn) {
fCount = 0;
isOn = true;
digitalWrite(fogA, HIGH);
digitalWrite(fogB, LOW);
}
}
if (digitalRead(IndRight) == HIGH) {
if(!isOn) {
fCount = 0;
isOn = true;
digitalWrite(fogA, LOW);
digitalWrite(fogB, HIGH);
}
}
if(isOn) {
delay(100);
fCount++;
if(fCount==50) {
isOn = false;
digitalWrite(fogA, LOW);
digitalWrite(fogB, LOW);
}
}
}
« Last Edit: April 19, 2013, 11:28:44 AM by jucole »
Hey! Frisbee! Far out!

Hants

  • Newbie
  • *
  • Posts: 14
Re: How to interrupt the 'delay' function?
« Reply #11 on: April 19, 2013, 05:24:37 PM »
Jucole! Thank you!

Sadly - it doesnt work, as Im unable to break the 5 sec delay :-(

Code:
==============

int fogA = 8;
int fogB = 7;
int indL = 4;
int indR = 2;

void setup()
{
 pinMode(fogA, OUTPUT);
 pinMode(fogB, OUTPUT);
 pinMode(indL, INPUT);
 pinMode(indR, INPUT);
}

bool isOn = false;
int fCount = 0;

void loop() {
       if (digitalRead(indL) == HIGH) {
          if(!isOn) {
             fCount = 0;
             isOn = true;
             digitalWrite(fogA, HIGH);
             digitalWrite(fogB, LOW);
          }
       }   
       if (digitalRead(indR) == HIGH) {
          if(!isOn) {
             fCount = 0;
             isOn = true;
             digitalWrite(fogA, LOW);
             digitalWrite(fogB, HIGH);
          }
       }
       if(isOn) {
          delay(100);
          fCount++;
          if(fCount==50) {
             isOn = false;
             digitalWrite(fogA, LOW);
             digitalWrite(fogB, LOW);
          }
       }
}

============================




Dr_P thank you for drawings! It is worth consideration, however whole point of doing it, is to have a bit of fun with Arduino. Maybe in the future - simple add on of the tilt sensor?   Certainly - I should consider transistors for a fade effect.

There are whole sets avaible on the market (http://www.realtruck.com/hella-dyna-view-evo-2-intelligent-driving-lights/images/) but that would be unambitious...

Hants

  • Newbie
  • *
  • Posts: 14
Re: How to interrupt the 'delay' function?
« Reply #12 on: April 19, 2013, 08:36:49 PM »
DONE!!   ;D

/* Light turning assistant for cars (cornering lights)
   Credits to user Dr_P at http://mjlorton.com forum
   
   (version without tilt sensor)
*/


int fogL = 8;                               // 55W Relay for L fog lamp
int fogR = 7;                               // 55W Relay for R fog lamp
int indL = 4;                               // Circuit between multiswitch and indicator relay
int indR = 2;                               // Circuit between multiswitch and indicator relay



void setup()
{
 pinMode(fogL, OUTPUT);
 pinMode(fogR, OUTPUT);
 pinMode(indL, INPUT);
 pinMode(indR, INPUT);
}

void loop()

{

      {
     int i=100;                                    // counter initialized with "done" value
     if(digitalRead(indR))                 
            {
            i=0;                                   //if indicators were reversed, the cycle needs to be reset to full 5s
            while(digitalRead(indR) && i<100)
                  {
                  i++;
                  digitalWrite(fogR, HIGH);
                  digitalWrite(fogL, LOW);
                  delay(50);
                  }
            }
      if(digitalRead(indL))                        //same as right side
            {
            i=0;
            while(digitalRead(indL) && i<100)
                  {
                  i++;
                  digitalWrite(fogR, LOW);
                  digitalWrite(fogL, HIGH);
                  delay(50);
                  }
            }
      if(!digitalRead(indR) && !digitalRead(indL))      // no indicators currently on
            if(i==100)                                  //neither indicators were on OR the 5s cycle is over
                  {
                  digitalWrite(fogR, LOW);
                  digitalWrite(fogL, LOW);
                  }
            else                                        //one or both indicators were on for less than 5s
                  while(!digitalRead(indR) && !digitalRead(indL) && i<100)      //but not any more
                        {
                        i++;                           //finish the 5s cycle in the last foglight configuration
                        delay(50);
                        }
      }
}

Now it works as intended!
Next step  - adding the tilt sensor :-)

dr_p

  • Jr. Member
  • **
  • Posts: 64
Re: How to interrupt the 'delay' function?
« Reply #13 on: April 20, 2013, 06:26:16 PM »
So I was right, it's repeating by itself, so no need for the "while()".

Minor suggestion: after "void loop()" you have two open brackets, and at the end you have two closed brackets. One of them is not needed. It's not technically wrong, but it's good to be tidy.

What's the tilt sensor for ?

Hants

  • Newbie
  • *
  • Posts: 14
Re: How to interrupt the 'delay' function?
« Reply #14 on: April 20, 2013, 09:51:43 PM »
Dr_P
Indeed - I have removed one pair of the curly brackets and sketch is still working. :-)

Tilt sensor idea was born during my driving in the mountains. Similar functionality as completed sketch above, but without using indicators - cornering lights switch on automatically during driving. But it is a distant future.

First I need to finish this project. Lamps,brackets, wires, project box, power supply and arduino nano has been ordered.

The only one thing I would like to implement in ver.1 is a gentle dimming effect. But there is no way to use PWM on relays, so I need to find out a more traditional way of sorting that.