Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AccelStepper.h>
- #include <MultiStepper.h>
- #include <LiquidCrystal_I2C.h>
- // Define the stepper motor and the pins that is connected to
- AccelStepper stepper1(1, 2, 5); // (Type of driver: with 2 pins, STEP, DIR)
- AccelStepper stepper2(1, 3, 6);
- MultiStepper steppers;
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- int xpin = A0;
- int ypin = A1;
- int v1pin = A2;
- int v2pin = A3;
- int homepin = 10;
- int Apin = 9;
- int Bpin = 8;
- int SETpin = 7;
- int timelapseMode = 11;
- int autoMode = 12;
- const int longPress = 600;
- int v1;
- int v2;
- int v;
- bool vSET; //true: v1 - false: v2
- bool isMoving = true;
- bool page = true;
- int mode; //1: live - 2: auto - 3: timelapse
- int xA;
- int xB;
- int yA;
- int yB;
- long timeBetweenPhotos;
- long totalPhotos;
- long photosTaken;
- int xStep;
- int yStep;
- long gotoposition[2];
- void setup() {
- pinMode(homepin, INPUT_PULLUP);
- pinMode(Apin, INPUT_PULLUP);
- pinMode(Bpin, INPUT_PULLUP);
- pinMode(SETpin, INPUT_PULLUP);
- pinMode(timelapseMode, INPUT_PULLUP);
- pinMode(autoMode, INPUT_PULLUP);
- // Set maximum speed value for the stepper
- stepper1.setMaxSpeed(1000);
- stepper1.setAcceleration(1000);
- stepper1.setCurrentPosition(0);
- stepper2.setMaxSpeed(1000);
- stepper2.setAcceleration(1000);
- stepper2.setCurrentPosition(0);
- steppers.addStepper(stepper1);
- steppers.addStepper(stepper2);
- lcd.init();
- lcd.backlight();
- lcd.setCursor(2, 0);
- lcd.print("Camera Robot");
- delay(1000);
- lcd.clear();
- }
- void loop() {
- //// LIVE mode ///////////////////////////////////////////////////////////////////////////
- if(digitalRead(timelapseMode) == 1 && digitalRead(autoMode) == 1) {
- if(mode != 1) {
- delay(500);
- if(digitalRead(timelapseMode) == 1 && digitalRead(autoMode) == 1) {
- mode = 1;
- isMoving = true;
- lcd.clear();
- lcd.setCursor(2,0);
- lcd.print("-LIVE MODE-");
- delay(1000);
- lcd.clear();
- }
- }
- v1 = map(analogRead(v1pin), 0, 1023, 0, 99)*10;
- v2 = map(analogRead(v2pin), 0, 1023, 0, 99)*10;
- //select speed with A and B buttons
- if(digitalRead(Apin) == 0) {
- vSET = true;
- lcd.setCursor(0,0);
- lcd.print(">");
- lcd.setCursor(0,1);
- lcd.print(" ");
- }
- if(digitalRead(Bpin) == 0) {
- vSET = false;
- lcd.setCursor(0,0);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(">");
- }
- if(vSET == true) {
- v = v1;
- }
- if(vSET == false) {
- v = v2;
- }
- //home motors with button
- if(digitalRead(homepin) == 0 && !digitalRead(SETpin) == 0 && (stepper1.currentPosition() != 0 || stepper2.currentPosition() != 0) ) {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Going to HOME...");
- lcd.setCursor(0,1);
- lcd.print("X 0");
- lcd.setCursor(9,1);
- lcd.print("Y 0");
- isMoving = true;
- stepper2.setMaxSpeed(v);
- stepper1.setMaxSpeed(v);
- gotoposition[0] = 0;
- gotoposition[1] = 0;
- steppers.moveTo(gotoposition);
- steppers.run();
- while(steppers.run() == true && digitalRead(SETpin) == 1) {
- steppers.run();
- }
- stepper2.setMaxSpeed(1000);
- stepper1.setMaxSpeed(1000);
- }
- //set new home position
- if(digitalRead(homepin) == 0 && digitalRead(SETpin) == 0) {
- isMoving = true;
- stepper2.setCurrentPosition(0);
- stepper1.setCurrentPosition(0);
- delay(longPress);
- }
- //move camera using the joystick
- if(analogRead(xpin) > 600){
- stepper1.setSpeed(map(analogRead(xpin), 600, 1023, 0, v));
- stepper1.runSpeed();
- isMoving = true;
- }
- if(analogRead(xpin) < 400){
- stepper1.setSpeed(map(analogRead(xpin), 400, 0, 0, -v));
- stepper1.runSpeed();
- isMoving = true;
- }
- if(analogRead(ypin) > 600){
- stepper2.setSpeed(map(analogRead(ypin), 600, 1023, 0, v));
- stepper2.runSpeed();
- isMoving = true;
- }
- if(analogRead(ypin) < 400){
- stepper2.setSpeed(map(analogRead(ypin), 400, 0, 0, -v));
- stepper2.runSpeed();
- isMoving = true;
- }
- //update display only when the motors are stopped, to avoid overloading the Arduino
- if(analogRead(xpin) > 400 && analogRead(xpin) < 600 && analogRead(ypin) > 400 && analogRead(ypin) < 600) {
- stepper1.setSpeed(0);
- stepper2.setSpeed(0);
- lcd.setCursor(1,0);
- lcd.print("A");
- lcd.setCursor(3,0);
- lcd.print(v1);
- lcd.setCursor(1,1);
- lcd.print("B");
- lcd.setCursor(3,3);
- lcd.print(v2);
- lcd.setCursor(7,0);
- lcd.print("|");
- lcd.setCursor(7,1);
- lcd.print("|");
- if(v1 < 100) {
- lcd.setCursor(5,0);
- lcd.print(" ");
- }
- if(v2 < 100) {
- lcd.setCursor(5,1);
- lcd.print(" ");
- }
- if(vSET == true) {
- lcd.setCursor(0,0);
- lcd.print(">");
- lcd.setCursor(0,1);
- lcd.print(" ");
- }
- if(vSET == false) {
- lcd.setCursor(0,0);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(">");
- }
- if(isMoving == true) {
- lcd.clear();
- lcd.setCursor(9,0);
- lcd.print("X");
- lcd.setCursor(11,0);
- lcd.print(stepper1.currentPosition());
- lcd.setCursor(9,1);
- lcd.print("Y");
- lcd.setCursor(11,1);
- lcd.print(stepper2.currentPosition());
- isMoving = false;
- }
- }
- }
- //// AUTO mode ///////////////////////////////////////////////////////////////////////////
- if(digitalRead(timelapseMode) == 1 && digitalRead(autoMode) == 0) {
- if(mode != 2) {
- mode = 2;
- xA = 0;
- xB = 0;
- yA = 0;
- yB = 0;
- isMoving = true;
- lcd.clear();
- lcd.setCursor(2,0);
- lcd.print("-AUTO MODE-");
- lcd.setCursor(0,1);
- lcd.print("Set speed: A");
- delay(2000);
- lcd.clear();
- }
- v1 = map(analogRead(v1pin), 0, 1023, 0, 99)*10;
- v2 = map(analogRead(v2pin), 0, 1023, 0, 99)*100;
- //move camera using the joystick
- if(analogRead(xpin) > 600){
- stepper1.setSpeed(map(analogRead(xpin), 600, 1023, 0, v2));
- stepper1.runSpeed();
- isMoving = true;
- }
- if(analogRead(xpin) < 400){
- stepper1.setSpeed(map(analogRead(xpin), 400, 0, 0, -v2));
- stepper1.runSpeed();
- isMoving = true;
- }
- if(analogRead(ypin) > 600){
- stepper2.setSpeed(map(analogRead(ypin), 600, 1023, 0, v2));
- stepper2.runSpeed();
- isMoving = true;
- }
- if(analogRead(ypin) < 400){
- stepper2.setSpeed(map(analogRead(ypin), 400, 0, 0, -v2));
- stepper2.runSpeed();
- isMoving = true;
- }
- //set A and B positions
- if(digitalRead(Apin) == 0 && digitalRead(SETpin) == 0) {
- xA = stepper1.currentPosition();
- yA = stepper2.currentPosition();
- isMoving = true;
- delay(longPress);
- }
- if(digitalRead(Bpin) == 0 && digitalRead(SETpin) == 0) {
- xB = stepper1.currentPosition();
- yB = stepper2.currentPosition();
- isMoving = true;
- delay(longPress);
- }
- //go to A and B positions
- if(digitalRead(Apin) == 0 && digitalRead(SETpin) == 1 && (stepper1.currentPosition() != xA || stepper2.currentPosition() != yA)) {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Going to A...");
- lcd.setCursor(0,1);
- lcd.print("X");
- lcd.setCursor(2,1);
- lcd.print(xA);
- lcd.setCursor(9,1);
- lcd.print("Y");
- lcd.setCursor(11,1);
- lcd.print(yA);
- gotoposition[0] = xA;
- gotoposition[1] = yA;
- stepper1.setMaxSpeed(v1);
- stepper2.setMaxSpeed(v1);
- steppers.moveTo(gotoposition);
- while(steppers.run() == true && digitalRead(homepin) == 1 && digitalRead(SETpin) == 1) {
- steppers.run();
- }
- stepper1.setMaxSpeed(1000);
- stepper2.setMaxSpeed(1000);
- isMoving = true;
- }
- if(digitalRead(Bpin) == 0 && digitalRead(SETpin) == 1 && (stepper1.currentPosition() != xB || stepper2.currentPosition() != yB)) {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Going to B...");
- lcd.setCursor(0,1);
- lcd.print("X");
- lcd.setCursor(2,1);
- lcd.print(xB);
- lcd.setCursor(9,1);
- lcd.print("Y");
- lcd.setCursor(11,1);
- lcd.print(yB);
- gotoposition[0] = xB;
- gotoposition[1] = yB;
- stepper1.setMaxSpeed(v1);
- stepper2.setMaxSpeed(v1);
- steppers.moveTo(gotoposition);
- steppers.run();
- while(steppers.run() == true && digitalRead(homepin) == 1 && digitalRead(SETpin) == 1) {
- steppers.run();
- }
- stepper1.setMaxSpeed(1000);
- stepper2.setMaxSpeed(1000);
- isMoving = true;
- }
- //update display only when the motors are stopped, to avoid overloading the Arduino
- if(analogRead(xpin) > 400 && analogRead(xpin) < 600 && analogRead(ypin) > 400 && analogRead(ypin) < 600) {
- stepper1.setSpeed(0);
- stepper2.setSpeed(0);
- lcd.setCursor(0,0);
- lcd.print("VEL");
- lcd.setCursor(0,1);
- lcd.print(v1);
- if(v1 < 100) {
- lcd.setCursor(2,1);
- lcd.print(" ");
- }
- if(isMoving == true) {
- lcd.clear();
- lcd.setCursor(4,0);
- lcd.print("X");
- lcd.setCursor(5,0);
- lcd.print(xA);
- lcd.setCursor(10,0);
- lcd.print(">");
- lcd.setCursor(11,0);
- lcd.print(xB);
- lcd.setCursor(4,1);
- lcd.print("Y");
- lcd.setCursor(5,1);
- lcd.print(yA);
- lcd.setCursor(10,1);
- lcd.print(">");
- lcd.setCursor(11,1);
- lcd.print(yB);
- isMoving = false;
- }
- }
- }
- //// TIME-LAPSE mode ///////////////////////////////////////////////////////////////////////////
- if(digitalRead(timelapseMode) == 0 && digitalRead(autoMode) == 1) {
- if(mode != 3) {
- mode = 3;
- xA = 0;
- xB = 0;
- yA = 0;
- yB = 0;
- isMoving = true;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("TIME-LAPSE MODE");
- lcd.setCursor(0,1);
- lcd.print("Time: A - NR: B");
- delay(2000);
- lcd.clear();
- }
- timeBetweenPhotos = map(analogRead(v1pin), 0, 1023, 0, 20000)/200;
- totalPhotos = map(analogRead(v2pin), 0, 1023, 0, 99)*10;
- //move camera using the joystick
- if(analogRead(xpin) > 600){
- stepper1.setSpeed(300);
- stepper1.runSpeed();
- }
- if(analogRead(xpin) < 400){
- stepper1.setSpeed(-300);
- stepper1.runSpeed();
- }
- if(analogRead(ypin) > 600){
- stepper2.setSpeed(300);
- stepper2.runSpeed();
- }
- if(analogRead(ypin) < 400){
- stepper2.setSpeed(-300);
- stepper2.runSpeed();
- }
- //set A and B positions
- if(digitalRead(Apin) == 0) {
- xA = stepper1.currentPosition();
- yA = stepper2.currentPosition();
- delay(longPress);
- }
- if(digitalRead(Bpin) == 0) {
- xB = stepper1.currentPosition();
- yB = stepper2.currentPosition();
- delay(longPress);
- }
- //calculate how much the motors should move for each photo that is taken
- xStep = (xB - xA)/totalPhotos;
- yStep = (yB - yA)/totalPhotos;
- //start timelapse
- if(digitalRead(homepin) == 0 && digitalRead(SETpin) == 1) {
- photosTaken = 0;
- //go to start (A) position
- bool goneToA = false;
- gotoposition[0] = xA;
- gotoposition[1] = yA;
- stepper1.setMaxSpeed(200);
- stepper2.setMaxSpeed(200);
- steppers.moveTo(gotoposition);
- if(stepper1.currentPosition() == xA && stepper2.currentPosition() == yA) {
- goneToA = true;
- }
- steppers.run();
- if(goneToA == false) {
- stepper1.setMaxSpeed(200);
- stepper2.setMaxSpeed(200);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Going to A...");
- lcd.setCursor(0,1);
- lcd.print("X");
- lcd.setCursor(2,1);
- lcd.print(xA);
- lcd.setCursor(9,1);
- lcd.print("Y");
- lcd.setCursor(11,1);
- lcd.print(yA);
- while(steppers.run() == true && digitalRead(SETpin) == 1) {
- steppers.run();
- goneToA = true;
- }
- }
- //do timelapse
- while(digitalRead(SETpin) == 1 && photosTaken < totalPhotos) {
- gotoposition[0] = stepper1.currentPosition() + xStep;
- gotoposition[1] = stepper2.currentPosition() + yStep;
- stepper1.setMaxSpeed(50);
- stepper2.setMaxSpeed(50);
- steppers.moveTo(gotoposition);
- steppers.run();
- while(steppers.run() == true && digitalRead(SETpin) == 1) {
- steppers.run();
- }
- photosTaken++;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Remain:");
- lcd.setCursor(8,0);
- lcd.print(timeBetweenPhotos*(totalPhotos - photosTaken)/60);
- lcd.setCursor(12,0);
- lcd.print("min");
- lcd.setCursor(7,1);
- lcd.print(totalPhotos - photosTaken);
- lcd.setCursor(11,1);
- lcd.print("photo");
- while(digitalRead(SETpin) == 1 && !(millis() % (timeBetweenPhotos*1000)) == 0) {}
- }
- stepper1.setMaxSpeed(1000);
- stepper2.setMaxSpeed(1000);
- lcd.clear();
- }
- //update display only when the motors are stopped, to avoid overloading the Arduino
- if(analogRead(xpin) > 400 && analogRead(xpin) < 600 && analogRead(ypin) > 400 && analogRead(ypin) < 600) {
- stepper1.setSpeed(0);
- stepper2.setSpeed(0);
- //display time between photos and total time
- if(digitalRead(SETpin) == 1) {
- if(page == false) {
- lcd.clear();
- page = true;
- }
- //lcd.setCursor(0,0);
- //lcd.print("Time");
- lcd.setCursor(0,0);
- lcd.print(timeBetweenPhotos);
- lcd.setCursor(3,0);
- lcd.print("s");
- if(timeBetweenPhotos < 100) {
- lcd.setCursor(2,0);
- lcd.print(" ");
- }
- if(timeBetweenPhotos < 10) {
- lcd.setCursor(1,0);
- lcd.print(" ");
- }
- lcd.setCursor(0,1);
- lcd.print("Total");
- lcd.setCursor(6,1);
- lcd.print((timeBetweenPhotos*totalPhotos)/60);
- lcd.setCursor(10,1);
- lcd.print("min");
- if(((timeBetweenPhotos*totalPhotos)/60) < 1000) {
- lcd.setCursor(9,1);
- lcd.print(" ");
- }
- if(((timeBetweenPhotos*totalPhotos)/60) < 100) {
- lcd.setCursor(8,1);
- lcd.print(" ");
- }
- if(((timeBetweenPhotos*totalPhotos)/60) < 10) {
- lcd.setCursor(7,1);
- lcd.print(" ");
- }
- //display total photos
- lcd.setCursor(6,0);
- lcd.print(totalPhotos);
- lcd.setCursor(10,0);
- lcd.print("photos");
- if(totalPhotos < 100) {
- lcd.setCursor(8,0);
- lcd.print(" ");
- }
- }
- //display A and B positions when SET is pressed
- if(digitalRead(SETpin) == 0) {
- if(page == true) {
- lcd.clear();
- page = false;
- }
- lcd.setCursor(0,0);
- lcd.print("X");
- lcd.setCursor(2,0);
- lcd.print(xA);
- lcd.setCursor(8,0);
- lcd.print(">");
- lcd.setCursor(10,0);
- lcd.print(xB);
- lcd.setCursor(0,1);
- lcd.print("Y");
- lcd.setCursor(2,1);
- lcd.print(yA);
- lcd.setCursor(8,1);
- lcd.print(">");
- lcd.setCursor(10,1);
- lcd.print(yB);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement