Arduino > Arduino

How to interrupt the 'delay' function?

(1/3) > >>

Hants:
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:
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.

dr_p:
whell, how about replacing


--- Code: ---delay(5000);
--- End code ---

with


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

--- End code ---

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

Hants:
Dr_P - thank you very much for input. I will try this immidiately!

Martin - hope you are getting better after your op.

Hants:
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);
    }
}

Navigation

[0] Message Index

[#] Next page

Go to full version