I need help with my code, I am using a Unipolar 8 lead stepper motor,Arduino Uno and a DM860H motor drive. My aim is to crate a code that will turn the motor when I receive a signal from the truck's camera system. The signal must tell me how much the truck is off the lane of the road and I have to convert that into steps the motor will turn. The problem is the motor is not turning. Also need advice on how should connect between the arduino and motor driver . Can you please kindly assist on what I should do. Below is the code I used
#include <Stepper.h>
int LeftSpeaker;
int RightSPeaker;
int LaneSensorInput;
int LaneDifference;
int WheelAngle;
int NWheelAngle;
int WheelAngleChange;
int Speed;
int StepsPerRevolution ;
int Motorangle;
const int stepsPerRevolution = 500; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(StepsPerRevolution, 8, 9, 10, 11);
void setup() {
// set the speed at 100 rpm:
myStepper.setSpeed(100);
// initialize the serial port:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
// based on the truck speed you can attain the blue line.
//based on the steering angle you can attain the orange line.
// read the input on analog pin 0:
int RightSpeaker = analogRead(A0);
// read the input on analog pin 4:
int LeftSpeaker = analogRead(A4);
//read the pwm on AO
analogWrite(3, RightSpeaker/4);
//read the pwm on A1
analogWrite(5, LeftSpeaker/4);
Serial.println("RS,LS:");
Serial.print(RightSpeaker);
Serial.print(",");
Serial.print(LeftSpeaker);
LaneDifference = LeftSpeaker - RightSpeaker; //calculate the orange line
LaneDifference = LaneDifference * 1.5/1023; //convert to meters
if (LaneDifference > 0)
{
LaneSensorInput == 1; // too far left
if (Speed >= 55 && Speed <65){
WheelAngleChange = tan (8/LaneDifference);
NWheelAngle=WheelAngle - WheelAngleChange; // new steering angle
}
if (Speed >= 65 && Speed < 75){
WheelAngleChange = tan (16/LaneDifference);
NWheelAngle = WheelAngle -WheelAngleChange; //new steering angle
}
if (Speed >= 75 && Speed <=85){
WheelAngleChange = tan (18/LaneDifference);
NWheelAngle = WheelAngle - WheelAngleChange; //new steering angle
}
Motorangle = NWheelAngle * 12/60 ;
StepsPerRevolution = Motorangle * 200/360;
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(StepsPerRevolution);
delay(100);
}
if (LaneDifference < 0) {
LaneSensorInput == 0; // too far right
if (Speed >= 55 && Speed <65){
WheelAngleChange = tan (8/LaneDifference);
NWheelAngle=WheelAngle + WheelAngleChange; // new steering angle
}
if (Speed >= 65 && Speed < 75){
WheelAngleChange = tan (16/LaneDifference);
NWheelAngle = WheelAngle + WheelAngleChange; // new steering angle
}
if (Speed >= 75 && Speed <=85){
WheelAngleChange = tan (18/LaneDifference);
NWheelAngle = WheelAngle + WheelAngleChange; // new steering angle
}
Motorangle = NWheelAngle * 12/60;
StepsPerRevolution = Motorangle * 200/360;
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(StepsPerRevolution);
delay(100);
}
}