servo coding issue

i have a 360 servo hooked up to a push button with the hope that it spins continuously until i press the button. i have attemped numerous ways to do this with no luck.

would an if statement work i am good at building the device but when it comes to coding it is my downfall.

i am currently using the example code in the arduino programme (knob) to make it spin but i need to make it stop when the push button is pressed (on) and re-start when it is not (off).

i used this code with a 180 servo to success but have had no luck with the 360

It sounds like you ‘stop’ pulse width value for the 360° servo may need adjustment in your code.

Is this 360° servo one you bought or modified from a 180° servo?

i purchased it online from amazon. currently the button it is connected to goes through a0 and the servo goes through pin 9 on the arduino

Post the link to your servo (or one like it).

Post your code in code tags (highlight code and hit “code” button in editor).

One of us will check out your code and see if we can’t help ya.

click on to open and enlarge

http://www.amazon.co.uk/gp/product/B004 … b_prodpg_1

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
}

Here is a code that you can try. Once you press the button, the servo will move in one direction and after 2 seconds, move in the other. You can build on this code to make it do what you want.

#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(180);
    delay(2000);
    servo1.write(1);
  }
}

i have tried using it and changed thed value of the servo to the correct pin it is connected to. it will not do anything however

How do you have it wired? (including power)

the button goes is connected to the arduino via A0 the servo goes through pin 9. the power is connected to 5v and the ground to ground. the servo is connected to the power,ground and the 9 pin the button connects to a transistor which conencts the ground to the button (cant remember the voltage) as the 5v is connected already.

the idea im making is i have a lcd and the servo connected to the button (A0) when the button is pressed the servo stops and a message is displayed on the lcd when i release the button the servo restarts and a new message comes on the lcd and so on in a repetetive loop. any advice for that

tyson642:
the button connects to a transistor which conencts the ground to the button (cant remember the voltage) as the 5v is connected already.

The above connection is not clear. What is connected to the base, collector and emitter of the transistor ?

Would you be willing to make it simpler ? Get rid of the transistor and add a pull-up resistor connecting pin A0 to 5V. Then add the button, a momentary normally open button, between A0 and ground. When the button is not pressed, A0 will have 5V on it due to the pull-up resistor. When the button is pressed (and held) it will have 0V because the button is tying A0 to ground.

An even simpler route would be to use A0, or another digital pin, as an input and enable the internal pull-up resistor. The read the pin as either a HIGH or LOW and set the servo speed to 0 or 179 based on that. This is similar to what codlink posted above.

(see input_pullup)

http://arduino.cc/en/Reference/Constants

you are correct i meant to say resistor not transistor my mistake. the rest is the same though. the item is built its just getting the coding done

OK, Try running this mod of your code. I added some simple prints to verify that the switch is being read and interpreted correctly. Open your serial monitor, set baud to 9600 and see that val changes properly when you push the button.

#include <Servo.h>

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

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

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

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  Serial.print("Val = ");
  Serial.println(val);
  delay(15);                           // waits for the servo to get there
}

yes it is reading the button correctly. seems a bit slower now to make it stop when button is pressed as it still goes other way

tyson642:
yes it is reading the button correctly. seems a bit slower now to make it stop when button is pressed as it still goes other way

The code to "print" to the serial monitor takes perhaps 10 msec so the loop will run slower than before. I don't understand the underlined above. Your 1'st post made it sound like the servo wasn't moving at all. The above post makes it sound like it's moving and even stops when the button is pushed ? Or does it go full speed in one direction and then go full speed in the reverse direction when the button is pushed ? I'd expect the latter as the Arduino code gives the command from 0 to 180 instead of the more usual (to me) -90 to +90. If the servo is running then you need to command 90 deg (give or take) to make it stop.

And I wonder if, to get a real, no movement at all stop, you’ll need to do a servo.detach() when the button is pushed. And then do the .attach() and .write() when the button is released ?

You are correct when the button is pressed it begins to spin in the other direction/reverse and the same when the button is released

So try commanding 90 deg (instead of 0) when the button is pushed. That should be pretty close to a stop.

thats the first part working well done thanks for that.

now wondering if you can help with the 2nd. now when the button is pressed i want the servo to stop (which it does now :smiley: )

the lcd i have installed now displays new text when the button is pressed/released.)

i have set up the lcd as shown on the arduino site arduino.cc/en/Tutorial/LiquidCrystal

What code do you have now that writes to the LCD ?