I'm a beginner. Can anyone see what the problem is in this code? The components that I use has worked with other code so there's nothing wrong with them. The stepper motor doesn't run at all no matter how I manipulate the joystick.
/* This program lets the X-axis on a joystick controll the speed and direction
of a stepper motor called A. This is done without delays that stops the loop of the program. */
int dirApin = 2; //Direction
int stepApin = 4; //Step
int potXpin = 0; //Joystick
long int PulseADelay = 1000; //Change if nessesary
long int SlowestSpeed = 1000; //The biggest allowed delay between steps
long int LastAPulse = 0; //Time since last step
// Controls motor A
void setup()
{
pinMode(dirApin, OUTPUT);
pinMode(stepApin, OUTPUT);
pinMode(potXpin, INPUT);
}
void loop()
{
PulseADelay = analogRead(potXpin); //Reads the joystick
//Sets direction and calculates the delay between the step
if (PulseADelay < 511)
{
dirApin = HIGH; //Sets the direction of motor
PulseADelay = map(PulseADelay, 511, 0, 1000, 15); //Maps the Pulse Delay
} else {
dirApin = LOW; //Sets the direction of motor
PulseADelay = map(PulseADelay, 512, 1023, 1000, 15); //Change last two numbers if nessesary
}
if (PulseADelay < SlowestSpeed) //Stops the motor if the delay is to long
if ((millis() - LastAPulse) > PulseADelay) //Determans if its time to make a new step
digitalWrite(stepApin, LOW); //This LOW to HIGH change is what creates the
digitalWrite(stepApin, HIGH); //"Rising Edge" so the easydriver knows to when to step.
LastAPulse = millis(); //Writes the time of when the step was done
}
if (PulseADelay < SlowestSpeed) //Stops the motor if the delay is to long
if ((millis() - LastAPulse) > PulseADelay) //Determans if its time to make a new step
digitalWrite(stepApin, LOW); //This LOW to HIGH change is what creates the
digitalWrite(stepApin, HIGH); //"Rising Edge" so the easydriver knows to when to step.
LastAPulse = millis(); //Writes the time of when the step was done
I am finding it very difficult to determine which statements you intend to execute when the if statements are true, as the code block is not in braces. I know that not using braces is valid if you only want one line to be executed as a result of the if, but the intention is so much clearer if you use them. Are you sure that the correct code is being executed as a result of the ifs ?
/* This program lets the axisis on a joystick controll the speed and direction
of two stepper motors called A and B. This is done without delays that stops the loop of the program. */
int dirApin = 2; //Direction
int stepApin = 4; //Step
int potXpin = 0; //Joystick
int PulseADelay = 100; //Change if nessesary
long int LastAPulse = 0; //Time since last step
int dirBpin = 7; //Direction
int stepBpin = 8; //Step
int potYpin = 1; //Joystick
int PulseBDelay = 100; //Change if nessesary
long int LastBPulse = 0; //Time since last step
int SlowestSpeed = 60; //The biggest allowed delay between steps
int BreakLow = 511; //Set the break point of joystick
int BreakHigh = BreakLow+1;
void setup()
{
pinMode(dirApin, OUTPUT);
pinMode(stepApin, OUTPUT);
pinMode(potXpin, INPUT);
pinMode(dirBpin, OUTPUT);
pinMode(stepBpin, OUTPUT);
pinMode(potYpin, INPUT);
}
void loop()
{
// Controls motor A
PulseADelay = analogRead(potXpin); //Reads the joystick
//Sets direction and calculates the delay between the step
if (PulseADelay < BreakLow)
{
digitalWrite(dirApin, HIGH); //Sets the direction of motor
PulseADelay = map(PulseADelay, BreakLow, 0, 70, 1); //Maps the Pulse Delay
} else {
digitalWrite(dirApin, LOW); //Sets the direction of motor
PulseADelay = map(PulseADelay, BreakHigh, 995, 70, 1); //Change last two numbers if nessesary
}
if (PulseADelay < SlowestSpeed) { //Stops the motor if the delay is to long
if ((millis() - LastAPulse) > PulseADelay) { //Determans if its time to make a new step
digitalWrite(stepApin, LOW); //This LOW to HIGH change is what creates the
digitalWrite(stepApin, HIGH); //"Rising Edge" so the easydriver knows to when to step.
LastAPulse = millis(); //Writes the time of when the step was done
}}
// Controls motor B
PulseBDelay = analogRead(potYpin); //Reads the joystick
//Sets direction and calculates the delay between the step
if (PulseBDelay < BreakLow)
{
digitalWrite(dirBpin, HIGH); //Sets the direction of motor
PulseBDelay = map(PulseBDelay, BreakLow, 0, 70, 1); //Maps the Pulse Delay
} else {
digitalWrite(dirBpin, LOW); //Sets the direction of motor
PulseBDelay = map(PulseBDelay, BreakHigh, 995, 70, 1); //Change last two numbers if nessesary
}
if (PulseBDelay < SlowestSpeed) { //Stops the motor if the delay is to long
if ((millis() - LastBPulse) > PulseBDelay) { //Determans if its time to make a new step
digitalWrite(stepBpin, LOW); //This LOW to HIGH change is what creates the
digitalWrite(stepBpin, HIGH); //"Rising Edge" so the easydriver knows to when to step.
LastBPulse = millis(); //Writes the time of when the step was done
}}
}
digitalWrite(stepApin, LOW); //This LOW to HIGH change is what creates the
digitalWrite(stepApin, HIGH); //"Rising Edge" so the easydriver knows to when to step.
to
digitalWrite(stepApin, HIGH); //This LOW to HIGH change is what creates the
digitalWrite(stepApin, LOW); //"Rising Edge" so the easydriver knows to when to step.
since stepper drivers assume +ve going pulses and you'll miss the very first pulse entirely.
Adding the braces has helped clarify the code blocks for the nested if statements but it would be even clearer formatted like this
if (PulseADelay < SlowestSpeed) //Stops the motor if the delay is to long
{
if ((millis() - LastAPulse) > PulseADelay) //Determans if its time to make a new step
{
digitalWrite(stepApin, LOW); //This LOW to HIGH change is what creates the
digitalWrite(stepApin, HIGH); //"Rising Edge" so the easydriver knows to when to step.
LastAPulse = millis(); //Writes the time of when the step was done
}
}