How do I make my servomotors move "slowly" according to sound sensor data?

Hey!

For my project I’d like my servo motors to move in an “open-close” motion in response to the sound input. I’ve managed to get all of that done, except I’d like the motors to move slowly, and hopefully then work out how to make them move back down much slower when there is no sound input.

I’ve tried heaps of things and understand that the motors must move in increments, I’ve been referencing the ‘Sweep’ example, tried booleans, tried variables, but I can’t work out what goes where so I’m a bit stuck. The furthest I’ve got is to make the motors switch direction based on the sound input, but it’s not what I want.

Any help is really appreciated!

#include <Servo.h>
Servo servo1;
Servo servo2;
boolean DEGStatus=false;
int sensorValue;
int deg;
int n;

void setup() {
 Serial.begin(9600);
 servo1.attach(2);
 servo2.attach(3);

}

void loop(){

  sensorValue = analogRead(A0);
//    deg = sensorValue/5, 100;
  Serial.println(sensorValue);
  
if (sensorValue > 600) {
  DEGStatus=true;
    servo1.write(90);
    int n = 90;
    servo2.write( 0 +- n);
    delay(10);
  } else {
    DEGStatus=false;
    servo1.write(0);
//      int n = 90;
    servo2.write(90);
  }
   
}

And here is one example (of the tens) I’ve tried. It is the one where the motor changes direction according to the sound input:

#include <Servo.h>
Servo servo1;
Servo servo2;
boolean DEGStatus=false;
int sensorValue;
int deg;
int pos;
int pos2;

void setup() {
 Serial.begin(9600);
 servo1.attach(2);
 servo2.attach(3);

}

void loop(){

  sensorValue = analogRead(A0);
//    deg = sensorValue/5, 100;
  Serial.println(sensorValue);
  
if (sensorValue > 600){
  posClock();
} else {
  posAnti();
}
  
 
}

void posClock(){
for (pos = 0; pos <= 180; pos += 1) {
  servo1.write(pos);
  delay(15);
}
}

void posAnti(){
   for (pos2 = 180; pos2 >= 0; pos2 -= 1) {
   servo1.write(pos2);
     delay(15);
}
}

One thing you need to remember is that servos are slow compared to microcontrollers.
Their position control is only updated 50 times a second, or once ever 20 milliseconds, so on average, any Servo.write will be acted upon 10 milliseconds later, but that action will not be instantly result in the servo moving to the requested position.

There’s also a variable speed servo library.

If you want to send to servo from 0 to 100 degrees then send the servo in increments.

Such as ok going to 100 and am at 0, so if I increment to 1 degree and then wait 15mS then go to 2 degree and wait 15 mS, then go to 3 then wait 15mS, then go to 4 and wait 15mS and do this till the motor is at 100.

Or you could just use VarSpeedServo.h in place of the standard Servo.h. It has a useful speed parameter in the write() command that does almost all of it for you.

Steve

This is also a good time to explore millis() because you’ll need to understand it within a few days.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.