servo programming

Hi guys

i want to build a small box 3x3x3 inches with a servo operating lid to be put in a car
i know i will have to fit a voltage regulator too regulate the voltage supplied the the Arduino

what i want the lid to do is open when the ignition is on and stay open
and close when the lid when the ignition is off

here is the program i have used to open the lid
#include <Servo.h>

Servo servo;
int angle = 0;

void setup() {
servo.attach(2);
servo.write(angle);
}

void loop()
{
// scan from 0 to 180 degrees
for(angle = 0; angle < 180; angle++)
{
servo.write(angle);
delay(30);
}
// now scan back from 180 to 0 degrees
for(angle = 0; angle > 180; angle--)
{
servo.write(angle);
delay(30);

}servo.detach();

}

what i am having trouble with is the closing part of the program

because i am taking the power away from Arduino on Vin pin
can i use the analog ports to operate the servo if i give the Arduino another switch 5 volts on the 5v pin

steve63:
what i am having trouble with is the closing part of the program

do you feel the angle variable moves from 180 down to 0 written this way?

  // now scan back from 180 to 0 degrees
  for(angle = 0; angle > 180; angle--)

have a look at the for loop syntax

steve63:
because i am taking the power away from Arduino on Vin pin
can i use the analog ports to operate the servo if i give the Arduino another switch 5 volts on the 5v pin

I'm not sure I get what you say there... Do you mean that when ignition goes off your arduino will loose power so you don't know how to close the lid? you need a bit of power set aside for your arduino to continue to operate for long enough (along with the servo) after you detect a power cut.

steve63:
what i am having trouble with is the closing part of the program

because i am taking the power away from Arduino on Vin pin
can i use the analog ports to operate the servo if i give the Arduino another switch 5 volts on the 5v pin

I think something like an Adafruit PowerBoost circuit is what you are looking for.
But I might be wrong.

Check it out here: PowerBoost 1000 Charger - Rechargeable 5V Lipo USB Boost @ 1A [1000C] : ID 2465 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Thanks for your reply

But how does the Arduino know that the ignition is switch off to start the closure of the lid??

steve63:
But how does the Arduino know that the ignition is switch off to start the closure of the lid??

if there is a way for you to have some power after ignition is switched off, then you can just monitor with a pin the state of the power and trigger some code when power is going away.

We actually just discussed this topic of monitoring power in the french forum and @hbachetti was kind enough to put together a small tutorial (in french but google translate can help) on this topic and how you can use a side capacity to have enough energy to do what you want before your arduino goes off

i have look through this forum and found this
The Arduino has a built in power switch.
If the i/p is > 6.2 volts USB power is turned off and the Arduino is powered by the i/p 12V (in this case).
If you were to connect a 4.8V P.S. (3 X 1.6 = 4.8V Alkaline D cells) to the USB power pins, no power would be drawn normally.
When the power fails the switch would turn on and the Arduino would be powered by the batteries (while power is off).
Then use cjdelphi idea of the voltage divider to detect the power loss.

so if i was to give power through the Vin pin when ignition is on and the use the usb port when the ignition is off

i think this would sort my live feed problem

its the coding that i am finding hard as i am new to this Arduino

steve63:
so if i was to give power through the Vin pin when ignition is on and the use the usb port when the ignition is off

your 3 Alkaline D cells will be constantly powering the Arduino until the next ignition though... how long do you think they will last ? (you could sleep your arduino once the lid is closed but then you need some sort of interrupt to wake it up when starting your car)

I can take the main feed from the ignition switch to do the first part opening the lid
And when ignition is switch off i can take the live feed from the clock which is live all the time to close the lid

its just the coding that i need help with

Which clock ?

I would think that constantly drawing current in a car from the battery is usually a bad idea and that will byte you if you leave the car unused for a long while. Monitoring power on pin 2 could be a way to awake arduino from sleep by attaching an interrupt there when 5V becomes available on that pin, but i’d rather go for a design without powering anything at rest, just put a large enough cap to close the lid when power goes away or put a small spring that will close the box automatically if power is cut (the servo becomes soft and can’t resist the spring force even if small)