avoid delay() please help me

hello i would like create a time delay and i create this code

    int proximity= 22;
    int relay = 13;   
    int val_proximity = 0;
 int start = millis();
 int flag=0;
 
        void setup()
    {   
      Serial.begin(9600);
      pinMode(proximity, INPUT);   
          pinMode(relay, OUTPUT);     
        }                       
    void loop()
    {                     
          val_proximity = digitalRead(proximity);   
       if (val_proximity== LOW)             
          {                 
            digitalWrite(relay,HIGH);
           flag++;
              
          }   
    if (val_proximity == HIGH && flag>0)
    {
       while(millis()-start<3000)
       {
          
          if (val_proximity== LOW)
            digitalWrite(relay,HIGH);
            
       }
       digitalWrite(relay,LOW);
       
      
    }
    if (val_proximity== HIGH && flag==0 )
    {
      digitalWrite(relay,LOW);
    }
    }

but i would like put three times for three proximity and three relays.

please tell me what must to do for this?

(proximity ON when is LOW and OFF when is HIGH)

One obvious error:

  if (val_proximity == HIGH && flag>0)
  {
    while(millis()-start<3000)
    {
      if (val_proximity== LOW)   // You've already fond val_proximity to be HIGH so this will never be true.
        digitalWrite(relay,HIGH);
    }
    digitalWrite(relay,LOW);
  }

Another note: once 'flag++' happens there is nothing to set flag back to 0 so it will just keep going up.

Tip #1 besides 'fix that flag!'.

In the IDE if you use Tools->Auto Format then your code would look like this:

int proximity= 22;
int relay = 13;   
int val_proximity = 0;
int start = millis();
int flag=0;

void setup()
{   
  Serial.begin(9600);
  pinMode(proximity, INPUT);   
  pinMode(relay, OUTPUT);     
}                       
void loop()
{                     
  val_proximity = digitalRead(proximity);   
  if (val_proximity== LOW)             
  {                 
    digitalWrite(relay,HIGH);
    flag++;

  }   
  if (val_proximity == HIGH && flag>0)
  {
    while(millis()-start<3000)
    {

      if (val_proximity== LOW)
        digitalWrite(relay,HIGH);

    }
    digitalWrite(relay,LOW);


  }
  if (val_proximity== HIGH && flag==0 )
  {
    digitalWrite(relay,LOW);
  }
}

but i would like put three times for three proximity and three relays.

please tell me what must to do for this?

Do much like you did but make these below into arrays.

int proximity= 22;
int relay = 13;   
int start = millis();

As to exactly how, I don't know exactly how you mean make them for 3. 3 sensors? If 3 sensors then why not each time through loop() you check and process a different one? Very little code change then.

i have this code

int proximity= 8;
int relay = 13;   
int val = 0;

void setup()
{   
  Serial.begin(9600);
  pinMode(proximity, INPUT);   
  pinMode(relay, OUTPUT);     
}                       
void loop()
{                     
  val = digitalRead(proximity);   
  if (val == LOW)             
  {                 
    digitalWrite(relay,HIGH);
    //???????? ? ????????????
  }   
  if (val == HIGH)
  {
    nonblocking_delay(5000);
    digitalWrite (relay,LOW);
  }               
}
int nonblocking_delay(int delayms)
{
  int start = millis();
  while(millis()-start<delayms)
  {
    val = digitalRead(proximity);
    if (val == LOW)
      return 1;
  }
  return 0;
}

but i woulk like this code for three sensor and three relays.

ex. one proximity on--> open relay-->proximity off->delay(5000) with open relay--> after 5sec relay off
second proximity2 on--> open relay2-->proximity2 off->delay(5000) with open relay2--> after 5sec relay2 off
third proximity3 on--> open relay3-->proximity3 off->delay(5000) with open relay3--> after 5sec relay3 off

i create a car wash (project for my school ) and i have pump water1 pump water2 blower

hello i have a problem, i would like create a time delay,
i have a sensor and a relay and i would like if sensor is high -->relay high-->if sensor is low -->relay low but if sensor is high and after is low the relay turn off after the 5 second. please help me

Have you looked at the 'blink without delay' example sketch? When you have, work out how to achieve what you want to do, using what you learned.

If you try it, and still need help, come back and ask.

yes i see but this code is not use for this

If you don't want to spend the effort to learn how to use the method shown in the blink without delay example, just stick a stupid delay() call in the code.

But don't complain that it becomes unresponsive, then,

sotisanis:
yes i see but this code is not use for this

Actually, it is exactly for this, but it takes longer than seven minutes to work it out.

i create this code

