Another accelstepper question adult toy

Hello, and this is another accelstepper question to go in with the hundreds of others I have been reading and searching thru in the last week. I figured out how to use the built-in stepper library to do what I want, but I need to use accelstepper for its non-blocking control of the motor.

My main program is around almost 900 or more lines of code including the spaces between different statements and things like that. I figured nobody here was wanting to go thru it so I have isolated the stepper control switchcase. The most common question people seem to have I have found for accelstepper is how to reverse the motor and I have read a lot of them but I cannot understand how to integrate anything into my switchcase that I have read.

I’ve read that you cannot just use moveTo(); run(); and then the negative moveTo(); run(); because it just goes forward then back over and over basically not moving. I read with runSpeed(); I must use negative speed for reverse but the same applies I need to have the moveTo(); complete its steps before it can go to the moveTo(-);. The other reverse issue discussions people only want 1 pattern to playout, I am needing multiple patterns to work depending on the rotation of the potentiometer to select the case number.
My question is how can I let it play out its moveTo(); steps with not block the code before it moves into the negative steps using switchcase.

For those interested this is an adult toy which this motor is going on thus the reason for repeating patterns and the ability to select those any time. I know I could have used a rotary encoder but potentiometer is what I went with. If everyone would like to review the entire program I could attach it but I didn’t think it was relevant in this case but I maybe wrong because I have a feeling this very statement about relevancy is going to get me in trouble.

#include <AccelStepper.h>

AccelStepper stepper(1, 8, 9);

int ena = 10; // used to release the motor low means no power to coils, high powers coils
int motor_pat = 0; //used for the map
int motor_intensity = 0; //used for the range and map
int motorPattern = 0; //the motor vibrator pattern

void setup() {

  // put your setup code here, to run once:
  stepper.setMaxSpeed(500);
  stepper.setSpeed(100);
  stepper.moveTo(200);
}

void loop() {

  motor_pat = analogRead(A0); //reading for motor pattern selection
  motor_intensity = analogRead(A1);  //reading for motor vibration intensity
  int motor_pat_sel = map(motor_pat, 0, 1023, 0, 5); //divided the pot into increments
  int motor_intensity_set = map(motor_intensity, 0 , 1023, 0, 500); //used to control the stepper speed

  stepper.run();

  switch (motor_pat_sel) {

    case 0:
      digitalWrite (ena, LOW);
      break;

    case 1:
      digitalWrite (ena, HIGH);
      stepper.setSpeed(motor_intensity_set);
      stepper.runSpeed();
      break;

    case 2:
      digitalWrite (ena, HIGH);
      stepper.setSpeed(motor_intensity_set);
      stepper.moveTo(200);
      stepper.run();
      stepper.moveTo(-200);
      stepper.run();
      break;

    case 3:
      digitalWrite (ena, HIGH); //enable the motor coils
      stepper.setSpeed(motor_intensity_set);
      stepper.moveTo(103);
      stepper.run();
      stepper.setSpeed(motor_intensity_set);
      stepper.moveTo(-201);
      stepper.run();
      break;

    default:
      digitalWrite (ena, LOW);
      break;
  }
}

JayMan:
My question is how can I let it play out its moveTo(); steps with not block the code before it moves into the negative steps using switchcase.

The function distanceToGo() will return 0 when a move has completed.

...R

So I would call a distance to go 200 and then if distancetogo == 0 then move to negative step value?

I am getting the Back-and-Forth. Even running the exact Robin2 example I've never gotten it to do any actual rotation. Slowed it down enough it is no longer just a vibration. Back-and-forht. Clearly I'm missing something.

From what I can think the moveTo sets the destination it begins it’s travel with run. It is moving there but as soon as the loop swings back around and goes thru the moveTo then it is basically resetting the target even if I use

moveTo(200)
Distance to go()
If (distance to go == 0)
moveTo(-200)

