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);
}
}
}