Hi there, I am trying to control a robot arm using the following code.
I would like to be able to record 2 position and then loop the movement between them on pressing a button. It would normally react to my potentiometers, but as I would press a button, it would record one position, i would move the servos with potentiometers again and record second position. When i would press second button, the servos would move from first position to the other in a loop.
I would appreciate any ideas, thanks!
/*
PCA9685 PWM Servo Driver Example
pca9685-servomotor-demo.ino
Demonstrates use of 16 channel I2C PWM driver board with 4 servo motors
Uses Adafruit PWM library
Uses 4 potentiometers for input
DroneBot Workshop 2018
https://dronebotworkshop.com
*/
// Include Wire Library for I2C Communications
#include <Wire.h>
// Include Adafruit PWM Library
#include <Adafruit_PWMServoDriver.h>
#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define FREQUENCY 50
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// Define Potentiometer Inputs
int controlA = A0;
int controlB = A1;
int controlC = A2;
int controlD = A3;
// Define Motor Outputs on PCA9685 board
int motorA = 0;
int motorB = 4;
int motorC = 8;
int motorD = 12;
void setup()
{
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
}
void moveMotor(int controlIn, int motorOut)
{
int pulse_wide, pulse_width, potVal;
// Read values from potentiometer
potVal = analogRead(controlIn);
// Convert to pulse width
pulse_wide = map(potVal, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
//Control Motor
pwm.setPWM(motorOut, 0, pulse_width);
}
void loop() {
//Control Motor A
moveMotor(controlA, motorA);
//Control Motor B
moveMotor(controlB, motorB);
//Control Motor C
moveMotor(controlC, motorC);
//Control Motor D
moveMotor(controlD, motorD);
}