led and servo

Hi All, as previously stated I am very new to this, so this is what I am trying to do is :

when I push the button once the led stays on I push the button again the led goes off I would like to add a servo to this so when I push the button the first time the led and the servo goes on the servo I would like to continue to rotate from 0 to 180 until I push the button again to stop it here is the code I am trying to use

int buttonPin = 7; // the number of the pushbutton pin
int ledPin = 13; // the number of the LED pin
boolean on=false;
int buttonState = 0;
#include <Servo.h>
Servo myservo;
int bt = 0;
int val;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
myservo.attach(9);

}

void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if(on==true){
on=false;
val = analogRead(bt);
val = map(val, 0, 1023, 0, 180);
myservo.write(val);
delay(15);

} else{
on=true;
}
}

if (on == true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}

delay(500);

}e

am I on the right track???

Pat

pjf610:
am I on the right track???

That is easy to find out. Upload it and see what happens.

Oh, can you go edit your previous post? Highlight the code and hit the code button </>
That wraps the code in code tags that make it much easier to import into the IDE. (and gives you better help with your questions)

Hi and thanks for the reply I was able to upload the code ok the led goes on and off but the servo does not respond like I said I am very new to this and much of the jargon is some what strange to me. I am sorry for my lack of knowledge please have patience's

Pat

What do you have connected to pin A0? You are using the voltage on that pin to control the servo position.

If you want the servo to move only when the LED is ON you should probably move the code that moves the servo to the place in the sketch where the LED is turned ON.

OK I will try to figure that out. What it is that I am trying to make is a servo tester this is only the first step but not being knowledgeable on this I hope I can figure it out before I drop(LOL). What I would like to add down the line is a button to center the servo and add a LCD. I know baby steps first, but I am not going to give up hopefully with help from this site I can get it figured out.

Many Thanks
Pat

The 'servo tester' part would look like this:

const byte ServoPin = 9;
const byte ButtonPin = 7;
const byte PotPin = A0;
Servo servo;

void setup() {
    pinMode(ButtonPin, INPUT_PULLUP);
    servo.attach(ServoPin,1000, 2000);
}

void loop() {
    servo.writeMicroseconds(map(analogRead(PotPin), 0, 1023, 1000, 2000));
}

To add the centering button:

void loop() {
    if (digitalRead(ButtonPin))
        servo.writeMicroseconds(map(analogRead(PotPin), 0, 1023, 1000, 2000));
    else
        servo.writeMicoseconds(1500);
}

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Tom... :slight_smile:

I have used servo testers and they were useful if a bit thin on the functionality side.

I have been thinking what I would want if I were to put one together. I have experience with servos in the RC aircraft hobby and with using then on arduino projects. These features apply to both.

UI
scroll wheel - finer control than a pot
button(s) - for selecting menu items
LCD - useful for describing menu modes and displaying servo position info. Let me toggle between degrees and μs.

Multiple modes
Center servo - useful for setting up an aircraft since this usually correlates to neutral stick position.
Go full left
Go full right - useful for testing limits of travel
End point adjustment using scroll wheel
Sweep (using end points)
user position - use the scroll wheel to move the servo, helpful for getting the μs or degrees info for a specific servo position

Most of this is just my ramblings on how I think it should behave. It has been shown many times that my expectations have little to do with the real world. Use my advice with caution.

Hi all, I do appreciate all your help, I know I will have much more questions on this and I do not want to burden anyone with my inexperience, but all your help is appreciated one question I do have is the code you sent me all I have to do is add it to what I have already correct??

Pat

Hi John, I have not connected the servo to the A0 pin, this is the next problem I am having which place does the servo wires go to???

Pat

Hi,
I think you need this schematic to start with.

Tom... :slight_smile:

Many thanks I will try this when I return back home on Mon.

Pat

Hi Tom, thanks for the reply I am a bit confused in the schematic you show the VR1 and the switch I am confused because is the VR1 my button?? then what is the switch for??

Pat

Hi,

VR1 my button??

You analogRead the VR1 at A0.

You digitalRead your buttonpin at pin 7. (SW1)

Using your code in the first post.

Tom.... :slight_smile:

Thank You Tom

Pat