Hi, this is my first post, I've read up on the guidelines but if i do anything wrong such as code tags or would be better asking this question in a better place please let me know.
So I'm currently working on a project to design a 6DOF robotic arm that has sensors to avoid obstacles, and whilst all the circuits and individual codes for reading the inputs work, I'm struggling to work with the motors specifically accelStepper library. The aim of this code is to read the signal of a potentiometer connected to a step down gear attached to the stepper motor - with calibration I should be able to read the actual current angle of the gear not the motor. then I aim to set a desired angle and calculate the error value, then simply move the stepper motor an amount of steps to minimize this error as close to 0 as possible
Here is my code so far
// Include the AccelStepper Library
#include <AccelStepper.h>
// Define pin connections
const int dirPin = 7;
const int stepPin = 6;
// Define motor interface type
#define motorInterfaceType 1
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
//----------------------------------------------------------------- angle sensor values
float sensorValue4 = 0; // raw data sensor value used for angle measurement
float currentAngle4 = 0; // the current angle value measured by the potentiometer
float desiredAngle4 = 180; // this is the desired angle of the joint (remember that a gear ratio of 1-4 is used) ---- NEW
float angleDifference4 = 0; // this is the difference between the current angle value and desired angle - stepper needs to move this much
int stepsNeeded4 = 0; // this is the value in steps needed to reach desired angle
//-----------------------------------------------------------------
void setup() {
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(3000);
myStepper.setAcceleration(50);
myStepper.setSpeed(100);
// myStepper.moveTo(200);
Serial.begin(9600);
}
void loop() {
if ((myStepper.distanceToGo() != 3) && (myStepper.distanceToGo() != -3)) {
sensorValue4 = analogRead(A11); // measure input of potentiometer
currentAngle4 = ((sensorValue4/1024)*330); // calculate the current angle of gear
angleDifference4 = (desiredAngle4-currentAngle4); // calculate the error value
stepsNeeded4 = (round(angleDifference4/0.45)); // calculate steps needed to reduce error value to 0
// Serial.print("desiredAngle = "); // for debugging only - if left in - slows down motor a lot
// Serial.print(desiredAngle4);
// Serial.print("\t currentAngle4 = ");
// Serial.print(currentAngle4);
// Serial.print("\t angleDifference = ");
// Serial.print(angleDifference4);
// Serial.print("\t stepsNeeded4 = ");
// Serial.println(stepsNeeded4);
// delay (500); //allow potentiometer to settle then read new values for updating
}
myStepper.move(-stepsNeeded4); // moves the stepper motor () amount of steps (minus as potentiometer is fitted backwards)
//myStepper.runToPosition(); // Move the motor () amount of steps whilst blocking the code - cannot use due to 6 simultaneous motor operation needed
myStepper.run(); // Move the motor one step and start loop again
}
so currently when i manually change the desired angle (this will be calculated and changed automatically later on in the project) the stepper motor does gradually accelerate like it suppose to, it starts suddenly but it does decelerate to a stop, the other problem i'm having is i cant seem to avoid reading the sensors every loop, I'd like to only read the sensors once the angle error is 0 and the motor has reached its destination
the components I have are
nema17 stepper motor - Stepper motor - NEMA 14 (35mm)
a4988 DRIVER
A 5v and 12v power supply and an Arduino mega, all the wiring is correct as I've ran other example codes which work perfectly - so I think its more my logic and lack of programming skills which is the error - any help on this project would be greatly appreciated, thank you.