# Help with Arduino Uno servo code PLEASE!?

Hello,

For the past hour I have been trying to develop an arduino code (I'm new to this and have no talent in programming). This is for my own personal project.

I am using the Arduino Uno and basically I am looking at If and Then Statements. I'll try to explain this as simple as possible.

I am trying to make it so that If power is going through one pin (not connected to servo) The servo will turn to 180 degrees. If the pin stops getting power then the servo will turn back to 0 degrees.

Since I am using the Digital pins it will be along the lines of If pin11 is 1 then servo will turn to 180 degrees, else if it will go to 0.

But I am not even sure which pin should be the one or zero "Sensor". Also the only part of the servo that will be connected to the Micro controller will be the yellow chord that receives the signals. The ground and power will be connected directly to the battery. Please help and let me know if I need to clear up anymore things.

could you post the code you currently have written?

I know my code is awful and makes no sense but here it is lol

I use ioref to be the pin that checks to see if an input is 1 or 0. If the input is one I want it to go to 180 degrees, if its 0 then I want it to go back to 0 degrees. But I don’t even know if that’s the right pin for the job.

My code is

#include <Servo.h>

Servo myservo;

int pos = 0;

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

void loop()
{
if ( ioref==1
then
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);

}
else if
for(pos = 180; pos>=1; pos-=1)
);
}
}

I've started writing a new one that is along the lines of

void setup () { pin 1 output } pin 2 input void loop( ) { if (p2==1) digitalwrite pwm

Is this a better start? Does it make more sense?

Is this a better start? Does it make more sense?

No.

I am not sure whether you example code is meant to be real or just an outline of what you want to do.

Start by looking at the Button example in the IDE. It turns an LED on when you press a button and off when you release it. Substitute the LED code with code to move the servo borrowed from the Sweep example that comes with the servo library to make it do what you want.

Is this some what you were looking for?

You don’t have to use this, but I thought it would nice to base something off of. This code is based off your original code, but isn’t complete either so there is still room for improvment.

``````#include <Servo.h>

Servo myservo;

int pos = 0;    // variable to store the servo position

void setup()
{
Serial.begin(9600); //start a serial port 9600 for debugging
pinMode(7, INPUT);  //make pin 9 our input. This is port will determines power input or no power input (1 or 0)
}

void loop()
{

Serial.println(digitalRead(7));  //print the value from pin 7 to the serial monitor

if(digitalRead(7) == 1)        //if the value of pin 7 is equal to 1 (power is applied)
{
myservo.attach(9);     //attach the servo to pin 9

while(pos < 180 && digitalRead(7) == 1)  // while'pos' is less than 180 AND pin 9 still equals 1
{
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                    // waits 15ms for the servo to reach the position
pos += 1 ;                //add 1 to 'pos'
}

myservo.detach();         //stop the servo

}

else if(digitalRead(7) == 0)   //else if pin 7 is equal to 0 (power is not applied)
{
myservo.attach(9);       //re-attach the servo to pin 9

while(pos > 0 || pos < 5 && digitalRead(7) == 0)     //while 'pos' is greater than zero OR less than 5 AND pin 7 still equals 0
{
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
pos -= 1;                     //subtract 1 from 'pos'
}

myservo.detach();                //stop the servo
}
}
``````

Thank you guys so much! Will get on it!

HailStorm:
Is this some what you were looking for?

You don’t have to use this, but I thought it would nice to base something off of. This code is based off your original code, but isn’t complete either so there is still room for improvment.

``````#include <Servo.h>
``````

Servo myservo;

int pos = 0;    // variable to store the servo position

void setup()
{
Serial.begin(9600); //start a serial port 9600 for debugging
pinMode(7, INPUT);  //make pin 9 our input. This is port will determines power input or no power input (1 or 0)
}

void loop()
{

Serial.println(digitalRead(7));  //print the value from pin 7 to the serial monitor

if(digitalRead(7) == 1)        //if the value of pin 7 is equal to 1 (power is applied)
{
myservo.attach(9);     //attach the servo to pin 9

while(pos < 180 && digitalRead(7) == 1)  // while’pos’ is less than 180 AND pin 9 still equals 1
{
myservo.write(pos);              // tell servo to go to position in variable ‘pos’
delay(15);                    // waits 15ms for the servo to reach the position
pos += 1 ;                //add 1 to ‘pos’
}

myservo.detach();         //stop the servo

}

else if(digitalRead(7) == 0)   //else if pin 7 is equal to 0 (power is not applied)
{
myservo.attach(9);       //re-attach the servo to pin 9

while(pos > 0 || pos < 5 && digitalRead(7) == 0)     //while ‘pos’ is greater than zero OR less than 5 AND pin 7 still equals 0
{
myservo.write(pos);              // tell servo to go to position in variable ‘pos’
delay(15);                       // waits 15ms for the servo to reach the position
pos -= 1;                     //subtract 1 from ‘pos’
}

myservo.detach();                //stop the servo
}
}

Why are the attach and detach commands where they are. One attach in setup() would suffice.

`      while(pos < 180 && digitalRead(7) == 1)  // while'pos' is less than 180 AND pin 9 still equals 1`Comment correct or code correct ?

`while(pos > 0 || pos < 5 && digitalRead(7) == 0)    //while 'pos' is greater than zero OR less than 5 AND pin 7 still equals 0`Why 2 checks on the value of pos ?

` pinMode(7, INPUT);  //make pin 9 our input. This is port will determines power input or no power input (1 or 0)`Comments don’t match the code again and
` pinMode(7, INPUT_PULLUP);`would probably be a better choice but we don’t know how the OP has got the button wired.

jeanpaulbadjo: the only part of the servo that will be connected to the Micro controller will be the yellow chord that receives the signals. The ground and power will be connected directly to the battery.

The ground from the servo must also be connected to the Arduino ground.

Don't use pins 0 or 1 - they are needed to communicate with the PC.

You don't say whether you want the servo to move slowly, or as fast as possible. If it's as fast as possible, you just need:

``````if(digitalRead(pin) == HIGH)
{
servo.write(180);
}
else
{
servo.write(0);
}
``````

Remember that many servos don't have a full 180 degrees of travel and you may need to alter the end values to avoid the servo running out of travel. Making the servo try to move past its end stop can damage it.

UKHeliBob:
Why are the attach and detach commands where they are. One attach in setup() would suffice.

`      while(pos < 180 && digitalRead(7) == 1)  // while'pos' is less than 180 AND pin 9 still equals 1`Comment correct or code correct ?

`while(pos > 0 || pos < 5 && digitalRead(7) == 0)    //while 'pos' is greater than zero OR less than 5 AND pin 7 still equals 0`Why 2 checks on the value of pos ?

` pinMode(7, INPUT);  //make pin 9 our input. This is port will determines power input or no power input (1 or 0)`Comments don’t match the code again and
` pinMode(7, INPUT_PULLUP);`would probably be a better choice but we don’t know how the OP has got the button wired.

#1 --I tried just having one but the servos either wouldn’t run or not stop. So a add the detach and attach.

#2–Code is correct, its a comment error.

#3–I am giving it a rang to stop in 0-5. It seemed to have trouble stopping at zero, so I gave it a range to allow for some error.

#4–Again another comment typo I am still getting used to arduino programming so my logic may not always be the fastest or easiest way of doing something. Thanks for the clarification! Very simple servo button code.

``````//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
if (press1 == LOW)
{
servo1.write(160);
}
else {
servo1.write(20);
}
}
``````

