Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repeat movement without going through the setup again #2

Open
maxmontegtr opened this issue Aug 9, 2020 · 9 comments
Open

repeat movement without going through the setup again #2

maxmontegtr opened this issue Aug 9, 2020 · 9 comments

Comments

@maxmontegtr
Copy link

Hi, I'm a total noob and I don't know what I am doing with code. That said... I made a slider and use your code, thanks!
It's awesome.
I think it would be even better if at the end of the cycle, when you click after "finish", it went back to X in e Y in (like it does on preview), and allowed you to star the program again with the last used parameters (same in and out points, same speed).
that way it would be more convenient to do multiple takes while filming, instead of having to set everything up every time.

Thanks! Max

@cgbright
Copy link

I have modified the code to do just that - after the run finishes it returns to the start position and you can change the speed if you wish.

`// Sketch for CamSlider
// 06/04/2019 by RZtronics [email protected]
// Project homepage: http://RZtronics.com/
///////////////////////////////////////////////////
//MODIFIED BY Chris G Bright (SparkeyB) July & August 2020
///////////////////////////////////////////////////

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include "pitches.h"

int startmelody[] = {
NOTE_G5, NOTE_C5, NOTE_G5, 0, NOTE_G5
};
int clickmelody[] = {
NOTE_F5, NOTE_G5
};
int noteDurations[] = {
8, 4, 8, 8, 4, 4, 4, 4
};

int volatile prevPOS = 0;
int volatile curPOS = 0;

#define enterSwitch1 11 //Enter values switch
#define limitSwitch2 12 //Homing protection limit switch

// setting pins for old mechanical rotary encoder //
#define PinSW 2
#define PinCLK 3
#define PinDT 8

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// setting pins for oled display //
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC A0 // was 30
#define OLED_CS A1 // was 31
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
//Adafruit_SSD1306 display(OLED_RESET);

AccelStepper stepper2(1, 7, 6); // (Type:driver, STEP, DIR)PANNING-MOTOR
AccelStepper stepper1(1, 5, 4); // X-axis SLIDER-MOTOR

MultiStepper StepperControl;

long gotoposition[2];

volatile long XInPoint=0;
volatile long YInPoint=0;
volatile long XOutPoint=0;
volatile long YOutPoint=0;
volatile long totaldistance=0;
int flag=0;
int temp=0;
int i,j;
unsigned long switch0=0;
unsigned long rotary0=0;
float setspeed=200;
float motorspeed;
float timeinsec;
float timeinmins;
volatile boolean TurnDetected;
volatile boolean rotationdirection;

void Switch()
{
if(millis()-switch0>500)
{
flag=flag+1;

}
switch0=millis();

}

void Rotary()
{
//// old mech rotary encoder
delay(75);
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
delay(75);
////
}

void setup()
{

// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 5; thisNote++) {

// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(A5, startmelody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(A5);

}

Serial.begin(9600);
stepper1.setMaxSpeed(6400); //was 3000
stepper1.setSpeed(1000);
stepper2.setMaxSpeed(3200); //was 3000
stepper2.setSpeed(200);

pinMode(enterSwitch1, INPUT_PULLUP);
pinMode(limitSwitch2, INPUT_PULLUP);
pinMode(PinSW,INPUT_PULLUP);
pinMode(PinCLK,INPUT_PULLUP);
pinMode(PinDT,INPUT_PULLUP);
pinMode(40, OUTPUT);

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally

if(!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.print("SSD1306 allocation failed");
for(;;); // Don't proceed, loop forever
}

display.clearDisplay();

// Create instances for MultiStepper - Adding the 2 steppers to the StepperControl instance for multi control
StepperControl.addStepper(stepper1);
StepperControl.addStepper(stepper2);

attachInterrupt (digitalPinToInterrupt(2),Switch,RISING); // SW connected to D2
attachInterrupt (digitalPinToInterrupt(3),Rotary,RISING); // CLK Connected to D3 //was RISING

// display Boot logo
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("CAMERA");
display.setCursor(15, 15);
display.print("SLIDER");
display.setCursor(64, 32);
display.print("By");
display.setCursor(10, 47);
display.print("SparkeyB");
display.display();
delay(2000);
display.clearDisplay();

Home(); // Move the slider to the initial position - homing

}

void Home()
{
stepper1.setMaxSpeed(12800); //was 3000
stepper1.setSpeed(2000); //was 200
stepper2.setMaxSpeed(3200); //was 3000
stepper2.setSpeed(200);
if(digitalRead(limitSwitch2)==1)
{
display.setCursor(0,16);
display.print("Homing....");
display.display();
}

while (digitalRead(limitSwitch2)== 1) // returns carriage to end-stop switch
{
stepper1.setSpeed(-12800); //was -3000
stepper1.runSpeed();

}
delay(1000);
stepper1.setCurrentPosition(0);
stepper1.moveTo(200); //was 200
while(stepper1.distanceToGo() != 0)
{
stepper1.setSpeed(1600); //was 6400
stepper1.runSpeed();
}
stepper1.setCurrentPosition(0);
display.clearDisplay();
}

void SetSpeed()
{
display.clearDisplay();
while( flag==6)
{
if (TurnDetected)
{
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
if (rotationdirection)
{
setspeed = setspeed + 10;
}
if (!rotationdirection)
{
setspeed = setspeed - 10;
if (setspeed < 0)
{
setspeed = 0;
}
}

      display.clearDisplay();
      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(30,0);
      display.print("Speed");
      motorspeed=setspeed/80;
      display.setCursor(5,16);
      display.print(motorspeed);
      display.print(" mm/s");   
      totaldistance=XOutPoint-XInPoint;
      if(totaldistance<0)
        {
          totaldistance=totaldistance*(-1);
        }
        else
        {

        }
      timeinsec=(totaldistance/setspeed);
      timeinmins=timeinsec/60;
      display.setCursor(35,32);
      display.print("Time");
      display.setCursor(8,48);
      if(timeinmins>1)
      {
      display.print(timeinmins);
      display.print(" min");
      }
      else
      { 
      display.print(timeinsec);
      display.print(" sec");
      }
      display.display();

}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(30,0);
display.print("Speed");
motorspeed=setspeed/80;
display.setCursor(5,16);
display.print(motorspeed);
display.print(" mm/s");
totaldistance=XOutPoint-XInPoint;
if(totaldistance<0)
{
totaldistance=totaldistance*(-1);
}
else
{

        }
      timeinsec=(totaldistance/setspeed);
      timeinmins=timeinsec/60;
      display.setCursor(35,32);
      display.print("Time");
      display.setCursor(8,48);
      if(timeinmins>1)
      {
      display.print(timeinmins);
      display.print(" min");
      }
      else
      { 
      display.print(timeinsec);
      display.print(" sec");
      }
      display.display();

}

}

void stepperposition(int n)
{
stepper1.setMaxSpeed(3200);
stepper1.setSpeed(200);
stepper2.setMaxSpeed(1600);
stepper2.setSpeed(200);
if (TurnDetected)
{
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
if(n==1)
{
if (!rotationdirection)
{
if( stepper1.currentPosition()-500>0 )
{
stepper1.move(-2400); //was 1200
while(stepper1.distanceToGo() != 0)
{
stepper1.setSpeed(-2400);
stepper1.runSpeed();
}
}
else
{
while (stepper1.currentPosition()!=0)
{
stepper1.setSpeed(-2400);
stepper1.runSpeed();
}
}
display.setCursor(0,50);

    }

    if (rotationdirection) 
    { 
      if( stepper1.currentPosition()+500<101000 )
      {
      stepper1.move(2400); //was 1200
      while(stepper1.distanceToGo() != 0)
        {
          stepper1.setSpeed(2400);
          stepper1.runSpeed();
        }
      }
      else
      {
        while (stepper1.currentPosition()!= 101000) 
         {
              stepper1.setSpeed(2400);
              stepper1.runSpeed();

         } 
      }
    }
 }
 if(n==2)
 {
   if (rotationdirection) 
   { 
     stepper2.move(-150);
     while(stepper2.distanceToGo() != 0)
       {
         stepper2.setSpeed(-1600);
         stepper2.runSpeed();
       }      
   }
    if (!rotationdirection) 
   { 
     stepper2.move(150);
     while(stepper2.distanceToGo() != 0)
       {
         stepper2.setSpeed(1600);
         stepper2.runSpeed();
       } 
   }
 }
 }

}

void loop()
{
//Begin Setup
if(flag==0)
{
display.clearDisplay();
display.setCursor(30,2);
display.println("SETUP");
display.setCursor(0,30);
display.println(" CLICK TO START");
display.display();
setspeed=200;
}

//Set Slider Start Position
if(flag==1)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Set Slider");
display.setCursor(0,15);
display.println("start pos.");
display.setCursor(0,36);
display.setTextSize(1);
display.println(" <-- X --------->");
display.setTextSize(2);
display.setCursor(0,50);
display.println("Then click");
display.display();
while(flag==1)
{
stepperposition(1);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Set Slider");
display.setCursor(0,15);
display.println("start pos.");
display.setCursor(0,36);
display.setTextSize(1);
display.println(" <-- X --------->");
display.setTextSize(2);
display.setCursor(0,50);
display.println(stepper1.currentPosition());
display.display();
}
XInPoint=stepper1.currentPosition();
}
//Set Pan Start Position
if(flag==2)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(" Set Pan");
display.setCursor(0,15);
display.println("START pos.");
display.setCursor(0,36);
display.setTextSize(1);
display.println(" <-- O --------->");
display.setTextSize(2);
display.setCursor(0,50);
display.println("Then click");
display.display();
while(flag==2)
{
stepperposition(2);
}
stepper2.setCurrentPosition(0);
YInPoint=stepper2.currentPosition();
}
//Set Slider END Position
if(flag==3)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Set Slider");
display.setCursor(0,15);
display.println(" END pos.");
display.setCursor(0,36);
display.setTextSize(1);
display.println(" <--------- X -->");
display.setTextSize(2);
display.setCursor(0,50);
display.println("Then click");
display.display();
while(flag==3)
{
stepperposition(1);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Set Slider");
display.setCursor(0,15);
display.println(" END pos.");
display.setCursor(0,36);
display.setTextSize(1);
display.println(" <--------- X -->");
display.setTextSize(2);
display.setCursor(0,50);
display.println(stepper1.currentPosition());
display.display();
}
XOutPoint=stepper1.currentPosition();

}
//Set Pan END Position
if(flag==4)
{
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(" Set Pan");
display.setCursor(0,15);
display.println("END pos.");
display.setCursor(0,36);
display.setTextSize(1);
display.println(" <--------- O -->");
display.setTextSize(2);
display.setCursor(0,50);
display.println("Then click");
display.display();
while(flag==4)
{
stepperposition(2);
}
YOutPoint=stepper2.currentPosition();
display.clearDisplay();

// Go to IN position
gotoposition[0]=XInPoint;
gotoposition[1]=YInPoint;    
display.clearDisplay();
display.setCursor(8,28);
display.println(" Preview  ");
display.display(); 
stepper1.setMaxSpeed(6000);
StepperControl.moveTo(gotoposition);
StepperControl.runSpeedToPosition();

}

//Display Set Speed
if(flag==5)
{
display.clearDisplay();
display.setCursor(8,28);
display.println("Set Speed");
display.display();
}
//Change Speed
if(flag==6)
{
display.clearDisplay();
SetSpeed();
}
//DisplayStart
if(flag==7)
{
display.clearDisplay();
display.setCursor(16,10);
display.println("CLICK TO");
display.setCursor(30,35);
display.println("START");
display.display();
}
//Start
if(flag==8)
{
display.clearDisplay();
display.setCursor(20,27);
display.println("Running");
display.setCursor(20,47);
//display.println(XInPoint - XOutPoint);
display.display();
//Serial.println(XInPoint);
//Serial.println(XOutPoint);
//Serial.println(YInPoint);
//Serial.println(YOutPoint);

gotoposition[0]=XOutPoint;
gotoposition[1]=YOutPoint;
stepper1.setMaxSpeed(setspeed);
StepperControl.moveTo(gotoposition);
StepperControl.runSpeedToPosition();
flag=flag+1;

}
//Slide Finish
if(flag==9)

{
display.clearDisplay();
display.setCursor(48,0);
display.println("Run");
display.setCursor(12,16);
display.println("COMPLETE");
display.setCursor(12,33);
display.println("PRESS TO");
display.setCursor(18,50);
display.println("RESTART");
display.display();
}

// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 1; thisNote++) {

// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 500 / noteDurations[thisNote];
tone(A5, clickmelody[thisNote], noteDuration); //Analogue A5 pin

// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(A5); //Analogue A5 pin
delay(750);

}

//Return to start
if(flag==10)
{
display.clearDisplay();
// Home();
// Go to IN position
gotoposition[0]=XInPoint;
gotoposition[1]=YInPoint;
display.clearDisplay();
display.setCursor(8,28);
display.println(" Preview ");
display.display();
stepper1.setMaxSpeed(6000);
StepperControl.moveTo(gotoposition);
StepperControl.runSpeedToPosition();
flag=6;
//SetSpeed();

}
}`

