Timelock

I’m fairly new to programming and know this is a simple code to write, but I’m having problems. I’m trying to make a box with a lock actuated with a servo motor. I want to be able to have the servo start at 90 degrees and then go to 180 degrees then stay there for an amount of time I can change by editing a delay(x) then return to 90 and then never repeat. I know this program does not work, but I want to prove how much help I need XD. I tried just editing the knob program and replace the pot inputs with set variables without luck

#include <Servo.h>

Servo myservo;

int val; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(512);
myservo.write(val);
delay(15);

val = analogRead(1023);
myservo.write(val);
delay(15);

delay(x);

val = analogRead(512);
myservo.write(val);
delay(15);
}

The syntax for analogRead is val = analogRead(pin); I don't think your Arduino has a pin 512 or 1023. Servo.write will take values 0-180 (degrees) or around 1000-2000 (microseconds). Your delays are way too short. 15 milliseconds will not be enough time for any movement. A lot of hobby servos have a spec of around 200 milliseconds to go 60 degrees.

Would this be a good fix? Also what can I add at the end of the void loop to indicate that I don’t want the program to continue any more after the servo returns to the 90 degree mark?

#include <Servo.h>

Servo myservo;

void setup()
{
myservo.attach(9);
}

void loop()
{
myservo.write(90);
delay(300);

myservo.write(180);
delay(300);

delay(x);

myservo.write(90);
delay(300);
}

That makes more sense and with the value of x in delay(x) defined should compile. If you want the code to only run once, put the code in setup() and leave loop() empty ( void loop() {}). Or at the end of loop() put while(1); which is an endless loop.

Thank you :smiley:

You are welcome. If you need more help we will be here.

So I tried out the code, and the servo is constantly powered so that it can correct its position. Is there any way that I can have the servo moved into position, and then have the power cut? I plan on having the servo in the same position for hours on end and I don't want to have the servo overheat or malfunction, just because of the length of time.

You can issue a servo.detach, which stops the Arduino constantly reminding the servo where it should be. (As long as you're sure the load on the servo will not continuously be trying to move it away from where you want it.)

I'm not sure what current a detached servo draws on its power line though. From memory of some testing I did quite a while ago, even an attached servo draws very little current unless it's trying to hold position. Easy enough to test with an ammeter.

If you really want to turn the power off, as opposed to detaching it, you could rig a transistor in the power line and toggle the transistor with another i/o pin. But that might be over-complicating things.

I am currently using the servo to actuate a deadbolt style mechanism, so there wont be any load on the servo. If I add servo.detach to the code, would I then need to do servo.reattach before I try and unlock the box again?

This is what my loop looks like. The program doesn't recognize myservo.detach, what should this section look like?

{
myservo.attach(9);

myservo.write(90);
delay(300);

myservo.write(0);
delay(300);

myservo.detach(9);

delay(x);

myservo.attach(9);

myservo.write(90);
delay(300);
}

Yes you need a servo.attach() later on when you want to move it. My testing with a 'scope showed that when you (re)attach, the pulse it sends is the same as it was before the detach so it's not going to zoom off to some random angle.

The program doesn't recognize myservo.detach

I don't think it needs the pin number, see reference.

It didnt need to have the pin number, thank you.

Great.

Just fyi, attached pic is 'scope trace from some testing I did. As you’ll see it’s a 50Hz “reminder”. This particular pic is just after an attach which defaults to 1500us.

servo attached.jpg