Bluetooth Camera Slider

I am new to all of this and have been self-educating.

However, I am unable how to figure something out.

I have been able to get my stepper motor to work with an android app that I created with the code listed below (my layout is attached as well).

I would like to be able to add two limiter switches that, when struck, will have the slider pause 5 seconds and then send it back in the opposite direction. So far I have had no success and am looking for help.

Feel free to explain things as if I am a child (although I am almost 50). Thanks in advance!

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::FULL2WIRE, 8, 9);

int spd = 1000;    // The current speed in steps/second
int sign = 1;      // Either 1, 0 or -1

void setup()
{ 
  Serial.begin(9600);
  stepper.setMaxSpeed(1000);
  stepper.setSpeed(1000);   
  Serial.println("App started");
}

void loop()
{ 
  char c;
  if(Serial.available()) {
    c = Serial.read();
    if (c == 'f') {  // forward
      sign = 1;
      Serial.println("Forward");
    }
    if (c == 'r') {  // reverse
      sign = -1;
      Serial.println("Reverse");
    }
    if (c == 's') {  // stop
      sign = 0;
      Serial.println("Stopped");
    }
    if (c == '1') {  // Super slow
      spd = 10;
      Serial.println("Speed: 10");
    }
    if (c == '2') {  // Slow
      spd = 100;
      Serial.println("Speed: 100");
    }
    if (c == '3') {  // Medium
      spd = 300;
      Serial.println("Speed: 300");
    }
       if (c == '4') {  // Fast
      spd = 500;
      Serial.println("Speed: 500");
    }  
       if (c == '5') {  // Faster
      spd = 700;
      Serial.println("Speed: 700");
    }  
       if (c == '6') {  // Super Fast
      spd = 1000;
      Serial.println("Speed: 1000");
    }
    stepper.setSpeed(sign * spd);
  }
  stepper.runSpeed();
   
}

Velcrobelly:
have been self-educating.

Ohh - I’m not happy with that at all. I think you need to take yourself to the Parish Priest and seek absolution. :slight_smile:

More seriously, for the future please use the Code button </> so your code looks like this and is easy to copy to a text editor to examine it.

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::FULL2WIRE, 8, 9);

int spd = 1000;    // The current speed in steps/second
int sign = 1;      // Either 1, 0 or -1

void setup()
{
  Serial.begin(9600);
  stepper.setMaxSpeed(1000);
  stepper.setSpeed(1000);  
  Serial.println("App started");
}

void loop()
{
  char c;
  if(Serial.available()) {
    c = Serial.read();
    if (c == 'f') {  // forward
      sign = 1;
      Serial.println("Forward");
    }
    if (c == 'r') {  // reverse
      sign = -1;
      Serial.println("Reverse");
    }
    if (c == 's') {  // stop
      sign = 0;
      Serial.println("Stopped");
    }
    if (c == '1') {  // Super slow
      spd = 10;
      Serial.println("Speed: 10");
    }
    if (c == '2') {  // Slow
      spd = 100;
      Serial.println("Speed: 100");
    }
    if (c == '3') {  // Medium
      spd = 300;
      Serial.println("Speed: 300");
    }
       if (c == '4') {  // Fast
      spd = 500;
      Serial.println("Speed: 500");
    }  
       if (c == '5') {  // Faster
      spd = 700;
      Serial.println("Speed: 700");
    }  
       if (c == '6') {  // Super Fast
      spd = 1000;
      Serial.println("Speed: 1000");
    }
    stepper.setSpeed(sign * spd);
  }
  stepper.runSpeed();
  
}

…R

Have a look at how I have reorganized your code into separate single-purpose functions. I reckon this will make it a lot more obvious how to add the extra capability that you want.

I have deliberately left the checkLimits() function empty so you can have a go at figuring out what to put in it.

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::FULL2WIRE, 8, 9);

int spd = 1000;    // The current speed in steps/second
int sign = 1;      // Either 1, 0 or -1
char c;

void setup()
{
  Serial.begin(9600);
  stepper.setMaxSpeed(1000);
  stepper.setSpeed(1000);  
  Serial.println("App started");
}

void loop() {
   readSerial();
   checkLimits();
   updateSpeedAndDirection();
   moveMotor();
}

void checkLimits() {
    
}

void moveMotor() {
  stepper.setSpeed(sign * spd);
  stepper.runSpeed();
}

void readSerial() {
    if(Serial.available()) {
       c = Serial.read();
   }
}

void updateSpeedAndDirection() {
    if (c == 'f') {  // forward
      sign = 1;
      Serial.println("Forward");
    }
    if (c == 'r') {  // reverse
      sign = -1;
      Serial.println("Reverse");
    }
    if (c == 's') {  // stop
      sign = 0;
      Serial.println("Stopped");
    }
    if (c == '1') {  // Super slow
      spd = 10;
      Serial.println("Speed: 10");
    }
    if (c == '2') {  // Slow
      spd = 100;
      Serial.println("Speed: 100");
    }
    if (c == '3') {  // Medium
      spd = 300;
      Serial.println("Speed: 300");
    }
       if (c == '4') {  // Fast
      spd = 500;
      Serial.println("Speed: 500");
    }  
       if (c == '5') {  // Faster
      spd = 700;
      Serial.println("Speed: 700");
    }  
       if (c == '6') {  // Super Fast
      spd = 1000;
      Serial.println("Speed: 1000");
    }
}

I have deliberately made as few changes as possible to your code.

I have not tested this version so if it does not compile let me know.

…R

Thanks for the reply, however, I could not quite get your code to work.

With a little bit of experimenting, I was able to come up with a resolution, and I post it here for others to use.

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::FULL2WIRE, 8, 9);

// Define our three input button pins
#define  button1 3
#define  button2 2

int spd = 1000;    // The current speed in steps/second
int sign = 0;      // Either 1, 0 or -1
 
void setup()
{ 
  Serial.begin(9600);
  stepper.setMaxSpeed(1000);
  stepper.setSpeed(1000);
  
  // Set up the three button inputs, with pullups
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop()
{ 
  char c;
  if(Serial.available()) {
    c = Serial.read();
    if (c == 'f') {  // forward
      sign = 1;
    }
    if (c == 'r') {  // reverse
      sign = -1;
    }
    if (c == 's') {  // stop
      sign = 0;
    }
    if (c == '1') {  // super slow
      spd = 10;
    }
    if (c == '2') {  // slow
      spd = 100;
    }
    if (c == '3') {  // medium
      spd = 300;
    }
       if (c == '4') {  // fast
      spd = 500;
    }  
       if (c == '5') {  // faster
      spd = 700;
    }  
       if (c == '6') {  // superfast
      spd = 1000;
    }
  }
  if (digitalRead(button1) == 0) {
    sign = 1;
  }
  else if (digitalRead(button2) == 0) {   
    sign = -1;
  }
  stepper.setSpeed(sign * spd);
  stepper.runSpeed();
}
// Define our three input button pins
#define  button1 3
#define  button2 2

Trouble counting today?

Velcrobelly: Thanks for the reply, however, I could not quite get your code to work.

With a little bit of experimenting, I was able to come up with a resolution, and I post it here for others to use.

Glad you found a solution.

If you are considering anything more complex, or are considering extending that program with additional capabilities it will pay to learn how to use functions.

...R Planning and Implementing a Program