I want to make if condition with 2 conditions one of them will be a time condition ; ex:
if the 1st condition is right and the time elapsed is 5000 ms then do something
(excute the if statement )
how can i make this
You can "and" the conditions in one if:
if ( condition1 AND condition2)
{
// do things
}
Or you can nest 2 statements:
if (condition1)
{
if (condition2)
{
// do things
}
}
Do you know how to get the elapsed time with millis()? ...Basically you "capture" the starting millis() time as a variable, then when you want to check, read millis() again and subtract. (millis() uses type unsigned long.)
You can include logical and (&&) in your if statement...
If (A && B && CC) and if you want you can include the subtraction & comparison to inside the if-statement, and you can even read current millis() time inside the if-statement. Or you can do some of that stuff right-before the if-statement if that's easier for you.
It's also possible to make nested if-statements so the second one gets skipped-over if it's not time yet.
(Normally it's best to keep it all on one line if you can, and if it's not too confusing... to humans.
)
This tutorial illustrates how to use millis() to perform a timed operation:
IMHO this tutorial is pretty hard to understand. At least harder than this one:
best regards Stefan
Thanks @StefanL38. I like that. I will start recommending that tutorial instead.
do u thing adding millis function? i always use it.
millis işlevi ekleyen bir şey var mı? her zaman kullanırım.
this is the code i want but id don't know how to start counting time after a certain condition is true
if (button1==HIGH && button2==HIGH)
{
// start counting time
}
if (// counted time == 5000 )
{
digitalWrite(led,high);
}
this is the code i want but id don't know how to start counting time after a certain condition is true
if (button1==HIGH && button2==HIGH)
{
// start counting time
}
if (// counted time == 5000 )
{
digitalWrite(led,high);
}
[/quote]
Hello sarah_20
Post your current sketch, well formated, with comments and in so called code tags "</>" to see how we can help.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
Try something like this...
disclaimer... 100% untested ![]()
//right at the top above setup
bool weAreTiming = false;
unsigned long startTime;
if (button1==HIGH && button2==HIGH) // HIGH is pressed?
{
if (!weAreTiming) //! means "not" so read as "if we are not timing"
{
startTime = millis(); //start the clock
weAreTiming = true; // but now we ARE timing so don't reset the timer next time round
}
}
if (weAreTiming && millis()-startTime >= 5000 ) //note >= rather than == which might miss the exact slot
{
digitalWrite(led,HIGH); //note HIGH not high as you had
}
if (button1==LOW || button2==LOW) // || is "or" ... either or both buttons are now released
weAreTiming=false; // ready for the next time both are seen to be pressed
The trick is capturing the time of the event that starts the timer. One possibility is:
if (! firstCondition) {
lastNotFirstConditionTime = millis();
}
...
if (firstCondition && millis() - lastNotFirstCondtionTime >= 5000){
doSomething();
}
Another is to set a flag to monitor something:
bool isHappening = false;
...
if(!firstCondition){
isHappening = false;
}
if (!isHappening && firstCondition){
isHappening = true;
tStart = millis();
}
if (isHappening && millis() - tStart) {
doSomething();
...
}
LOL.
Sry, this variable name just does. Make me. LOL.
a7
Aw, I worked hard at that. ;)