Need help moving stepper based on multiple inputs

Hello,

The functions of this module and where I need help have been clearly explained in the code comments.
The shift lever should move 6 steps(or as close to 11 degrees as possible) when the shift switch is placed in forward or reverse, and then it should return to neutral when the park brake is activated while in gear.
The motion sensor should only keep the shift lever from going out of neutral when motion is detected.

I am micro stepping to increase the torque output with jumpers to 5 volts on the A4988.
3200 1/16th microsteps is a full revolution so 98 microsteps should be about 11 degrees.

All of the hardware is functioning correctly, the park brake and the motion sensor interrupt the stepper and pause it wherever it is at.

I have been experimenting with the accelstepper library, and step counters etc. I'm not sure how to tie it in to this. What operators and control structures should is be using? Please feel free togive me some examples.

Any help is greatly appreciated, feel free to comment on my code formatting as its been 6 years since I last touched an Arduino, and even then is was minimal. I'm an R&D engineer that failed college coding and almost algebra but I have been studying, watching tutorials, and trying a lot of example codes.
Thanks in advance!

/* FNR Electronic Shift module
   by Mariobro_3 Feb. 2020
This code is to make a stepper motor move a lever forward, reverse, or back to neutral, when no motion is detected and the park brake is off.
When the park brake is active while the shifter is F or R, it should return to N
 **Hardware used:
   Arduino Mega 2560
   A4988 clone stepper driver
   RCWl-0516 radar motion sensor
   directional shifter switch (FNR) "N" has no output
   latching park brake switch
   NEMA 17 stepper motor
   10k pullup resistors
   100uF cap for 12 volt motor power
   12 volt 5 amp power supply
*/

const int fPin = 10;    // the number of the forward pin
const int rPin = 11;    // "               " reverse pin
const int parkPin = 12; // "               " park brake pin
const int motInd =  13; // "               " motion indicator(LED_BUILTIN)
const int motion = 30;  // "               " radar sensor output
const int dirPin = 2;   // "               " stepper driver directional input
const int stepPin = 3;  // "               " stepper driver step command pin

int fState = 0;      // state of the forward pin
int rState = 0;      // "          " reverse pin
int parkState = 0;   // "          " park brake pin
int motionState = 0; // "          " motion sensor

void setup() {
  // initialize the, stepper direction, stepper step, and motion indicator(LED_BUILTIN) as an output:
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(motInd, OUTPUT);
  // intitialize the motion, forward, reverse, and park brake as inputs
  pinMode(motion, INPUT);
  pinMode(fPin, INPUT);
  pinMode(rPin, INPUT);
  pinMode(parkPin, INPUT);
}

void loop() { // how do I make my motor turn 6 steps forward or backward, then go 6 steps back to "home" when shifter in in "neutral"

  fState = digitalRead(fPin);        // read state of forward pin
  rState = digitalRead(rPin);        // read state of reverse pin
  motionState = digitalRead(motion); // read state of motion sensor
  parkState = digitalRead(parkPin);  // read state of park brake

  // Check if motion is sensed. If it is, the motion indicator is HIGH:
  if (motionState == HIGH) {
    // turn LED on:
    digitalWrite(motInd, HIGH);
  } else {
    // turn LED off:
    digitalWrite(motInd, LOW);
  }

  // Check if shifter is in forward position. If it is, the forward pin is HIGH:
  // If shifter is moved into forward while park brake is on or motion is sensed then no shift will occur.
  if (fState == HIGH && motionState == LOW && parkState == LOW) {
    // move shifter lever to forward
    digitalWrite(dirPin, HIGH);
    //***** Need to make a rotation of 11 degrees(6 steps) here please help*****
    for (int x = 0; x < 200; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }
  }

  // Check if shifter is in reverse position. If it is, the forward pin is HIGH:
  // If shifter is moved into reverse while park brake is on or motion is sensed then no shift will occur.
  if (rState == HIGH && motionState == LOW && parkState == LOW) {
    //move shift lever to reverse
    digitalWrite(dirPin, LOW);
    // *****Need to make a rotation of -11 degrees(6 steps) here please help*****
    for (int x = 0; x < 200; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1100);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1100);
    }
  }
}

Good job with code tags on your first post.

I am micro stepping ... so 98 microsteps should be about 11 degrees.

Why then do you move 200 steps?

//***** Need to make a rotation of 11 degrees(6 steps) here please help*****
    for (int x = 0; x < 200; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(1000);
    }

When i set it at 6 or when I set up micro stepping and set it at 98 it makes no difference. It just rotates in either direcrion and does not stop.

And thanks btw. I did read the rules and guides. Just want to be respectful to people that are giving me free advice.

