# Controlling Speed of Micro Servo based on Water Flow

Hi there,

Thanks for taking the time to help me out!

On the Arduino program, I’m trying to control the speed of my micro servo so that it rotates 180 degrees based on how much water is flowing through my water sensor.

I currently have “delay” to control the speed but I don’t think this will work in sync with water flowing through my sensor. Do you have any suggestions of particular code I can use to make this work?

Here is what I have at the moment:

#include <Servo.h>

Servo servo_test; //initialize a servo object for the connected servo

int angle = 0;

volatile double waterFlow;

void setup() {
Serial.begin(9600); //baudrate
waterFlow = 0;
attachInterrupt(0, pulse, RISING); //DIGITAL Pin 2: Interrupt 0

servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino

}
void loop() {
Serial.print(“waterFlow:”);
Serial.print(waterFlow);
Serial.println(“L”);
delay(500);

const int angleIncrement = 1;
int incrementDelay = map(waterFlow*100, 0, 100, 10, 200);
Serial.println(incrementDelay);
for (int angle = 0; angle < 180; angle += angleIncrement) { // single “degree” increments
servo_test.write (angle);
delay (incrementDelay);
}

for (angle = 180; angle >= 1; angle -= 10) // command to move from 180 degrees to 0 degrees
{
servo_test.write(angle); //command to rotate the servo to the specified angle
delay(incrementDelay);
}

}

void pulse() //measure the quantity of square wave
{
waterFlow += 1.0 / 450.0;
}

Thanks for your help! It is greatly appreciated!

Declan

You'll have to go with the Blink Without Delay approach and unroll your for loops so that on each turn of the loop function you only take one step.

If you cared enough to read the "How to use this forum - please read" post first and properly post your code then I might care enough to show you how. Since you thumb your nose at our forum policies I'll just do the same for you.

Programming is a field where you absolutely MUST be willing to read the instructions first.

Hi Delta_G,

I'm sincerely sorry I didn't read the forum policies. I'm new to programming and I'm just trying to get some much needed help. I'm not trying to make up excuses so I will make sure I thoroughly read the policy before posting again.

Is there any other information you could provide me that would help or any links to resources that can?

Kind Regards,
Declan

Hi Delta_G,

Thank-you for your patience. Here is my properly posted code for the above issue:

``````#include <Servo.h>

Servo servo_test;        //initialize a servo object for the connected servo

int angle = 0;

volatile double waterFlow;

void setup() {
Serial.begin(9600);  //baudrate
waterFlow = 0;
attachInterrupt(0, pulse, RISING);  //DIGITAL Pin 2: Interrupt 0

servo_test.attach(9);      // attach the signal pin of servo to pin9 of arduino

}
void loop() {
Serial.print("waterFlow:");
Serial.print(waterFlow);
Serial.println("L");
delay(500);

const int angleIncrement = 1;
int incrementDelay = map(waterFlow*100, 0, 100, 10, 200);
Serial.println(incrementDelay);
for (int angle = 0; angle < 180; angle += angleIncrement) { // single "degree" increments
servo_test.write (angle);
delay (incrementDelay);
}

for (angle = 180; angle >= 1; angle -= 10) // command to move from 180 degrees to 0 degrees
{
servo_test.write(angle);              //command to rotate the servo to the specified angle
delay(incrementDelay);
}

}

void pulse()   //measure the quantity of square wave
{
waterFlow += 1.0 / 450.0;
}
``````

Kind Regards,
Declan

Because I’m lazy I’d try using VarSpeedServo.h instead of Servo.h. See GitHub - netlabtoolkit/VarSpeedServo: Arduino library for servos that extends the standard servo.h library with the ability to set speed, and wait for position to complete

VarSpeedServo.write has an extra parameter for the speed of movement which you could map fairly directly to your waterflow. That way you don’t need the for loop,write and delay. You can do it all with one write().

Steve

Thanks for that Steve!

I have actually added this into the code because I too am somewhat lazy. I am yet to test it with the water flow but I'm sure I can get it to map fairly directly to the rate of flow. Thanks again for your help! Greatly appreciated.

Declan