Hello! For a school project, I am making a camera slider, using 3 nema 17 motors and an arduino uno. Its going to be controlled trough an MIT app inventor application.
One motor is for the sliding, one is for the pan and one is for the tilt of the camera. I am using the AccelStepper library for them.
Currently i am just making a crude version of the program, which will have a button for the slider to perform a movement to the left ( as you can see, i still have to polish the movements it does, but this is just a test version ), one to the right and a stop button.
I want it to work so that it can remember its position. For example, if i randomly press STOP while its still performing the right movement , it should know where it left off, so if i then press the right button again, it will do the remainder of the steps to that position ( or to the left, if i pressed the left button) .
In order for the program to achieve that, i wrote currentPosition = stepper.currentPosition() for each motor under the loops, does that work?
I am so sorry if this is confusing, i am very unexperienced at this, but thank you so much for any insights!!!!
#include <AccelStepper.h>
#include <SoftwareSerial.h> // library for bluetooth communication
// Define the stepper motors and the pins the will use
AccelStepper stepper1(1, 7, 6); // Stepper for sliding
AccelStepper stepper2(1, 5, 4); //Stepper for pan
AccelStepper stepper3(1, 3, 2); //Stepper for tilt
#define RX_PIN 0
#define TX_PIN 1
#define limitSwitchLeft A1
SoftwareSerial bluetoothSerial(0, 1); // RX, TX
String command; // String for storing received commands
// Declare the global variables for storing the current positions of the steppers
long currentPosition1 = 0;
long currentPosition2 = 0;
long currentPosition3 = 0;
void setup() {
// Set initial seed values for the steppers
stepper1.setMaxSpeed(3000);
stepper1.setSpeed(200);
stepper2.setMaxSpeed(3000);
stepper2.setSpeed(200);
stepper3.setMaxSpeed(3000);
stepper3.setSpeed(200);
// Move the slider to the initial position - homing
while (digitalRead(limitSwitchLeft) != 0) { //loop works until limit switch activated
stepper1.setSpeed(3000); //movement speed
stepper1.runSpeed(); // execute movement
stepper1.setCurrentPosition(0); // When limit switch pressed set position to 0 steps
stepper2.setCurrentPosition(0);
stepper3.setCurrentPosition(0);
}
}
void loop() {
if (bluetoothSerial.available()) {
String command = bluetoothSerial.readString();
if (command == "right") { //if it recevies "right" it will execute this program
stepper1.setMaxSpeed(3000);
stepper1.setSpeed(3000);
stepper1.moveTo(60000);
stepper2.setMaxSpeed(2000);
stepper2.setSpeed(2000);
stepper2.moveTo(4000);
stepper3.setMaxSpeed(1000);
stepper3.setSpeed(1000);
stepper3.moveTo(2000);
currentPosition1 = stepper1.currentPosition(); // memorising position ( not sure if works)
currentPosition2 = stepper2.currentPosition();
currentPosition3 = stepper3.currentPosition();
}
else if (command == "left") { // if it recevies "left", it will execute this program
stepper1.setMaxSpeed(3000);
stepper1.setSpeed(3000);
stepper1.moveTo(-60000);
stepper2.setMaxSpeed(2000);
stepper2.setSpeed(2000);
stepper2.moveTo(-4000);
stepper3.setMaxSpeed(1000);
stepper3.setSpeed(1000);
stepper3.moveTo(2000);
currentPosition1 = stepper1.currentPosition();
currentPosition2 = stepper2.currentPosition();
currentPosition3 = stepper3.currentPosition();
}
}
else if (command == "stop") { // if it recevies "stop" it will stop the execution of "left" or "right"
stepper1.stop();
stepper2.stop();
stepper3.stop();
currentPosition1 = stepper1.currentPosition();
currentPosition2 = stepper2.currentPosition();
currentPosition3 = stepper3.currentPosition();
}
}