Servo control help

Hello, Everyone, I’m new to programming and I need a little help!

I have this code that controls servo that comes with the pre-sample programs in Arduino.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

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

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(20);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(20);                       // waits 15ms for the servo to reach the position
  }
}

I need to add an if else statement in here so if LED on pin 2 is high then it will rotate 0 to 180 degrees and when led on Pin 2 is Low it will rotate the servo from 180 to zero degrees.

Can someone help me with this, please?

Thank you in advance!!!

if (digitalRead(2)==1) //if HIGH
{
do_this();
}

so if LED on pin 2 is high then it will rotate 0 to 180 degrees and when led on Pin 2 is Low

What is going to change the state of the LED ?

Rmedina4:
I need to add an if else statement in here so if LED on pin 2 is high then it will rotate 0 to 180 degrees and when led on Pin 2 is Low it will rotate the servo from 180 to zero degrees.

Can someone help me with this, please?

Thank you in advance!!!

No. Add it first and then either pat yourself on the back for doing a good job, or come back here with your revised code, error messages and possibly a diagram. How do you expect to learn if you just ask others to do it for you?

Just use a digital read. It is extremely simple. UKHeliBob has a good point, how are you changing it, please post your complete code.

-- DH

I'm not usually this critical of Newbies, but all you've done is copy the sweep example from the servo library distribution, deleted the credits to Scott Fitzgerald and changed the delay from 15 to 20. And you didn't even think to change the comments!

Rmedina4:
delay(20); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(20); // waits 15ms for the servo to reach the position
}
}

At least show us that you've tried to do something other than turn your computer on.

So this is what I have right now but I feel like this isn’t correct. I have a PC led that I’m going to tie this Arduino to so when the PC led turns on it will rotate the servo to 180 and when the LED turns off it will turn the servo the opposite direction. the LED coming from the PC turns on and off multiple times in periods of 10-15 mins. I have the wiring done I just need the code to work with it.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo


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

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  LED.attach(2); // attaches the LED on pin 2 
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    if (digitalRead(2)==1) //if HIGH
    {
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(20);
    }
                           
  } else if (digitalRead(2)==0)

  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(20);                       
  }
}

DKWatson:
I'm not usually this critical of Newbies, but all you've done is copy the sweep example from the servo library distribution, deleted the credits to Scott Fitzgerald and changed the delay from 15 to 20. And you didn't even think to change the comments!
At least show us that you've tried to do something other than turn your computer on.

@DKWatson
I state in my original post that I got the code from the Arduino pre-sample programs! I came to this forum to gather help not grief on everything I posted so if you don't want to help please stop replying to this post.

Thank you

Rmedina4:
@DKWatson
I state in my original post that I got the code from the Arduino pre-sample programs! I came to this forum to gather help not grief on everything I posted so if you don't want to help please stop replying to this post.

Thank you

And I was just about to help the guy with this. Then he shows his ass. Sorry dude, if you're more worried about perceived slights than you are with the code then why should anyone waste their time with you? If you don't like a post just ignore it. But when you start bitching about it then folks like me who can see your issue and how to fix it decide to skip over you and help someone who isn't a jerk.

So do you want help or do you just want to whine about not liking the replies you get?

I have a PC led that I'm going to tie this Arduino to so when the PC led turns on it will rotate the servo to 180 and when the LED turns off it will turn the servo the opposite direction. the LED coming from the PC turns on and off multiple times in periods of 10-15 mins.

Please explain how this connection is going to be made

Delta_G:
And I was just about to help the guy with this. Then he shows his ass. Sorry dude, if you're more worried about perceived slights than you are with the code then why should anyone waste their time with you? If you don't like a post just ignore it. But when you start bitching about it then folks like me who can see your issue and how to fix it decide to skip over you and help someone who isn't a jerk.

So do you want help or do you just want to whine about not liking the replies you get?

I don't mind feedback it is just, of course, I'm new to this and I'm not taking ownership of some else's work like he said I was. I don't mind someone telling me to go read this or go read that..... this person is making it seem like I'm not giving credit to where its due. Yes, I would love help as I am new and would like to get this small project complete.

UKHeliBob:
Please explain how this connection is going to be made

The connection would be in a series so when the LED gets signal from the PC it will complete the circuit to my Arduino indicating if its on or off. Essentially I’m going to make it in a switch but instead of it being a switch/relay but it will be a LED and when the LED from the PC gets powered I will have digital read to look for it. I hope this makes more sense.

Really you ought to be playing with the examples that come with the Arduino IDE so that you start to learn how to do things with code. , or study some of the tutorials or get a book .There is no point in copying some code out then asking what to do to modify it in a certain way and slapping a resulting wedge of code in . You learn nothing with such short cuts , and it will end in frustration. Take your time study the examples , modify them , understand how they work and how to debug code. - after all that’s the point isn’t it ?

Rmedina4:
The connection would be in a series so when the LED gets signal from the PC it will complete the circuit to my Arduino indicating if its on or off. Essentially I'm going to make it in a switch but instead of it being a switch/relay but it will be a LED and when the LED from the PC gets powered I will have digital read to look for it. I hope this makes more sense.

What steps are you going to take to ensure that the PC and the Arduino have a common GND connection ?

I have this code that kinda of works but it doesn’t work when there is a change.

Example of what I’m trying to do: the LED blinks on and off at different times so I want a loop that will notice if there is a change and make the servo move a certain direction depending if the LED is on or off.

Can someone help me with this?

Thank you

Here is my code:

#include <Servo.h>

Servo ser;

int pos =0;
int led = 3;

void setup(){
  pinMode(led, INPUT);
  
  ser.attach(9);
}


void loop (){


  if(digitalRead(led) == HIGH){
  ser.write(180); 
  }
  else if(digitalRead(led) == LOW){
    ser.write(30);
  }
    
 }

Change in what?

Why does your program read from a digital pin called "led"?

Please explain your project and describe what is connected to the Arduino (post a hand drawn circuit diagram).

Note that servos or motors should never be powered from the 5V Arduino pin.

This seems to be a continuation of this thread and this one.

OP: Please keep to one thread per topic that way all that information you gave us on earlier threads isn't lost.

Threads merged.

Let's go back a step.
Can you explain what each of these lines does...?

  if(digitalRead(led) == HIGH){
  ser.write(180); 
  }
  else if(digitalRead(led) == LOW){
    ser.write(30);
  }

p.s. We could remove two lines from this with no effect on the result.
What happens if you alternately touch a wire between pin 3 and ground ?
Now you need to go to a Windows programming forum - and ask how to toggle drive a TTL level (0-5V) output pin. Remember as noted above - the PC and Arduino should have their 0-V/Gnd connected common.

Rmedina4:
So this is what I have right now but I feel like this isn't correct. I have a PC led that I'm going to tie this Arduino to so when the PC led turns on it will rotate the servo to 180 and when the LED turns off it will turn the servo the opposite direction. the LED coming from the PC turns on and off multiple times in periods of 10-15 mins. I have the wiring done I just need the code to work with it.

boolean led_is_on = false;

void loop() {
  boolean led_was_on = led_is_on;
  led_is_on = digitalRead(2) == HIGH;

  if(led_was_on && !led_is_on) {
    myServo.write(0);
  }

  if(!led_was_on && led_is_on) {
    myServo.write(180);
  }
}