It would do the negative 200 but the loop keeps on resetting the target so distance to go never becomes true.

Perhaps a count maybe the answer and each count pertains to a move to number of my choosing whether it be negative or positive.

If case 2 is true then it would call the variable 1 for example then when distance to go equals 0 becomes true it will change the variable to another number pertaining to a step in the direction desired.

That way the loop would not keep resetting the count as long as the conditional statement rings true.

I guess thinking of it that way I would still have trouble when the negative numbers take the count away from zero it would want to switch back to the positive step variable.

JayMan:
It would do the negative 200 but the loop keeps on resetting the target so distance to go never becomes true.

You code needs to be a little more sophisticated so it only sets the destination once. Maybe have a variable called destinationSet and change it to true when you set a value and set it to false when distanceToGo() shows 0.

Then you can do something like

if (destinationSet == false) {
   moveTo(nnnn);
   destinationSet = true;
}

I did a little programming with a destination bool, but I am unsure if it is going to work as I intend because I think if I come off another case statement and the position is going to be != 0 and screw up what I have going. I honestly have to really play around with the accelstepper library more I guess.

I may just write my own control considering I am using a driver:

void motor_step() {
  digitalWrite(stepPin, HIGH);
  digitalWrite(stepPin, LOW);
  count++
}[code]

and just use millis with an interval mapped from the speed controller pot to change the firing interval simply calling motor_step and then using a count to control the forward and reversing of the motor. Of course I need the count variable setup in the real code but I will just call the direction pin to be high and low when the step hits a certain interval. 

That way I can run it however I want hopefully. I am hoping when I get home tonight it will work that easy but who knows.

This is what I came up with to get the motor to work with the switchcase. It doesn’t work great but it does do what I need it to. It is still a work in process.

#include <AccelStepper.h>

AccelStepper stepper(1, 8, 9);

int ena = 10; // used to release the motor low means no power to coils, high powers coils
int motor_pat = 0; //used for the map
int motor_intensity = 0; //used for the range and map
int motorPattern = 0; //the motor vibrator pattern
bool trigger_forward = false ;
bool trigger_backward = false ;
unsigned long time_step = 0L ;

void setup() {

 // put your setup code here, to run once:
 stepper.setMaxSpeed(500);
 stepper.setSpeed(100);
 stepper.moveTo(200);
}

void loop() {

 motor_pat = analogRead(A0); 
 motor_intensity = analogRead(A1);
 int motor_pat_sel = map(motor_pat, 0, 1023, 0, 2); 
 int motor_intensity_set = map(motor_intensity, 0 , 1023, 0, 500); 
 stepper.run();

 switch (motor_pat_sel) {

   case 0:

     digitalWrite (ena, LOW);
     break;


   case 1:

     digitalWrite (ena, HIGH);
     stepper.run();
     if ( millis() - time_step >= 1000 && trigger_forward) {
       time_step = millis() ;
       trigger_forward = false ;
       trigger_backward = true;
       stepper.setMaxSpeed(-motor_intensity_set);
       stepper.move(-rand() % 200);
       //stepper.setAcceleration((rand() % 200) + 1);
     }

     if  (millis() - time_step >= 1000 && trigger_backward) {
       time_step = millis();
       trigger_backward = false ;
       trigger_forward = true;
       stepper.setMaxSpeed(motor_intensity_set);
       stepper.move(153);
     }

     break;
  
   case 2:

     digitalWrite (ena, HIGH);
     stepper.setMaxSpeed(motor_intensity_set);
     stepper.move(200);
     stepper.run();
     break;

     case 3:
     digitalWrite (ena, HIGH);
     stepper.setMaxSpeed(-motor_intensity_set);
     stepper.move(-200);
     stepper.run();
     break;

   default:

     digitalWrite (ena, LOW);
     stepper.run();
     break;

 }
}

JayMan:
It doesn't work great but it does do what I need it to.

If you want advice you need to explain what the remaining problem is.

...R