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.
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.
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
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,
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
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?