Need help with my code ASAP

Hello guys, I'm trying to program a water sensor to read water level and output it to a servo. If there is water detected, the servo should turn 90deg and back with 1 sec delay. Unfortunately, when i run this code, the servo turns randomly. Can you guys spot any errors in the code? this is the code

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

else{
  servo.write
  }

Oops.

Why don't you post the code you actually compiled?
In code tags, of course.

Original code

#include <Servo.h>
Servo servo;
int angle = 0;
int watersensor = 8;

void setup() {
  servo.attach(9);
  servo.write(angle);
  pinMode(watersensor, INPUT);

}
void loop() {
  if(digitalRead(watersensor) == HIGH) {
  for(angle = 0; angle < 90; angle++)  
  {                                  
    servo.write(angle);               
    delay(1000); 
    
     for(angle = 90; angle > 0; angle--)    
  {                                
    servo.write(angle);           
    delay(1000);       
  }
  }   
  }
  else{
  servo.write
  }
}

That nesting will have to go.

The second for is inside the first for, causing the servo to move from 90 to 1 degrees after each angle of movement from 0 to 89.

..but sharing the same loop control variable

sorry, i posted the link?

#include <Servo.h>
Servo servo;
int angle = 0;
int watersensor = 8;

void setup() {
  servo.attach(9);
  servo.write(angle);
  pinMode(watersensor, INPUT);

}
void loop() {
  if(digitalRead(watersensor) == HIGH) {
  for(angle = 0; angle < 90; angle++)  
  {                                  
    servo.write(angle);               
    delay(1000); 
    
     for(angle = 90; angle > 0; angle--)    
  {                                
    servo.write(angle);           
    delay(1000);       
  }
  }   
  }
  else{
  servo.write(angle);
  }
}

heres the code if it is more convenient than the link

  {
    for (angle = 0; angle < 90; angle++)
    {
      servo.write(angle);
      delay(1000);
      for (angle = 90; angle > 0; angle--)
      {
        servo.write(angle);
        delay(1000);
      }
    }
  }

As has been pointed out, you are using the same global variable for 2 nested for loops

...and even if they weren't nested, that's an awfully slow movement.

Hi,
You do not need to nest the for loops.
You need to run the servo 0 to 90 FIRST.
Then wait.
Then run the servo 90 to 0

Also you need to delay between each degree step to give the servo time to respond.

Try this edit of your code.

#include <Servo.h>
Servo servo;
int angle = 0;
int watersensor = 8;
int stepdelay= 10;  //step every 10ms
void setup() {
  servo.attach(9);
  servo.write(angle);
  pinMode(watersensor, INPUT);

}
void loop() {
  if (digitalRead(watersensor) == HIGH)
  {
    for (angle = 0; angle < 90; angle++)  //drive servo from 0 to 90
    {
      servo.write(angle);
      delay(stepdelay);
    }
    delay(1000);   // wait 1000ms
    for (angle = 90; angle > 0; angle--) //drive servo from 90 to 0
    {
      servo.write(angle);
      delay(stepdelay);
    }
  }
  else
  {
    servo.write(angle);
  }
}

You can change the variable "stepdelay" to tune the speed of the servo,
It is untried so you may need to increase "stepdelay" to 20 or 40.

Tom.... :smiley: :+1: :coffee: :australia:

When the link points to code that has no hope of compiling, much less running, posting the actual code you are describing is always preferable.

thank ye kindly, it works like a charm now

Hi,
Do you understand how the code works?

Tom... :smiley: :+1: :coffee: :australia: