renlc
September 20, 2021, 1:09am
1
I'm trying to use an obstacle sensor to activate the servo. That's been accomplished.
However, servo speed is kinda fast fot what I'm using it (a rack and pinion). I tried delay but its not working.
This is the code I'm currently using:
#include <Servo.h>
Servo servo;
int sensor_pin = 7; //pin on ta el sensor de obstaculos
int servo_pin = 2; //pin on ta el servo
int val; //si algo se acerca al sensor
void setup() {
pinMode (sensor_pin, INPUT); //sensor como input
servo.attach(servo_pin); //attacha el servo a si pin
}
void loop() {
val = digitalRead(sensor_pin); //lee el sensor
if (val==0) //nada cerca del sensor
{
servo.write(0);
delay (200);
}
if (val==1) //cerca del sensor
{
servo.write(90);// grados a girar
delay (100);
}
}
LarryD
September 20, 2021, 1:11am
2
renlc:
servo.write(90);
Use smaller increments until you reach 90°
1 Like
renlc
September 20, 2021, 2:07am
4
What do you mean by increments?
Wawa
September 20, 2021, 2:47am
5
Move one degree, wait, move one degree, wait, move one degree.....
The IDE has a servo>sweep example that shows you how to do that.
Leo..
1 Like
Do a search for the varSpeedServo library.
1 Like
LarryD
September 20, 2021, 3:28am
7
#include <Servo.h>
Servo servo;
bool servoMoving = false;
const byte sensor_pin = 7; //+5V---[Sensor]---GND
const byte servo_pin = 2; //servo pin
byte moveTo = 0;
byte currentLocation = 0;
byte servoSpeed = 10; //10ms between increments
byte servoIncrement = 2; //degree between servo movement
int val; //sensor
int incDec = 1;
unsigned long servoMillis;
//*******************************************************************
void setup()
{
pinMode (sensor_pin, INPUT_PULLUP); //sensor
servo.attach(servo_pin);
} //END of setup()
//*******************************************************************
void loop()
{
val = digitalRead(sensor_pin);
//*********************
if (servoMoving == false && val == 0)
{
moveTo = 0;
if (currentLocation >= moveTo)
{
incDec = -1;
}
else
{
incDec = 1;
}
//the servo is now going to move
servoMoving = true;
//start the TIMER
servoMillis = millis();
}
//*********************
if (servoMoving == false && val == HIGH)
{
moveTo = 90;
if (currentLocation >= moveTo)
{
incDec = -1;
}
else
{
incDec = 1;
}
//the servo is now going to move
servoMoving = true;
//start the TIMER
servoMillis = millis();
}
//*********************
//if enabled, is it time to move the servo ?
if (servoMoving == true && millis() - servoMillis > servoSpeed)
{
//restart the TIMER
servoMillis = millis();
moveServo();
}
} //END of loop()
//*******************************************************************
void moveServo()
{
if (currentLocation == moveTo)
{
//we have arrived
servoMoving = false;
return;
}
currentLocation = currentLocation + incDec;
servo.write(currentLocation);
} //END of moveServo()
1 Like
renlc
September 20, 2021, 3:59am
9
I see! The servo sweep uses the delay (I think?) and it wasn't working for some reson in my code.
Still, thank you
LarryD
September 20, 2021, 4:01am
10
Had a small mistake in the posted code.
Here is version 2.
Points if you can tell what was wrong with the sketch in post #7 .
//Version two
#include <Servo.h>
Servo servo;
bool servoMoving = false;
const byte sensor_pin = 7; //+5V---[Sensor]---GND
const byte servo_pin = 2; //servo pin
byte moveTo = 0;
byte currentLocation = 0;
byte servoSpeed = 10; //10ms between increments
int val; //sensor
int incDec = 1;
unsigned long servoMillis;
//*******************************************************************
void setup()
{
pinMode (sensor_pin, INPUT_PULLUP); //sensor
servo.attach(servo_pin);
} //END of setup()
//*******************************************************************
void loop()
{
val = digitalRead(sensor_pin);
//*********************
if (servoMoving == false && val == 0)
{
moveTo = 0;
if (currentLocation >= moveTo)
{
incDec = -1;
}
else
{
incDec = 1;
}
//the servo is now going to move
servoMoving = true;
//start the TIMER
servoMillis = millis();
}
//*********************
if (servoMoving == false && val == HIGH)
{
moveTo = 90;
if (currentLocation >= moveTo)
{
incDec = -1;
}
else
{
incDec = 1;
}
//the servo is now going to move
servoMoving = true;
//start the TIMER
servoMillis = millis();
}
//*********************
//if enabled, is it time to move the servo ?
if (servoMoving == true && millis() - servoMillis > servoSpeed)
{
//restart the TIMER
servoMillis = millis();
moveServo();
}
} //END of loop()
//*******************************************************************
void moveServo()
{
if (currentLocation == moveTo)
{
//we have arrived
servoMoving = false;
return;
}
currentLocation = currentLocation + incDec;
servo.write(currentLocation);
} //END of moveServo()
1 Like
Wawa
September 20, 2021, 6:51am
11
You did only have one delay, after the full movement.
There must have a small delay between each small movement.
Leo..
1 Like
renlc
September 20, 2021, 2:51pm
12
Is it that "byte servoIncrement" was not in use?
LarryD
September 20, 2021, 3:31pm
13
Yes, we could have used an increment variable but in this case we just needed an increment value of one (1).
The servoSpeed variable which is the time between incrementing adjusts our servo speed.
system
Closed
January 18, 2022, 3:32pm
14
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.