servo arm responding erratically to if statement write.pos

Hello,essentially I want my servo to go to position 0 if my potentiometer is reading above 200(analog reading) and 180 if it is reading above but instead it just jerks about 4 degrees back and forth when below 200. the potentiometer is connected to the breadboard and the servo is connected to ground and 5 volts along the same strip. The sweeper is connected to A0 and the signal wire of the servo to pin 8. I’m new to Arduino and am quite clueless as to how to proceed or as to whether this is a coding or wiring problem.

Here is my code.

#include <Servo.h> 
 
Servo myservo;   
 
int pos=0 ;    


void setup() {
  
  Serial.begin(9600);
  
  myservo.attach(8);
}


void loop() {
 
  int sensorValue = analogRead(A0);
  
  Serial.println(sensorValue);
  delay(100);       

if (sensorValue<200)
myservo.write(pos=180);
delay (15);

if (sensorValue>200);
myservo.write(pos=0);
delay (15);
}

thanks very much

myservo.write(pos=180); should be myservo.write(180);

dito pos 0

Mark

ok thank you very much I based my assumption regarding the inclusion of pos on the sweep example.

cheers

I am a digital media student at aucb in bounremouth , england.

void loop() {
 
  int sensorValue = analogRead(A0);
  
  Serial.println(sensorValue);
  delay(100);       

if (sensorValue<200)
myservo.write(pos=180);
delay (15);

if (sensorValue>200);
myservo.write(pos=0);
delay (15);
}

This is riddled with bugs. The one that is doing the most damage is the spurious semicolon here:

if (sensorValue>200);

The semicolon is introducing an empty statement after the if clause, which means the following line is NOT controlled by the if clause, so your code is always executing myservo.write(pos=0); regardless of the value of sensorValue.

Your use of pos=0, pos=180 in the calls to myservo.write() is wrong (just delete the pos= part and leave the number) but harmless in this case.

I suggest you get into the habit of following each if,else, for, while clause with a pair of braces { and } (on separate lines, please) and put any statements that you want controlled by those clauses inside the braces. Also, indent the matching pairs of braces by the same amount and indent the code between them one extra level. As it is, there’s no way to tell whether you intend the delay(15) statements to be conditional - they aren’t, but I have no idea whether you intend them to be.