Run servos with push button

Hey there, Im working with a code that uses two servos, and they run inmediatly after connect the arduino to the computer, I would want they run after push a button, but I have read many tutorials but nothing work with my code, can sombody help me, I know it has to be to easy but obviously not for me.
Here is the code:

#include <Servo.h>

//Variables

Servo servoYaw, servoPitch;
char s[15];
int yawAngle, pitchAngle;
const int buttonPin = 2;
int buttonState = 0;

void setup() {

//Servo init
servoYaw.attach(10);
servoPitch.attach(11);

pinMode(buttonPin, INPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
delay(5000);
//Sweep Yaw servomotor
for (yawAngle = 0; yawAngle <= 180; yawAngle += YAW_STEP) {
servoYaw.write(yawAngle);
//Sweep Pitch servomotor. The direction depends on the current directory
if (pitchAngle < 90) {
for (pitchAngle = 0; pitchAngle <= 180; pitchAngle += PITCH_STEP) {
servoPitch.write(pitchAngle);
sendMeasurement();
}
} else {
for (pitchAngle = 180; pitchAngle >= 0; pitchAngle -= PITCH_STEP) {
servoPitch.write(pitchAngle);
sendMeasurement();
}
}
}
}

if (buttonState == HIGH)
  delay(5000);

You have a couple of braces {} missing, so the only thing controlled by that if() is the delay(). I don't think that's what you intended.

Steve

Hi, thank for the reply I have done like this but nothing,with out the button start inmediatly but with the button is doig nothing:Ive place some brackets:

void loop() {

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
delay(5000);
//Sweep Yaw servomotor
for (yawAngle = 0; yawAngle <= 180; yawAngle += YAW_STEP) {
servoYaw.write(yawAngle);
//Sweep Pitch servomotor. The direction depends on the current directory
if (pitchAngle < 90) {
for (pitchAngle = 0; pitchAngle <= 180; pitchAngle += PITCH_STEP) {
servoPitch.write(pitchAngle);
sendMeasurement();
}
} else {
for (pitchAngle = 180; pitchAngle >= 0; pitchAngle -= PITCH_STEP) {
servoPitch.write(pitchAngle);
sendMeasurement();
}
}
}
}
}

Im not sure if I am controlling both cycle after for an if.

Please post a complete program. One that actually compiles.