zoomkat:
Very simple servo button code.

``````//zoomkat servo button test 7-30-2011
``````

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
{"ft_lang_label":"__label__en","ft_lang_prob":0.7752908,"math_prob":0.7755445,"size":2152,"snap":"2021-43-2021-49","text_gpt3_token_len":634,"char_repetition_ratio":0.12290503,"word_repetition_ratio":0.35858586,"special_character_ratio":0.32527882,"punctuation_ratio":0.09589041,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9717781,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-07T03:59:00Z\",\"WARC-Record-ID\":\"<urn:uuid:b0b341bb-ed9c-4649-b5ca-b064a26a6646>\",\"Content-Length\":\"49732\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:65407468-d67b-4eea-9467-558a1a6da392>\",\"WARC-Concurrent-To\":\"<urn:uuid:2a2ca2e8-5007-4cc6-b6e5-917657abd6b7>\",\"WARC-IP-Address\":\"184.104.202.141\",\"WARC-Target-URI\":\"https://forum.arduino.cc/t/help-with-arduino-uno-servo-code-please/222916\",\"WARC-Payload-Digest\":\"sha1:POUNPKJX2TKENB55EXNQCSGMCQXAIQ3V\",\"WARC-Block-Digest\":\"sha1:XGI3XT4LEMO65X6LMGQPKVSATSX7CI2F\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363332.1_warc_CC-MAIN-20211207014802-20211207044802-00515.warc.gz\"}"}