Your movements are inside of this conditional statement

 if (fState == HIGH && motionState == LOW && parkState == LOW) {

If one of the conditions does not change after the move, it will keep repeating.

Can you add some Serial printing to your sketch to test the state of your variables, and check that the conditions are as you expect them to be.

I'll reveiw my operators and conditions more and see if i can figure out serial print out.
How are my movements there?
My understanding of that section is that im stating If my shifter is in forward, the park brake is off and there is no motion then move. 1 revolution, but it just keeps spinning.

Its not really setup right to ignore motion when it moves.
Motion should prevent move when forward is activated and park brake should make it go back to where it was.
Im not sure how to set that up.

I try to avoid using complex IF statements like this

if (fState == HIGH && motionState == LOW && parkState == LOW) {

because there are 7 ways for it to be wrong and you have no means to know which one it is

I would program this something like

if (parkState == LOW) { // I'm putting it first because it seems the most important case
  // do stuff
}
else {
   if (motionState == LOW) { 
        
     if (fState == HIGH) {
         
     }
     
     if (rState == HIGH) {

     }
}

I don't know enough about your system to put these in the correct order - I'm just trying to illustrate my concept.

...R

My if statements do their job for now.

When I move the switch to forward i go (~11 degrees) 98 steps forward, reverse: 98 steps back, neutral: return to 0.

This unit is now functioning as desired.
I'm not sure the acceleration feature is needed, but I'm gonna experiment with torque capabilities, various step levels, etc. I will probably have to upgrade to NEMA 23 and bigger driver, but the programming wont change any.

The purpose of this module it to shift a transmission forward or reverse if the vehicle is not moving or the park brake is off so that simple minded operators cannot destroy the transmission. I will also be adding some other inputs for throttle so that it cannot be rev dropped into gear and do wheelies or burnouts...

I am aware that none of this hardware is correct for an industrial application. I will design board based on what I'm using now, I just needed to prove my design and get the code down.

Any advice on a heavier duty harware ie; vibration resistance, temp resistance(-40 to 240 deg F), IP 68 enclosure... I will probably cast the whole thing into resin with heat sink for the driver and stepper.

Thanks for the help.

/* FNR Electronic Shift module
   by Mariobro_3 Feb. 2020
  This code is to make a stepper motor move a lever forward, reverse, or back to neutral,
  when no motion is detected and the park brake is off.
  When the park brake is active while the shifter is F or R, it should return to N
 **Hardware used:
   Arduino Mega 2560
   A4988 clone stepper driver
   RCWl-0516 radar motion sensor
   directional shifter switch (FNR) "N" has no output
   latching park brake switch
   NEMA 17 stepper motor
   10k pullup resistors
   100uF cap for 12 volt motor power
   12 volt 5 amp power supply
*/

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 3, 2);

const int fPin = 10;    // the number of the forward pin
const int rPin = 11;    // "               " reverse pin
const int parkPin = 12; // "               " park brake pin
const int motInd =  13; // "               " motion indicator(LED_BUILTIN)
const int motion = 30;  // "               " radar sensor output

int fState = 0;      // state of the forward pin
int rState = 0;      // "          " reverse pin
int parkState = 0;   // "          " park brake pin
int motionState = 0; // "          " motion sensor
int forward = 98;         // ~11 degrees of rotation
int neutral = 0;
int reverse = -98;

void setup() {
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(1000);
  // initialize the, stepper direction, stepper step, and motion indicator(LED_BUILTIN) as an output:
  pinMode(motInd, OUTPUT);
  // intitialize the motion, forward, reverse, and park brake as inputs
  pinMode(motion, INPUT);
  pinMode(fPin, INPUT);
  pinMode(rPin, INPUT);
  pinMode(parkPin, INPUT);
}

void loop() { // how do I make my motor turn 6 steps forward or backward, then go 6 steps back to "home" when shifter in in "neutral"

  fState = digitalRead(fPin);        // read state of forward pin
  rState = digitalRead(rPin);        // read state of reverse pin
  motionState = digitalRead(motion); // read state of motion sensor
  parkState = digitalRead(parkPin);  // read state of park brake

  // Check if motion is sensed. If it is, the motion indicator is HIGH:
  if (motionState == HIGH) {
    // turn LED on:
    digitalWrite(motInd, HIGH);
  } else {
    // turn LED off:
    digitalWrite(motInd, LOW);
  }

  // Check if shifter is in forward position. If it is, the forward pin is HIGH:
  // If shifter is moved into forward while park brake is on or motion is sensed then no shift will occur.
  if ((fState == HIGH) && (motionState == LOW) && (parkState == LOW)) {
    if (stepper.distanceToGo() == 0)
    {
      stepper.moveTo(forward); \
    }
    stepper.run();
  }
  // Check if shifter is in forward position. If it is, the forward pin is HIGH:
  // If shifter is moved into forward while park brake is on or motion is sensed then no shift will occur.
  if ((rState == HIGH) && (motionState == LOW) && (parkState == LOW)) {
    if (stepper.distanceToGo() == 0)
    {
      stepper.moveTo(reverse); \
    }
    stepper.run();
  }
  // Check if shifter is in neutral position(forward and reverse pins are low) if true then move back to home(pos = 0)
   if ((fState == LOW) && (rState == LOW)) {
    if (stepper.distanceToGo() == 0)
    {
      stepper.moveTo(neutral); \
    }
    stepper.run();
  }
  // Check if park brake is on while shifter is forward, if true then return to nuetral
  if ((fState == HIGH) && (parkState == HIGH)) {
    if (stepper.distanceToGo() == 0)
    {
      stepper.moveTo(neutral); \
    }
    stepper.run();
  }
  // Check if park brake is on while shifter is reverse, if true then return to nuetral
  if ((rState == HIGH) && (parkState == HIGH)) {
    if (stepper.distanceToGo() == 0)
    {
      stepper.moveTo(neutral); \
    }
    stepper.run();
  }
}