Servo with pot and micros function

I am making a programm for a servo, where i can set the speed with a pot. I am also using the micros() function for a non delay programm. I am doing something wrong in my programm, but i dont know what. Can someone help me?

my programm :

#include <Servo.h>
// this is to create a servo object to controll the servo
Servo myservo;
// twelve servo objects can be created on most boards
// rhis is the pin where the servo is hanging on
const int SERVO = 9;
// this is the pin where the potentiometer on hangs
const int POTPIN = 0;
// this is the variable to store the servo's position
int pos;
// this is the variable to store the potentiometers value
int valPot;
// this is the standart speed for the servo microseconds
const int STANDARTSPEED = 5000;
// this is fast speed for the servo microseconds
const int FASTSPEED = 2000;
// this is slow speed for the servo in microseconds
const int SLOWSPEED = 15000;

// this is the max amount of degrees for the servo
// I putted it to 210 beacuse 180 is not actualy 180
const int MAXPOSITION = 210;

// these variables are for the micros function
unsigned long startMicros;
unsigned long currentMicros;

// this is the variable to let the servo move
bool runningUp = true;
// this is the minimum amount of degrees for the servo
const int MINPOSITION = 0;


void setup()
{
  // this is so we can use serial monitor later on in the programm
  Serial.begin(9600);
  // this is to attach the servo to pin 9
  // the 544 is the mimum value of the servo
  // the 2400 is the maximum value of the servo
  myservo.attach(SERVO, MINPOSITION, MAXPOSITION);
  // this is to set the servo to 0 degrees
  myservo.write(0);
  // this is to wait 1s
  // I putted this delay here, it can be put here beacuse it only goes true once
  delay(1000);
}

void loop()
{

  // this is to read the value of the potentiometer
  valPot = analogRead(POTPIN);
  // this is to map the value of the pot to fast/standart speed
  valPot = map(valPot, 0, 1023, 5000, 10000);

  // take a new time stamp by POLLING
  unsigned long startMicros = micros();



  if (startMicros - currentMicros > valPot)
  {
    startMicros = currentMicros;
    if (runningUp)
    {
      // this is to set the position of the servo from 0 degrees to 210 degrees
      for (pos = 0; pos < MAXPOSITION;  pos++);

      // this is to see if it has reached the maximum amount of degrees
      if (pos >= MAXPOSITION)
      {
        runningUp = false;
      }

    }
    else
    {
      // this is to set the position of the servo from 210 degrees to 0 degrees
      for (pos = 210; pos > MINPOSITION;  pos--);

      // this is to see if it has reached the minimum amount of degrees
      if (pos <= MINPOSITION)
      {
        runningUp = true;
      }
    }
    // this is to write the position to the servo
    myservo.write(pos);
  }
}

Edit the post to include code tags around the code.

Explain the problem with a bit more details.

The servo is going like crazy, it goes from the left to the right, stops in the middle or does nothing. I want it to go from 0 to 180 degrees, but it doesn't do that.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

Post images of your project. Post a schematic.

You have got startMicros and currentMicros confused

currentMicros should be set each time through loop() whilst startMicros should only be set when a period starts

#include <Servo.h>
Servo myservo;// this is to create a servo object to control the servo// twelve servo objects can be created on most boards
const byte SERVO = 9;// this is the pin where the servo is hanging on
const byte POTPIN = A0;// this is the pin where the potentiometer on hangs
byte pos = 0; // this is the variable to store the servo's position
int valPot = 0; // this is the variable to store the potentiometers value
uint32_t timer = 0;
void setup() {
  myservo.write();  // this is to set the servo to 0 degrees
  myservo.attach(SERVO);  // this is to attach the servo to pin 9
  delay(100);
}

void loop() {
  valPot = (valPot + analogRead(POTPIN)) / 2;
  if (millis() - timer >= 100) {
    pos = map(valPot, 0, 1023, 0, 180); // this is to map the value of the pot to fast/standart speed
    myservo.write(pos);  // this is to write the position to the servo
    timer+=100;
  }
}

this is to set the angle with the pot, but i want that if I rotate the pot the speed of the servo goes up or down

and how the angle will be set?

I am setting the angle with these two for loops

      for (pos = 0; pos < MAXPOSITION;  pos++);

      for (pos = 210; pos > MINPOSITION;  pos--);

#include <Servo.h>
Servo myservo;
const byte SERVO = 9;
const byte POTPIN = A0;
const byte MAXPOSITION = 2600;
const byte MINPOSITION = 500;
int pos = 0;
int valPot = 0;
bool CW = false;

void setup() {
  myservo.write();  // this is to set the servo to 0 degrees
  myservo.attach(SERVO, MINPOSITION, MAXPOSITION);
  delay(100);
}

void loop() {
  valPot = (valPot + analogRead(POTPIN))/2 ;
  if (CW) {
    pos++;
    if (pos >= MAXPOSITION) {
      CW = false;
      pos = MAXPOSITION;
    }
  } else {
    pos--;
    if (pos <= MINPOSITION) {
      CW = true;
      pos = MINPOSITION;
    }
  }
  myservo.writeMicroseconds(pos);
  delayMicroseconds(valPot);
}

I think that this is a good program, but the servo isn't turning 180 dgrees

Is your servo able to turn 180°? Not all servos can do that. From my experience most servos don't have a real working range of 180°

I think the MIN and MAX are supposed to be in microseconds, not degrees.

mark as solution

and now?

no, it is still not working

Please post "it"


those are the max positions that the servo goes to

Looking at the pics, it appears the servo turned about 40° clockwise, put a crayon mark on one end of the horn and repost the pics.

what do you mean with"put a crayon on one end of the horn"?