hello everyone
I have a very simple project, but I am unable to do something in code .. I want to stop the { for loop } if the condition is not met and go back to the { main loop }.. Can anyone help me with that?
int value=0;
byte Sensor=A1;
byte LED1=3;
byte LED2=5;
byte LED3=6;
void setup() {
Serial.begin(9600);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop()
{
value=analogRead(Sensor);
Serial.println("Sensor value is :");
Serial.println(value);
if ((value)>120 && (value)<150)
{
for(int BL=0;BL<10;BL++)
{
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
delay(500);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,HIGH);
delay(500);
//From this point, if the value is still between 120 to 150, complete the code.. If it is not, go back to the main loop.
if ((value)>120 && (value)<150)
{
for(int BL=0;BL<30;BL++)
{
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
for(int ledVal = 0; ledVal <= 100; ledVal +=1) {// Fade in
analogWrite(LED1, ledVal);
delay(15);
}
for(int ledVal = 100; ledVal >= 0; ledVal -=1) { // Fade out
analogWrite(LED1, ledVal);
delay(30);
}
delay(750); // Pause
}
}
}
}
}
You seem to have a lot of experience in the programming world to the point that you could see my bracket variables.. how about you help me solve the problem in my code
My question is that I don't want this {for loop} to work in all cases unless the value is still between 120 and 150..
That is, if the value is between 120 to 150, the first {for loop} is played directly for 10 seconds, and then if the value is still between 120 to 150, the second {for loop} is played, and if the value is not between 120 and 150, return to the main loop
I did that before.. The problem is that the second {for loop} works even if the value is different after the first.. because it depends on the value of the first..
I will give an example
if value = 0 to 120 or value = > 150
nothing happens .
if value = between 120 and 150
do this
for(int BL=0;BL<10;BL++)
It will take 10 seconds
Then
if value still = 120 to 150
do this
for(int BL=0;BL<30;BL++)
If the value changes and becomes not equal between 120 to 150 don't complete.
That's all i want.
I don't think so. Your code does not match the description of what you wanted to do.
Looks like you messed up the parenthesis.
In particular, you got a 30 cycles loop inside a loop of 10 cycles - that is, in fact, you got not 10 + 30 cycle passes, but 10 * 30 = 300 passes
The code Works perfect.. Cycle 30 will not work unless the condition XY>= 20 is met with the condition that the value is between 120 and 150.. And this is what I was trying to achieve..
XY = 10
after while loop
*XY=20
*if value between 120 and 150
Both conditions have been fulfilled
do Cycle 30
if value not between 120 and 150
evrything will stop
Because the condition for a while loop also requires the value 120 to 150