controlling 2 servos and button with uno and PCA9685

hey so hopefully i can get some input soon as im already so behind on this project.

i’m trying to control two servos with a PCA9685 servo driver which is a pretty standard hook up for servos.

i’m able to successfully run two servos using a “for loop” however when i try to input a button, just a simple, “if the button is pressed the servos rotate from 0-180 and back”

this is proving to be very difficult, i thought this would be easier… i’ve tried like everything and i just don’t know what to do anymore

my code is here, but i know its probably completely wrong so if anyone has any advice i would be forever grateful!!

update: I tagged my code i believe?
i’m simply trying to initialize a button that controls two servo motors at the same time using a servo driver. I just want the servos to rotate 180 degrees and back while being able to control how fast they rotate.when the button is held the servos move from 0-18 and 180-0 when the button isn’t pressed they retreat to an original position.

//control servo motors with one button using  servo driver 

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  570 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
const int servonum1 = 0; //pin zero on driver
const int servonum2 = 3; //pin three on driver 





const int buttonPin = 7; //controls both servos
int buttonState = 0; //controls state of the button(on or off)


void setup() {
 pwm.begin();
 pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  
  //intitializes the button as the input
  pinMode(buttonPin,INPUT);

 //initializes the servos as outputs 
 pinMode(servonum1,OUTPUT);
 pinMode(servonum2,OUTPUT);

 //serial values
 Serial.begin(9600);
}


void loop() {
  buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH){
    motor_on();
    }
    else{
      motor_off();
    }
  
}





void motor_on() {
  digitalWrite(servonum1, HIGH); //servos are on
  digitalWrite(servonum2, HIGH); //servos r on


 for (int angle =181; angle>0; angle+=-3){
   delay(45);
  //control the motors 
  pwm.setPWM(0, 0, angleToPulse(angle) );
  pwm.setPWM(3, 0, angleToPulse(angle) ); 
}
 
  //mapping 0-180 to the servos values 
  int angleToPulse(int ang){
  int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX);
  return pulse;
}



}

void motor_off(){
  digitalWrite(servonum1, LOW); //servos are off
  digitalWrite(servonum2, LOW); //servos r off
}

maybe_works_test_5.ino (1.61 KB)

Please post your code.
In code tags

Edit: My original post was intended to be a reply to a PM.

For content on this forum, my advice starts with a +1 for AWOL's request for use of code tags.
The second piece of advice is to make sure we understand what your goal is.
Are you trying to make a sketch that sits and waits for a button press. When it detects one, it sweeps the servo from 0 to 180 and back, and then it starts looking for button presses again?

hey i just updated my origional post hopefully my objective makes more sense now!

UPDATE!
I have figured out how to control the angle with a button for both servos, however i cant adjust the speed or sweep the motors from 0 to 180 and 180 to 0.

here is my revised code

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

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN 125
#define SERVOMAX 570

uint16_t servonum;
const int servoNUM1 = 0;
const int servoNUM2 = 3;

int angle = 0;
int anglespeed = 0;//trying to figure out how to control speed

const int buttonpin = 7;
int buttonState =0;

void setup(){
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60);
pinMode(buttonpin, INPUT);

pinMode(servoNUM1, OUTPUT);
pinMode(servoNUM2, OUTPUT);
}

void loop(){
buttonState = digitalRead(buttonpin);

if (buttonState == HIGH) {
digitalWrite(servoNUM1, HIGH);
//(angle =181, angle>0, angle+=3){
//delay(45);
pwm.setPWM(0, 0, angleToPulse(180) );
pwm.setPWM(3, 0, angleToPulse(180) );
delay(50);
pwm.setPWM(0, 0, angleToPulse(0) );
pwm.setPWM(3, 0, angleToPulse(0) );
delay(50);
}

else if (buttonState == LOW){
digitalWrite(servoNUM1, LOW);

// for(angle =181; angle>0; angle+=-3)
// delay(45);
pwm.setPWM(0, 0, angleToPulse(90));
pwm.setPWM(3, 0, angleToPulse(90));
}

}

int angleToPulse(int ang){

int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX);
return pulse;

}
de


alright if anyone is wondering, i did just solve my own problems, thanks to a couple other useful codes out there !

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

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN 125
#define SERVOMAX 570

uint16_t servonum =0;
const int servoNUM1 = 0;
const int servoNUM2 = 3;

int angle = 0;

const int buttonpin = 7;
int buttonState =0;

void setup(){
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60);
pinMode(buttonpin, INPUT);

}

void setServoPulse(uint16_t n, double pulse) {
double pulselength;

pulselength = 1000000; // 1,000,000 us per second
pulselength /= 60; // 60 Hz
pulselength /= 4096; // 12 bits of resolution
pulse *= 1000;
pulse /= pulselength;
pwm.setPWM(n, 0, 0);
}

void loop(){
if(digitalRead(7) == HIGH){
for(uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++){
if (digitalRead(7) == HIGH){
pwm.setPWM(0, 0, pulselen);
pwm.setPWM(3, 0, pulselen);
delay(10);
}
}
}
if (digitalRead(7) == HIGH){
for(uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen–){
if (digitalRead(7) == HIGH){
pwm.setPWM(0, 0, pulselen);
pwm.setPWM(3, 0, pulselen);
delay(10);
}
}
}

if (digitalRead(7) == LOW){
uint16_t pulselen = SERVOMIN;
if (digitalRead(7) == LOW){
pwm.setPWM(0, 0, pulselen);
pwm.setPWM(3, 0, pulselen);
delay(10);
}

}
}

Glad you solved your problem! Maybe you can help me with mine; I'm using the following code...simple, but exactly what I need.

The only problem is that, right now, the servos move in a loop. I want them to execute their movements once each, in order, and only on a button press.

What I need is the button code and any hints you've got on how to wire that button.

Much appreciated in advance! Thanks!

JG