Inside the main loop of sketch I had If-else condition , if the condition is true , the compiler should executes what is in the if-body . but I need to add some thing else which is : if for any loop the condition was true and the compiler executed what is in the if-body , I want the compiler to execute what is in the if-body for next loop even when the condition is false . I wish the question is clear .
There is no such thing.
There is a "while" loop, a "do..while" loop and a "for" loop (which is just a "while" loop in a fancy frock), and the "goto", which we keep chained-up in the cellar.
There is no such thing.
There is a "while" loop, a "do..while" loop and a "for" loop (which is just a "while" loop in a fancy frock), and the "goto", which we keep chained-up in the cellar.
if (condition || wasTrue)
{dostuff();
wasTrue=true;
}
thanks a lot
But in the code above it will do that just for the first time that condition is true . I need it for any time the condition became true it should enter condition in next loop even when the condition is false .
But in the code above it will do that just for the first time that condition is true
Look carefully I've used the OR function. The first time the condition becomes true wasTrue gets set. From then on EVERY time through the loop it will keep doing it, until you do something to reset wasTrue back to false.
To avoid future confusion the compiler is a program on your PC that converts the text of your code into machine code that the Arduino can understand. The compiler doesn't play any role when the code is actually running and making decisions based on your IF statements. So it is technically incorrect to say "the compiler should executes what is in the if-body" A more correct sentence would be "the Arduino should execute what is in the if-body"
boolean a = true;
void loop() {
if( a )
{
a = !a; // complement
//Serial.print( " True ");
Serial.println (a); // process
}
else
{
//Serial.print( " False ");
Serial.println (a); // process
}
delay(2000);
}
Moderator edit: code tags added. Vaclav, for future reference, it's the control marked with a # on the post editor's toolbar, or you can add them manually like this put your code here
I just wrote a simple example show what I need to do
void setup() {
int X=0
}
void loop() {
if (ConditionIsTrue)
{
X=X+1;
Serial.println("X");
}
}
Loop 1 : ConditionIsTrue is true (for example if it is true )
X=1;
Loop 2 : ConditionIsTrue if true or false it should exexute X=X+1 and Serial.println("X");
X=2;
Loop 3 : ConditionIsTrue is False
X=2;
Loop 4 : ConditionIsTrue is False
X=2;
Loop 5 : ConditionIsTrue is False
X=2;
Loop 6 : ConditionIsTrue is true
X=3;
Loop 7 : ConditionIsTrue if true or false it should exexute X=X+1 and Serial.println("X");
X=4;
Loop 8 : ConditionIsTrue is false
X=4;
Loop 9 : ConditionIsTrue is false
X=4;
.
.
.
.
.
.
I just wrote a simple example show what I need to do
That is not an explanation of WHY you want to do that. A concrete, real-life story is needed. What you are doing is bizarre. The WHY behind it is essential.
I just wrote a simple example show what I need to do
That is not an explanation of WHY you want to do that. A concrete, real-life story is needed. What you are doing is bizarre. The WHY behind it is essential.
thanks
It is a very big code with many classes and libraries I couldn't post it , in the code I have a function that dependent on sensor reading , if the sensor senses any thing , the function should call , after that I want to call the function in next time (next loop) even when sensor didn't sense , shortly, Each call to the function after verification condition , it should follow with another call .
But in the code you post, it will do that just for the first time that condition is true and if I reset wasTrue to false it will cancel the true that set inside if-body .
for example :
bool wasTrue=false;
int X=0;
void loop()
{
if (condition || wasTrue)
{
X=X=+1;
wasTrue=true;
}
wasTrue=false;
Loop 1 : if condition is true
X=1;
wasTrue=true;
Loop 2 :
wasTrue=false
.
.
.
.