@maxmontegtr
Copy link
Author

Thanks! There is a lot more on your code... but I figured what I needed to do... I had made a mistake setting a flag at the end after returning to XInPoint e YInPoint

@cgbright
Copy link

cgbright commented Aug 11, 2020 via email

@maxmontegtr
Copy link
Author

just a Nano

@cgbright
Copy link

cgbright commented Aug 11, 2020 via email

@maxmontegtr
Copy link
Author

I am using the original code (I just changed the speed and direction, but that depends on my slider design), plus a little tweak at the end for the last two "if"

//Slide Finish
if(flag==9)
{
display.clearDisplay();
display.setCursor(20,26);
display.println("Repeat?");
display.display();
}
//Return to start
if(flag==10)
{
gotoposition[0]=XInPoint;
gotoposition[1]=YInPoint;
display.clearDisplay();
display.setCursor(6,28);
display.println("Rewinding");
display.display();
stepper1.setMaxSpeed(3000);
StepperControl.moveTo(gotoposition);
StepperControl.runSpeedToPosition();
flag=6;
//SetSpeed();

@cgbright
Copy link

cgbright commented Aug 11, 2020 via email

@3cm3cm
Copy link

3cm3cm commented Nov 22, 2020

Hi , can I know where can get the 3 libraries file. and what transistor use for this project pls....

any one can help ? wish to make one set of this amazing build.

@3cm3cm
Copy link

3cm3cm commented Nov 24, 2020

I have problem on this project, the Y motor not always working and work very weak...I have few times are working well, then after that is not working at all.....I have no idea why, check the cable connect is fine.

and the encoder is keep floating, just touch a little bit with my finger, then it will move the motor, and sometime switch to another mode like from X out change to y out....

im not sure is about the hardware or the coding or the cabling ..... I watch the video many time and Raj are using transistor and resistor in this project...but it not show up in the schematic ...

hope some one can help...thanks so much !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants