Motorized Camera slider (convert to blynk)

Hi i’m new at the forum i’m 23 and live in Sweden!

I need help!
Did build a slider for my camera with a stepper motor and a controller.

Now to my problem i want to unsold-er the buttons and the pots and use blynk instead.
and want the help to convert the code.

Parts i used:
Adafruit Feather 32u4 Bluefruit LE (because i knew i wanted a wireless and more compact system at some point)

Stepper motor SM-42BYG011-25

Driver:
HALJIA EasyDriver Shield Stepping Stepper Motor Driver A3967 for Arduino

My code as it runs today:

/*
 Stepper Motor Single Channel Manual Controller
 language: Wiring/Arduino

 This program drives a single bi-polar stepper motor using an EasyDriver
 stepper motor controller from SparkFun.
 It takes input from a set of switches and potentiometers.
 
 The goal is to create smooth motion of the steppper motor without having a 
 PC attached to the Arduino.

 The motor moves 1600 steps in one rotation. (8th step microstepping on a 200 s/r motor)

 Created 08/18/2010
 by Brian Schmalz
 
 Version 0.2  01/30/2011  Added accel/decl
 Version 0.3  10/09/2011  Fixed two bugs found by Daniel Schweinert:
                           1) Speed Pot not working during Rotate Left or Rotate Right moves
                           2) When Goto A or Goto B is pressed after using Rotate Left or Right, motor moves in the
                           wrong directly for a little bit before moving in the right direction.
*/

#include <AccelStepper.h>

// Define some steppers and the pins the will use
AccelStepper stepper1(1, 12, 11); 

#define stepPin          12
#define dirPin           11
#define ledPin           13
#define rotateLeftPin    10
#define rotateRightPin   9
#define savePositionAPin 6
#define savePositionBPin 5
#define gotoPositionAPin 3
#define gotoPositionBPin 2
#define maxSpeedPin      0
#define accelPin         1

// Set this to zero if you don't want debug messages printed
#define printDebug       0

// These are the constants that define the speed associated with the MaxSpeed pot
#define  MAX_SPEED 3000    // At 200 s/r and 1/8th microstepping, this will be 333 rev/minute
#define  MIN_SPEED 0.1     // At 200 steps/rev and 1/8th microstepping, this will be 1 rev/minute

// Change this value to scale the acceleration pot's scaling factor
#define ACCEL_RATIO           1

int buttonState = 0;
int stepNumber = 0;
int curSpeed = 100;
int dir = 0;
int maxSpeed = 0;
int accel = 0;
long savedPosA = 0;
long savedPosB = 0;

int loopCtr = 0;
  
float fMaxSpeed = 0.0;
float fStepsPerSecond = 0.0;

void setup() 
{
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(rotateLeftPin, INPUT);
  pinMode(rotateRightPin, INPUT);
  pinMode(savePositionAPin, INPUT);
  pinMode(savePositionBPin, INPUT);
  pinMode(gotoPositionAPin, INPUT);
  pinMode(gotoPositionBPin, INPUT);
  
  if (printDebug)
  {
    // Initialize the Serial port
    Serial.begin(9600);
  }
  
  // blink the LED:
  blink(2);

  stepper1.setMaxSpeed(3000.0);
  stepper1.setAcceleration(600.0);

  // Grab both speed and accel before we start
  maxSpeed = analogRead(maxSpeedPin);
  // Do the math to scale the 0-1023 value (maxSpeed) to 
  // a range of MIN_STEPS_PER_SECOND to MAX_STEPS_PER_SECOND
  fMaxSpeed = maxSpeed / 1023.0;
  fStepsPerSecond = MIN_SPEED + (fMaxSpeed * (MAX_SPEED - MIN_SPEED));
  if (fStepsPerSecond > 3000)
  {
    fStepsPerSecond = 3000;
  }
  accel = analogRead(accelPin)/ACCEL_RATIO;
}

