I'm trying to write simple code to move my servo but cant figure it out!

I Have a simple push-button with 4 pins and a simple micro servo. I'm using an Arduino nano. I don't have any resistors and don't know if I will need them. What I'm trying to do is this:

  • When I start the process, the servo starts at 0 degrees
  • When I press down the button (hold) the servo moves from 0 to 180 degrees, not more
  • When I let go of the button, the servo moves from 180 degrees back to 0 degrees

I'd love for someone to help me with regards to the code, and the pins to attach my wires to on the arduino. Many thanks...

Archie

NO CODE POSTED = NO HELP

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

Please post a schematic of your project. A photo of a hand drawn circuit is good enough

How is the servo powered ?

If you’re starting from scratch, and just need someone to work with, you can try asking to move this across to the Help Needed section… formerly Gigs & Collaboration.

1 Like

@r-chee what have you tried so far ?

Have you tried the Button example in the IDE ? It turns an LED on/off when a button is pressed/released and could easily be adapted to move a servo one way or the other instead

#include <Servo.h>
const byte ServoPin = 3;
const byte ButtonPin = 4;

Servo servo;

// * When I start the process,
void setup()
{
  // the servo starts at 0 degrees
  servo.write(0);

  servo.attach(ServoPin);
  pinMode(ButtonPin, INPUT_PULLUP);
  // NOTE: The internal pull-up resistor will keep the
  // pin reading HIGH when not connected.  The pin
  // will read LOW when connected to Ground through
  // a closed switch.
}

void loop()
{
  // * When I press down the button (hold) 
  if (digitalRead(ButtonPin) == LOW)
  {
    // the servo moves from 0 to 180 degrees, not more
    servo.write(180);
  }
  else  // * When I let go of the button, 
  {
   // the servo moves from 180 degrees back to 0 degrees
    servo.write(0);
  }
}

[/quote]

1 Like

Thank you so much @johnwasser!
Is there a Circuit design you could quickly make so I know what everything looks like and where all the wires go?
Much appreciated...

Archie

Thanks I’ll see if that’s possible

At the moment it’s just powered by my laptop. But in the future I want to power it with as small as a battery as possible. Possibly a a23 battery. I was also wondering if it’s possible to make a second button spin a mini motor when held down or do I do need a seperate arduino to do that as well. Is it possible to substitute 2 other pins with gnd and 5v? If so then I might be able to use motor on it (arduino) as well as the servo?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.