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);
}
}
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.
#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;
}
}