void loop() 
{  
  // First, we need to see if either rotate button is down. They always take precidence.
  if(digitalRead(rotateLeftPin))
  {
    stepper1.setSpeed(-fStepsPerSecond);
    while(digitalRead(rotateLeftPin))
    {
      CheckPots();
      stepper1.runSpeed();
      stepper1.setSpeed(-fStepsPerSecond);
    }
  }
  else if (digitalRead(rotateRightPin))
  {
    stepper1.setSpeed(fStepsPerSecond);
    while(digitalRead(rotateRightPin))
    {
      CheckPots();
      stepper1.runSpeed();
      stepper1.setSpeed(fStepsPerSecond);
    }
  }

  // Go see if we need to update our analog conversions
  CheckPots();
  
  // Check to see if user is trying to save position A or B
  if(digitalRead(savePositionAPin))
  {
    savedPosA = stepper1.currentPosition();
    if (printDebug)
    {
      Serial.print("Saved A at :");
      Serial.println(savedPosA);
    }
    while(digitalRead(savePositionAPin));
  }
  if(digitalRead(savePositionBPin))
  {
    savedPosB = stepper1.currentPosition();
    if (printDebug)
    {
      Serial.print("Saved B at :");
      Serial.println(savedPosB);
    }
    while(digitalRead(savePositionBPin));
  }
    
  // Check to see if the user wants to go to position A or B
  if (digitalRead(gotoPositionAPin))
  {
    if (printDebug)
    {
      // Yup, let's go to position A
      Serial.print("cur pos = ");
      Serial.println(stepper1.currentPosition());
      Serial.print("Going to A = ");
      Serial.println(savedPosA);
      Serial.print("Speed = ");
      Serial.println(fStepsPerSecond);
      Serial.print("Accel = ");
      Serial.println(accel);
    }

    stepper1.setAcceleration(0);
    stepper1.runToNewPosition(stepper1.currentPosition());
    
    stepper1.setMaxSpeed(fStepsPerSecond);
    stepper1.setAcceleration(accel);
    stepper1.runToNewPosition(savedPosA);

    if (printDebug)
    {
      Serial.print("new pos = ");
      Serial.println(stepper1.currentPosition());
    }
    while(digitalRead(gotoPositionAPin));
  }
  else if (digitalRead(gotoPositionBPin))
  {
    // Yup, let's go to position B
    if (printDebug)
    {
      Serial.print("cur pos = ");
      Serial.println(stepper1.currentPosition());
      Serial.print("Going to B = ");
      Serial.println(savedPosB);
      Serial.print("Speed = ");
      Serial.println(fStepsPerSecond);
      Serial.print("Accel = ");
      Serial.println(accel);
    }

    stepper1.setAcceleration(0);
    stepper1.runToNewPosition(stepper1.currentPosition());

    stepper1.setMaxSpeed(fStepsPerSecond);
    stepper1.setAcceleration(accel);
    stepper1.runToNewPosition(savedPosB);

    if (printDebug)
    {
      Serial.print("new pos = ");
      Serial.println(stepper1.currentPosition());
    }
    while(digitalRead(gotoPositionBPin));
  }
}

// Blink the reset LED:
void blink(int howManyTimes) 
{
  int i;
  for (i=0; i < howManyTimes; i++) 
  {
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
  }
}

void CheckPots(void)
{
  loopCtr++;

  // Only read these once in a while because they take a LONG time
  if (loopCtr == 100)
  {
    maxSpeed = analogRead(maxSpeedPin);

    // Do the math to scale the 0-1023 value (maxSpeed) to 
    // a range of MIN_STEPS_PER_SECOND to MAX_STEPS_PER_SECOND
    fMaxSpeed = maxSpeed / 1023.0;
    fStepsPerSecond = MIN_SPEED + (fMaxSpeed * (MAX_SPEED - MIN_SPEED));
    if (fStepsPerSecond > 3000)
    {
      fStepsPerSecond = 3000;
    }
  }

  // Read in the acceleration analog value
  // This needs to be scaled too, but to what?
  if (loopCtr >= 200)
  {
    accel = analogRead(accelPin)/ACCEL_RATIO;
    loopCtr = 0;
  } 
}

I want to have this buttons and slider.
and really need your guys help!

Big code, big port.

It possible but you will probably have to re-write most of the sketch.

The community is full of other makers who can help guide you, but its best if you know how to use and work with Blynk first.

You will need to learn how to use SimpleTimer so you can time all you events and also clear out your loop() so it only contains Blynk.run(); and timer.run();. Also remove all delays from your code as these will block Blynk’s heartbeat timer and cause disconnects.

Also your code contains horrible > characters at the start of each line, please remove those so people can easily help you with your code.

Format C++ code with the example below:

```cpp
CODE
```

1 Like

Thanks Jamin for your response!
i was on my way to massage u because you seems to have your coding fingers with you!

yeah it’s a big code but i think i can cut it down a bit!

But i really need some one to help and collaborate with me on this.

so if your up for it i would love to have your help.

