I am trying to make a servo move based on its current position

Here is the code:

#include<Servo.h>

Servo servo1;

void setup() {
servo1.attach(9); // signal wire

if (servo1=90) {
servo1.write(0)
}

if (servo1=0) {
servo1.write(90)
}

void loop() {

}

What does the code do and what do you expect it to do?

By running the code in setup the code is only ran once.

A servo needs time to get there. The servo needs more time for distance traveled. There should be some sort of delay, after each servo movement to allow the servo to reach its position before sending the servo to another position.

Have you looked at the info on the Arduino servo library? https://www.arduino.cc/en/Reference/Servo. You might find functions like read() to be useful in determining where the servos current position.

Servo servo1;

void setup() {
  servo1.attach(9); // signal wire

if (servo1=90) {

You can’t assign a numeric value to a Servo object, and it makes no sense to compare a Servo object to 90, either.

You can’t read the current position of a servo, either. You can read where you last told the servo to go to, but you have no idea whether it has gotten there yet, or whether it can get there at all.

You’ll need some other approach, like limit switches.

Or a feedback servo.

-jim lee

russiantechsupport:
Here is the code:

#include<Servo.h>

Servo servo1;

void setup() {
servo1.attach(9); // signal wire

if (servo1=90) {
servo1.write(0)
}

if (servo1=0) {
servo1.write(90)
}

You might want to look at this example:

https://www.instructables.com/id/Arduino-Servo-Motors/

void loop() {

}