How to turn a power pin on and off

In my project I have a micro servomotor attached to a elegoo UNO R3 board (which I believe is very similar to an Arduino board). I need the motor to rotate under the influence of an outside force when not activated, but with power being supplied to it (even if the board is not telling it to spin), it is too stiff to turn. Is there some code I could use to turn a power pin on and off so an outside force can rotate it when the motor's power supply pin is turned off, and then turn the pin back on to have the board control it?

If you want the servo to be able to be turned by hand then use the detach() function, but be aware that when/if you attach() it again it will move immediately to its previously commanded position

Thank you. The detach() function should work great. I need it to move back into its original position once I am done moving it by hand.

I have tried to include the detach function in my code. I am having this error: "no matching function for call to 'Servo::detach(int)'" Any ideas on how to fix this? I made sure I am using the "#include" function to include the servo library, and I updated my library to the latest version.

Not unless you post the code...

How should I post my code? Do I include it in the reply or through another method?

Please read the forum guideline threads. They have that information and a lot more you should know.

In the IDE, I believe it is in the Edit->Copy for Forum and then paste into a message.

#include <Servo.h>

Servo lockservo;

int unlockangle = 0;
int lockangle = 200;
int dtime = 5*1000;
bool Dig1 = false;
bool Dig2 = false;
bool Dig3 = false;
bool Dig4 = false;
bool Dig5 = false;
bool Dig6 = false;
bool Dig7 = false;
bool Dig8 = false;
bool Dig9 = false;

void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
lockservo.write(lockangle);
}

void loop()
{
if(digitalRead(2) == LOW)
{
Dig1 = true;
}

if(digitalRead(3) == LOW && Dig1 == true)
{
Dig2 = true;
}

if(digitalRead(4) == LOW && Dig2 == true)
{
Dig3 = true;
}

if(digitalRead(5) == LOW && Dig3 == true)
{
Dig4 = true;
}

if(digitalRead(6) == LOW && Dig4 == true)
{
Dig5 = true;
}

if(digitalRead(7) == LOW && Dig5 == true)
{
Dig6 = true;
}

if(digitalRead(8) == LOW && Dig6 == true)
{
Dig7 = true;
}

if(digitalRead(10) == LOW && Dig7 == true)
{
Dig8 = true;
}

if(digitalRead(11) == LOW && Dig8 == true)
{
Dig9 = true;
}

if(digitalRead(12) == LOW)
{
Dig1 = false;
Dig2 = false;
Dig3 = false;
Dig4 = false;
Dig5 = false;
Dig6 = false;
Dig7 = false;
Dig8 = false;
Dig9 = false;
}

if(Dig9 == true)
{
lockservo.attach(9);
lockservo.write(unlockangle);
delay(dtime);
lockservo.write(lockangle);
lockservo.detach(9);
Dig1 = false;
Dig2 = false;
Dig3 = false;
Dig4 = false;
Dig5 = false;
Dig6 = false;
Dig7 = false;
Dig8 = false;
Dig9 = false;
}
}

That was clearly not a "Copy for Forum" because there are no code tags formatting it. You got away with it this time, there are many characters that will cause a post like that to turn to gibberish. Anyway, you lucked out and it's readable.

You created a Servo object named "lockServo" and attached it to pin 9. When you detach it, it already knows what pin you previously attached to it, so you don't and can't specify a pin number.

That is really confusing, cascading the logic based on the preceding assignment of variables. I have to ask, what are you really trying to do there? If it's just checking that all the inputs are LOW, there are hundreds of better, more concise, and easier to understand ways to do it.

On that note, you should have comment lines to explain what is going on, embedded in the program. I had to take a wild guess...

Also your variable names should be descriptive. "Dig9" means nothing as far as I can see.

Here is a hint:

for (int i = 2; i <=12; i++) {
  pinMode(i, INPUT_PULLUP);
}

I am trying to make a keypad to unlock my door. Pressing the buttons in the right order will make the servo motor turn, unlocking the door. The "DigN's" represent digits or the numbers of the buttons. I should mention that I am a beginner. I am working with my current (small) pool of knowledge. I know my code could be better - and I will make it better as I learn more. I will make it a practice now to include comment lines. Thank you for your help and critiques.

Hmmm. Well I don't think it will do what you describe at all, sequential keys to unlock. They are all processed at the same time, so holding down all the keys will open the lock. Oops, sorry, actually pressing "Dig1" alone will always unlock it.

Edit - okay wait I see it now. Okay, it may work. I did misinterpret the logic flow. It's just really confusing code. Sorry about that...

Does the servo do what you want now?

Don't worry. You are forgiven. Yes the servo works great. I have been playing with it during our conversation.

Think about your problem. If you consider some series of pin numbers, to enforce an order you only have to screen for the currently selected pin. So if you keep track of that number, you don't have to maintain separate variables for each one.

Pseudocode:

locked = true;
pinNum = first pin;
while there are still pins {
  if pin[pinNum] is LOW
    {
     pinNum = pinNum +1
    }
  if pinNum is the last pin
  { locked = false }
}

only one variable to keep track of the current pin number...

1 Like

Absolutely amazing. That is infinitely better. Thank you very much.