Hello, do you know how to solve it with the code and the elements of electronics?

Program a servomotor to sweep from 0 to 180 advancing from 1 degree to 1 degree, and when it reaches 180 it returns to 0 advancing from 2 to 2. In addition, the angle or position from 0 to 0 must be displayed on the serial monitor. engine at all times of travel
*help you can get by using 2 For loops

When is your assignment due to be handed in ?

Hove you looked at the Servo Sweep example ?

Please read the how to get the most from the forum post. The post contains information on what we need to know in order to help you.

What have you tried?

What Arduino?

How is the servo powered?

The servo sweep example.

To answer your question yes! Show us what you have tried and a simple annotated schematic of your circuit with links to each of the hardware items. Hint: I will program the Arduino to control the servomotor to, not the servomotor.

Remember, you're in a loop already..
Adding 2 more will block..

no for loops..
2 globals..
1 position
1 direction
a couple of if's..
probably need a timer too else it will happen too fast..

good luck.. ~q

Please, post a picture of the Motor you are working with.

Hello mateon

consider

#include <Servo.h>
Servo myServo;
constexpr uint8_t ServoPin {2};
void setup()
{
  myServo.attach(ServoPin);
  Serial.begin(115200);
}
void loop()
{
  static uint8_t direction = 0;
  static uint8_t servoValue=0;
  Serial.print(direction ? "backwards with " : "forewards with "), Serial.println(servoValue);
  myServo.write(direction ? servoValue -= 2 : servoValue++);
  if (servoValue == 0 || servoValue == 180) direction = direction ? LOW : HIGH;
  delay(50);
}

Have a nice day and enjoy coding in C++.