Create a Github repo and put your code in there and ill help you get started.

Hi again Jamin i created a github i think i did it right this time.

1 Like

Hi again Blynk community!

been working with my slider and getting closer!
its working as intended now but some small fixes i’m working on to make it perfect.

7 Button and 2 Slider going to add one more button to save the Center

if some one can check the code and see if and and if they spot something wrong can tell me!

Good blynking guys.

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

#include <BlynkSimpleSerialBLE.h>
#include <Adafruit_BLE.h>
#include <Adafruit_BluefruitLE_SPI.h>
#include <SPI.h>
#include <AccelStepper.h>
//************************************************************************************************
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "53ad82607b3e417c86c7a5fcf393729d";
//************************************************************************************************
// SHARED SPI SETTINGS (see adafruit webpages for details)
#define BLUEFRUIT_SPI_CS               8
#define BLUEFRUIT_SPI_IRQ              7
#define BLUEFRUIT_SPI_RST              4    // Optional but recommended, set to -1 if unused
#define BLUEFRUIT_VERBOSE_MODE         true
//************************************************************************************************
// Create ble instance, see pinouts above
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
//************************************************************************************************
// Define some steppers and the pins the will use
AccelStepper stepper1(1, 12, 11);
//************************************************************************************************
#define rotateLeftPin V10
//************************************************************************************************
int fStepsPerSecond = 4000;
int Stop = 0;
int Center = 0;
long savedPosA = 0;
long savedPosB = 0;
//************************************************************************************************
void setup() {
  Serial.begin(9600);
  ble.begin(BLUEFRUIT_VERBOSE_MODE);
  ble.factoryReset(); // Optional
  ble.setMode(BLUEFRUIT_MODE_DATA);
  Blynk.begin(auth, ble);

  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(300.0);
}

//************************************************************************************************
// Slider Acceleration
BLYNK_WRITE(V1) { 
  stepper1.setAcceleration(param.asInt());
}
//************************************************************************************************
// Slider Speed
BLYNK_WRITE(V0) { 
  stepper1.setMaxSpeed(param.asInt()); 
}
//************************************************************************************************
// Slider Move Left
BLYNK_WRITE(V10) 
{
    int i=param.asInt();
    if (i==1) 
    {
      stepper1.runSpeed(); 
      stepper1.setSpeed(1000);
      //Serial.println("Going Left");
    }
    else 
    {
      stepper1.runSpeed();
      stepper1.setSpeed(Stop);
      //Serial.println("Stopping");
    }
}
//************************************************************************************************
// Slider Move Right
BLYNK_WRITE(V9)
{
    int i=param.asInt();
    if (i==1) 
    {
      stepper1.runSpeed(); 
      stepper1.setSpeed(-1000);
      //Serial.println("Start");
    }
    else 
    {
      stepper1.runSpeed();
      stepper1.setSpeed(Stop);
      //Serial.println("Stopping");
    }
}
//************************************************************************************************
// Slider Save A
BLYNK_WRITE(V6)
{
  int i=param.asInt();
    if (i==1) 
    {
savedPosA = stepper1.currentPosition();
Serial.println("Saved A");
Serial.println(savedPosA);
}
}
//************************************************************************************************
// Slider Save B
BLYNK_WRITE(V5)
{
  int i=param.asInt();
    if (i==1) 
    {
savedPosB = stepper1.currentPosition();
Serial.println("Saved B");
Serial.println(savedPosB);
}
}
//************************************************************************************************
// Slider Go To A
BLYNK_WRITE(V3)
{
  int i=param.asInt();
    if (i==1) 
    {
    stepper1.runToNewPosition(savedPosA);
Serial.println("Going To A");
Serial.println(savedPosA);
}
}
//************************************************************************************************
// Slider Go To B
BLYNK_WRITE(V2)
{
  int i=param.asInt();
    if (i==1) 
    {
    stepper1.runToNewPosition(savedPosB);
Serial.println("Going To B");
Serial.println(savedPosB);
}
}
//************************************************************************************************
// Slider Go To Center
BLYNK_WRITE(V4)
{
  int i=param.asInt();
    if (i==1) 
    {
    stepper1.runToNewPosition(Center);
Serial.println("Going To Center");
Serial.println(Center);
}
}
//************************************************************************************************
void loop() {
  Blynk.run();
  stepper1.runSpeed();
}

How is this project going? Is it working like you intended? I want to do the same kind of thing and looks like you have a great idea especially with the variable settings. Any updates to your progress?