I am controlling a servo motor using an arduino UNO R3.
The servo motor would rotate once every 6 hours using a timer.
How do I make the servo motor sleep when it has just rotated and wake up every 6 hours when it has to rotate?
Is there a code to do this?
I am using an LCD display as well and I am making use of the "delay" command as the "timer".
The user would input a "1st hour" and a "current hour". The current time would then increase by 1 using the delay(3600000) which is 1 hour. when the current hour reaches the 1st hour, the servo will rotate. The next rotation time would be the 1st hour + 12 hours/8hours/6 hours (this depends on the user input as well). So the motor would then rotate again at the calculated time. In between, the servo does not do anything. But upon running the program, I understand that internally, the servo still running and could feel some vibration.
So I would like to know if it is possible to have a code to temporarily sleep the servo till its next rotation time.
If you power off the servo it will jump, possibly on power off, and definately on power on. Also - with power removed it might not hold position ans the motor would have no holding torque. How much power does it use when sitting?
A DC motor or a stepper motor through some gear reduction might be a better choice as they could both be powered off.
So I would like to know if it is possible to have a code to temporarily sleep the servo till its next rotation time.
You might try the servo.detach(); function to stop the control signal to the servo.
Ok, Then if I want to turn it back on just before the motor is required to rotate, is it servo.attach(); ?
I have attached a part of my code below:
if (feedfrequencyperday == 2) // freq input is 2
{
lcd.setCursor(9,3);
lcd.print(totalfeedforfeedfrequencyperday2); // total number of feed (freq x
days)
for (runninghour = currenthour; runninghour <= 24; runninghour++) // runninghour acts
as hour clock value
{
if (feeddone == totalfeedforfeedfrequencyperday2) // total number of feeds
completed
{
setCounter = 0; // stop running the program
}
else if (feeddone < totalfeedforfeedfrequencyperday2) // feeds not completed
{
if (runninghour == 24) //if runninghour = 24, reset to 0 (just
like hour clock)
{
lcd.setCursor(13,2);
lcd.print(" ");
runninghour = 0;
}
lcd.setCursor(13,2);
lcd.print(runninghour);
if (runninghour == firstfeedhour) // when runninghour value reaches the firstfeedhour
{
servo(); // servo rotates
feeddone++; // feeddone will increase by 1
whenever servo rotates
lcd.setCursor(17,3);
lcd.print(feeddone);
}
secondfeedhour = firstfeedhour + 12; // 2nd feed hour calculated
if (secondfeedhour < 24)
{
newsecondfeedhour = secondfeedhour;
}
else if (secondfeedhour >= 24)
{
newsecondfeedhour = secondfeedhour - 24;
}
if (runninghour == newsecondfeedhour) // when runninghour reaches 2nd feed hour, servo rotates
{
servo();
feeddone++; // feeddone value increases
lcd.setCursor (17,3);
lcd.print(feeddone);
}
delay (3600000); // delay func acts as the timer
// every 1 hr interval, the
runninghour value (hour clock) will increase by 1
for test purpose, 2000ms interval used
}
and the servo code is:
void servo()
{
for (int i= 0; i < 1; i ++)
{
for(servoposition = 0; servoposition < 180; servoposition += 1) // goes from 0 degrees to 180
degrees
{ // in steps of 1 degree
myservo.write(servoposition); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(servoposition = 180; servoposition>=1; servoposition-=1) // goes from 180 degrees to 0
degrees
{
myservo.write(servoposition); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
So would it be correct to use the detach command at the end of the servo code?
// zoomkat 10-14-11 serial servo test
// type servo position 500 to 2500 in serial monitor
// type in number <500 to detach servo
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(2000); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-22"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); // allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt();
if (n < 500) {
myservo.detach();
}
else {
myservo.attach(7);
myservo.writeMicroseconds(n); //convert readString to number for servo
}
readString=""; //empty for next input
}
}
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int potpin = A0; // analog pin used to connect the potentiometer
const int servoPin = 9;
int lastVal = 0; // variable to read the value from the analog pin
unsigned long timeOfLastChange = 0;
void setup()
{
}
void loop()
{
int val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
if (val != lastVal)
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
timeOfLastChange = millis();
lastVal = val;
}
else if (millis() - timeOfLastChange > 500)
{
myservo.detach();
}
}
Servo attach/dettach has no effect on power to the servo. It might lower the power consumption partially because it is sending to control signal, but the servo is still powerred.
It might lower the power consumption partially because it is sending to control signal, but the servo is still powered.
I guess the terms "sleep a servo motor" should be more technically defined. There have been other previous discussions of removing total power from a servo. when a servo is detached there should not be any control signal going to the servo control chip, which would result in the servo h-bridge not receiving any control pulses, resulting in the servo motor not receiving any power, which could be equated to the motor being "asleep". The servo chip and the pot in the servo are using some power anytime the servo itself is receiving power.