int proximity= 8;
    int relay = 13;   
    int val = 0;

        void setup()
    {   
      Serial.begin(9600);
      pinMode(proximity, INPUT);   
          pinMode(relay, OUTPUT);     
        }                       
    void loop()
    {                     
          val = digitalRead(proximity);   
       if (val == LOW)             
          {                 
            digitalWrite(relay,HIGH);
                         }   
    if (val == HIGH)
    {
      nonblocking_delay(5000);
      digitalWrite (relay,LOW);
    }               
    }
    int nonblocking_delay(int delayms)
    {
    int start = millis();
    while(millis()-start<delayms)
       {
          val = digitalRead(proximity);
          if (val == LOW)
             return 1;
       }
    return 0;
    }

but i would like i use this code for three diferrent relay and three diferrent proximity.

      nonblocking_delay(5000);

So, why does nonblocking_delay() return a value?

Have you rigged up a servo and some Arduino code that randomly indents for you?

Use the Tools + Auto format option before posting code, so you look a little more professional.

http://arduino.cc/forum/index.php/topic,76140.0.html

I thought that interesting coding style looked familiar - avoid delay() please help me - #4 by system - Programming Questions - Arduino Forum

Thanks for that dxw00d, to avoid further time wasting, I've merged the topics.

    int nonblocking_delay(int delayms)
    {
    int start = millis();
    while(millis()-start<delayms)
       {
          val = digitalRead(proximity);
          if (val == LOW)
             return 1;
       }
    return 0;
    }

Good for what you want but you got the function name wrong. It still blocks everything but LOW proximity.

i create the code and its ok look the code

int proximity= 2;
int proximity2= 3;
int relay = 13;   
int relay2 = 4; 
int val_proximity2 = 0;
int val_proximity = 0;
boolean flag=false;
boolean flag2=false;
unsigned long Timer1=0 ;
unsigned long Timer2=0 ;


void setup()
{   
  Serial.begin(9600);
  pinMode(proximity, INPUT); 
  pinMode(proximity2, INPUT);  
  pinMode(relay, OUTPUT); 
  pinMode(relay2, OUTPUT); 


}                       
void loop()
{       

  val_proximity = digitalRead(proximity);  
  val_proximity2 = digitalRead(proximity2); 
  ///////first sensor and relay/////////
  if(val_proximity==HIGH)
  {
    flag=true;
    digitalWrite(relay,HIGH);
    Serial.print(flag);
  }
  if(val_proximity==LOW && flag==true)
  {
    Timer1= millis();
    while (flag2=true && millis()-Timer1 < 5000)
    {
      digitalWrite(relay,HIGH);

    }
    digitalWrite(relay,LOW);
    flag=false;
  }
  /////////////////second sensor and relay2///////
  if(val_proximity2==HIGH)
  {
    flag2=true;
    digitalWrite(relay2,HIGH);
    Serial.print(flag2);
  }
  if(val_proximity2==LOW && flag2==true)
  {
    Timer2= millis();
    while (flag2=true && millis()-Timer2 < 5000)
    {
      digitalWrite(relay2,HIGH);

    }
    digitalWrite(relay2,LOW);
    flag2=false;
  }

}

but i would like to work the two different millis() in same time

flag=true is always true.
You need a comparison, not an assignment.

Go back and work through blink without delay until you understand it.
The code you posted just reimplements delay().

i use this code with two led and two button and it work very good, please tell me, how i use two different millis() in the same time.
because must completed the fisrt millis() and after i use the second millis or the opposite

There is only one millis(), but you can compare it to as many different interval counters as you need.

millis() only returns the current time.

unsigned long count = 0UL;
unsigned long startTimer;
unsigned long endTimer;
unsigned long duration;

startTimer = millis();
while ( count < 1000000 ) 
{
}
endTimer = millis(); 
duration = endTimer - startTimer;

You can set variables to time at the moment, as many as you want. You can do what you want with them. You can add a value to time set from millis() and watch for millis() to become more, then you know a certain time has passed since the time had been set. You can set up as many of those as you want and -none- of them makes any of the others invalid.

I look at the clock and know in 2 minutes I should do one thing and in 3 minutes another. I might even write down what time the clock will show in 2 minutes and write down what time the clock will show in 3 minutes. Whatever else I do, I check the clock when I get the chance and when it is the same or past a time written down I will do the thing for that time.
That is all there is about it. Being able to do possibly long-running tasks while watching the clock to catch other tasks instead of this process:
start task 1 - do nothing but wait for it to finish - start task 2 - wait - etc, etc.

Better to start tasks as ready and work out how to interleave the handling. Can you read a book or watch TV when cooking? What happens when you get focused on TV too long while cooking? Perhaps you set an alarm timer to not miss when to turn the stove off, it is the same as millis() + interval. So how can you watch TV and cook two things at once? Is it really so hard?