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 1Comment 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 0Why 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 1Comment 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 0Why 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.

Numbering your questions 1-4

#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 :slight_smile:

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! :slight_smile:

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()
{
  press1 = digitalRead(button1);
  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()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

It doesn’t stop the servo when it gets to its set value. Instead it just sweeps the servo back and forth.