servo coding

i am using a 360 continuous servo for my coursework it is connected to a button. now when the button is pressed i want the servo to stop and start again when the button is released. i am good at building with arduino not so much the coding side

here is what i have (using an example from arduino)

#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
}

How is the button connected to the servo?
What kind of button?

Please use code tags when posting code.

apologies i am using a simple push button i used this previously with a 180 servo and worked welll but i want to recreate this feature with the 360 one

apologies i am using a simple push button

Normally open?
Normally closed?

I still don't know how it is connected to the servo.

the servo when the programme runs starts to spin so the button is closed when i press it the button is open and i want the servo to stop instead od spinning the opposite direction like it currently does. the servo is connected to the button via the ground and 5v on the breadboard

Why not connect the button to the Arduino instead and use software to control the servo?

i found this way previously easier when working with the 180 servo. i wouldnt be sure how to hook the button up to the ardunio then as i say software isnt my strongpoint so wouldnt be sure of the next steps

could i not use an if statement to say when the button (A0) is off servo moves when the button is on (pressed) the servo stops

could i not use an if statement to say when the button (A0) is off servo moves when the button is on (pressed) the servo stops

Yes, but you will need to connect the servo and the button to the Arduino but you seem to be reluctant to do that

ive been an idiot whilst ive been replying the button is already connected to the arduino via A0.

only noticed this when i was looking over other things sorry

Remember though, that when you do a servo write with a proper servo, that value is the position between 0 and 180.

With a continuous servo*, that value is now the speed and direction of movement, with 90** being stationary, 0 being full speed one way, and 180 full speed the other. Other values give speeds between 0 and full.

  • At least one member here argues that that's no longer a servo 8)

** May not be exactly 90 apparently... might need to try values around 90

i am using a 360 continuous servo for my coursework it is connected to a button. now when the button is pressed i want the servo to stop and start again when the button is released. i am good at building with arduino not so much the coding side

The below may be close to what you want. Bottom is some servo test code where you can find the us value for having the servo stop (detaching the servo is another option).

//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.writeMicroseconds(1000);
  }
  else {
    servo1.writeMicroseconds(1500);
  }
}

Servo test code:

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

no luck :frowning: when it comes to code im clueless

no luck smiley-sad when it comes to code im clueless

If you don't post your code, so are we.

when it comes to code im clueless

Time to switch to another course perhaps ?

coding is something we havent really been taught to get into. pretty much we were told the build is more important and tht most code we can find online.

I may be a bit late to this party, but ... in your original post you state you want to use a push button to control on/off of the servo, yet you have not code referencing any push button. Or perhaps that's your point ... you don't know where/how to do it?

Taking zoomkat's example and perhaps just simplifying it, just do digital read on a pin with the pushbutton connected. When/while the button is LOW then run your existing code to read the pot, map, and move servo.

You might need to wire a pull-down on the button to make sure its low when not pushed; or try zoomkats trick of writing high to the pin (never done that myself).

Ex.

#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 
int button1 = 4;
 
void setup() 
{ 
  pinMode(button1, INPUT);

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  if (digitalRead(button1) == LOW)  //if button is not pressed then read pot and run servo
  {
    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 
  }
}

most code we can find online.

It is true that there is plenty of code online but rarely will it do exactly what you want, hence the need to do some coding yourself despite what your teachers might say.

UKHeliBob:

most code we can find online.

It is true that there is plenty of code online but rarely will it do exactly what you want, hence the need to do some coding yourself despite what your teachers might say.

To emphasize UKHeliBob's sentiment, not only will the code rarely do exactly what you want, you need to be able to understand code to be able to even find code close to what you want. This teacher is doing you and your class a disservice with his attitude.

How many software engineers does it take to screw in a light bulb? None, it's a hardware problem Unfortunately, I've often seen the reciprocal attitude.

i really want to learn the code. the build is pretty much complete just the coding to do. i have enjoyed this and would like to understand it further.

here is what i have in the build. 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 5v resistor which connects the ground to the button as the 5v is connected already. i have added an lcd which will change value also when the button is pressed it will display new text everytime the button is pressed/released