Need help in Loop cycle

Hi,
Need to loop cycle, for now its working only 1 time:

#include <Servo.h>

Servo myservo;
int val;
int Vah;

void setup()
{
Serial.begin(9600);
myservo.attach(2); //Pin where Servo motor signal is connected.
Vah = 0;
}

void loop()
{
val = analogRead(7); //Pin where LDR is connected.
Serial.println(val);
myservo.write(90);

if (val > 1000 && Vah == 0)
{
myservo.write(45);
Vah = 1;
delay(1000);
do
{
delay(2000);
val = analogRead(7);
Serial.println(val);
}
while (val > 1000);
}

if (val > 1000 && Vah == 1)
{
myservo.write(0);
Vah = 3;
delay(1000);
}
}

while (true) {...} - not working
for (;:wink: {...} - not working

SOLUTION ! :
Found Solution, it was very simple command, just add Vah = 0; after Vah=3;
Arduino nano was used

What is "cycle"? :wink:

It's already zero before the setup function executes.

have a read of how-to-get-the-best-out-of-this-forum
in particular

  1. what arduino are you using?
  2. used code tages </> when uploading code
  3. post a schematic or your wiring
  4. upload a copy of the serial monitor text output

Yes, because you wrote it such way that it runs only once.

Look to the code - first condition match only if Vah == 0 , then you change Vah to 1.
The second condition will run if Vah == 1 and then you change the variable to 3.
That's all. The Vah value never will 0 or 1 again and your loops never run.

what are you trying to do?

      val = analogRead(7);

can IO-pin 7 be used as analog input?

This wokwi-simulation makes visible what your code is doing

best regards Stefan

No, but pin A7 (aka 7) on a Nano or Mega can.
The OP didn't tell us which processor they're using.
Let's wait until we find out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.