I also suggest you write down in simple steps EXACTLY what you are expecting to happen. E.g.

  1. When button is pressed
  2. Do nothing for 5 seconds
  3. Then move servoYaw (how is it supposed to move?)
  4. Then do something with servoPitch (but because you haven't posted the complete program I have no idea what)

What is supposed to happen if the button is released part way through all this activity?

Also how is your button wired? Do you have a pull down resistor on it?

Note that because of that delay(5000) it will not be very responsive because it only checks the button every so many seconds.

Steve

This is the complete code, with out the button, this code runs in a perfect way, but I would like to do it with a button:

  1. Do nothing
  2. Push button, then Servoyaw go to a position, and servopitch begin to sweep, because the lidar is taking measurements
  3. Hopefully I could press the button and stop everything

So sorry I have spend many time but nothing.. Thank for your help

Here s the code:

#include <Servo.h>
#include <Wire.h>
#include <LIDARLite.h>

//Delay between each sample to avoid mechanical wobble
#define DELAY_BETWEEN_SAMPLES 100
//Size of the steps of YAW/PITCH in degrees (1 = full res)
#define YAW_STEP 2
#define PITCH_STEP 2
#define MATH_PI 3.1415f

//Variables
LIDARLite myLidarLite;
Servo servoYaw,servoPitch;
char s[15];
int yawAngle,pitchAngle;
int x,y,z,r;
float theta,phi;

void setup() {
// Initialize serial connection to display distance readings
Serial.begin(115200);

//Servo init
servoYaw.attach(10);
servoPitch.attach(11);

//Lidar Lite v3 init
myLidarLite.begin(0, true);
myLidarLite.configure(0);
}

void loop() {
delay(5000);
//Sweep Yaw servomotor
for (yawAngle = 0; yawAngle <= 180; yawAngle += YAW_STEP) {
servoYaw.write(yawAngle);

//Sweep Pitch servomotor. The direction depends on the current directory
if(pitchAngle < 90){
for (pitchAngle = 0; pitchAngle <= 180;pitchAngle+= PITCH_STEP){
servoPitch.write(pitchAngle);
sendMeasurement();
}
} else {
for (pitchAngle = 180; pitchAngle >= 0;pitchAngle-= PITCH_STEP){
servoPitch.write(pitchAngle);
sendMeasurement();
}
}
}
}

// Function to acquire, convert and send the measurement.
void sendMeasurement(){
delay(DELAY_BETWEEN_SAMPLES);

// Get spherical coordinates
r = myLidarLite.distance(false);
theta = (float)yawAngle * PI / 180.0f;
phi = (float)pitchAngle * PI / 180.0f;

// Convert and send them
sprintf(s,"%d %d %d\n\0",(int)(r*cos(phi)cos(theta)),(int)(rcos(phi)sin(theta)),(int)(rsin(phi)));
Serial.print(s);
}

I have wired the resistor too.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

Hi, so sorry there's the code:

#include <Servo.h>
#include <Wire.h>
#include <LIDARLite.h>

//Delay between each sample to avoid mechanical wobble
#define DELAY_BETWEEN_SAMPLES 100
//Size of the steps of YAW/PITCH in degrees (1 = full res)
#define YAW_STEP 2
#define PITCH_STEP 2
#define MATH_PI 3.1415f

//Variables
LIDARLite myLidarLite;
Servo servoYaw,servoPitch;
char s[15];
int yawAngle,pitchAngle;
int x,y,z,r;
float theta,phi;

void setup() {
  // Initialize serial connection to display distance readings
  Serial.begin(115200); 

  //Servo init
  servoYaw.attach(10);
  servoPitch.attach(11);

  //Lidar Lite v3 init
  myLidarLite.begin(0, true);
  myLidarLite.configure(0);
}

void loop() {
  delay(5000);
  //Sweep Yaw servomotor
  for (yawAngle = 0; yawAngle <= 180; yawAngle += YAW_STEP) {
    servoYaw.write(yawAngle);
    
    //Sweep Pitch servomotor. The direction depends on the current directory
    if(pitchAngle < 90){
      for (pitchAngle = 0; pitchAngle <= 180;pitchAngle+= PITCH_STEP){
        servoPitch.write(pitchAngle);
        sendMeasurement();
      }
    } else {
      for (pitchAngle = 180; pitchAngle >= 0;pitchAngle-= PITCH_STEP){
        servoPitch.write(pitchAngle);
        sendMeasurement();
      }
    }
  }
}

// Function to acquire, convert and send the measurement.
void sendMeasurement(){
  delay(DELAY_BETWEEN_SAMPLES);
  
  // Get spherical coordinates
  r = myLidarLite.distance(false);
  theta = (float)yawAngle * PI / 180.0f;
  phi = (float)pitchAngle * PI / 180.0f;

  // Convert and send them
  sprintf(s,"%d %d %d\n\0",(int)(r*cos(phi)*cos(theta)),(int)(r*cos(phi)*sin(theta)),(int)(r*sin(phi)));
  Serial.print(s);
}

Hope somebody can help me

Hope somebody can help me

Start by answering ALL the questions in reply #3.

Thanks for posting the code properly.

And, as @jremington has said, you need to answer the questions so we can get a clear understanding of the problem and the requirement.

Answering the questions may also help you to find a solution.

...R

Here some answers,

  1. In this moment by connecting the arduino the program start immediately
  2. I would like to press a button to start both servos and the measurements, it is not necessary the delay then
  3. Finally same or another button (I think different one is better) can stop the program, what I mean is to control when to start and stop everything as a switch
  4. I have wired the button, one leg to the possitive, another to a resistor and same to ground

Thanks to all

You can have a variable (let's call it allowMovement) which starts as false and which is set to true when the GO button is pressed and which is set to false when the STOP button is pressed. Then much of the code could be inside an IF clause

if (allowMovement == true) {
    // code

}

...R

void loop() 
{

  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH)
  {
    yaw_Angle();
    delay(5000);
  }
  else
  {
  }

}

*makes your for function as void yaw_Angle()