I am trying to make a servo spin at the push of a button. When I set up the breadboard, my Arduino turns on through a USB connection only when my servo is not connected to the same 5v and GND as the button. I can power the servo with a battery and the button with USB, but my code doesn't work in that situation anyways. So I guess I have 2 problems here...
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
const int buttonPin = 2;
int pos = 0; // variable to store the servo position
int buttonState = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
for(pos = 0; pos < 180; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 10 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=10) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
tbrant16:
When I set up the breadboard, my Arduino turns on through a USB connection only when my servo is not connected to the same 5v and GND as the button. I can power the servo with a battery and the button with USB, but my code doesn't work in that situation anyways. So I guess I have 2 problems here...
This suggests 2 things to me.
The servo draws too much current from the Arduino. Arduinos generally should not be used to power servos or motors.
When you have the servo powered by battery you have forgotten to connect the servo ground with the Arduino ground.
And just possibly the battery doesn't provide enough current.
Hi. i tried to do the same thing here. The codes shows no error but my servo does'nt work as the codes tells. why is that?
the led works perfect but the servo does'nt work. Help me solve my problem. thanks.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int ledPin = 13;
int buttonPin = 2; // The button will be on Pin 2
int buttonState = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(2, HIGH);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
digitalWrite(ledPin, LOW);
myservo.write(140);
delay(10);
}
else
{
digitalWrite(ledPin, HIGH);
myservo.write(0);
delay(10);
}
}
#include <Servo.h>
Servo myservo; // creating myservo object
int buttonPin = 2; // set button pin
int buttonState = 0; // set buttonState
void setup()
{
myservo.attach(9); // attach the 13 pin to servo
pinMode(buttonPin, INPUT); // set button to input
}
void loop()
{ buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button if (buttonState == HIGH)
if (buttonState == HIGH)
{
myservo.write(0);
}
else
{
myservo.write(180);
}
}
too, but still nothing happen.
i tried the example code, it works perfect but not with my code.
The simplest way is to use pinMode(buttonPin, INPUT_PULLUP); which means that the the button pin normally reads HIGH. Then you wire the switch between the pin and ground. When you press the switch the pin will read LOW.
The other thing is that your program has no means to wait while the servo moves. For testing add delay(500); just before the end of loop().
My switch works fine.
yet, the servo does'nt work.
#include <Servo.h>
Servo myservo; // creating myservo object
int buttonPin = 2; // set button pin
int buttonState = 0; // set buttonState
int LEDpin = 13;
void setup()
{
pinMode(LEDpin, OUTPUT);
myservo.attach(9); // attach the 13 pin to servo
pinMode(buttonPin, INPUT_PULLUP); // set button to input
}
void loop()
{ buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button if (buttonState == HIGH)
if (buttonState == HIGH)
{
digitalWrite(LEDpin, LOW);
myservo.write(140);
delay(500);
}
else
{
digitalWrite(LEDpin, HIGH);
myservo.write(0);
delay(500);
}
}