Help with joystick control of L298N/Motors

Hi,
after a little advice where i may be going wrong with my code.
im trying to control two DC motors, directionaly, through an H-bridge with a joystick,
the code i am using is modified from a tutorial on ‘How to Mechatronics’,
now before you start, i understand that modifying code is no way to learn and will generally only serve to confuse me but I have limited time to get this project together for proof of concept and starting at square one isnt going to help me.

Essentially the code from ‘How to Mechatronics’ controls both motors forward and back wards together
then varies the speed of each motor to control left and right,
the function i need us to control one motor as the y-axis and one motor as the x-axis, similar to a cnc or printer but manually.

original code:
/*  Arduino DC Motor Control - PWM | H-Bridge | L298N
        Example 02 - Arduino Robot Car Control
   by Dejan Nedelkovski, www.HowToMechatronics.com
*/

#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
 pinMode(enA, OUTPUT);
 pinMode(enB, OUTPUT);
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 pinMode(in3, OUTPUT);
 pinMode(in4, OUTPUT);
}

void loop() {
 int xAxis = analogRead(A0); // Read Joysticks X-axis
 int yAxis = analogRead(A1); // Read Joysticks Y-axis

 // Y-axis used for forward and backward control
 if (yAxis < 470) {
   // Set Motor A backward
   digitalWrite(in1, HIGH);
   digitalWrite(in2, LOW);
   // Set Motor B backward
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);
   // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
   motorSpeedA = map(yAxis, 470, 0, 0, 255);
   motorSpeedB = map(yAxis, 470, 0, 0, 255);
 }
 else if (yAxis > 550) {
   // Set Motor A forward
   digitalWrite(in1, LOW);
   digitalWrite(in2, HIGH);
   // Set Motor B forward
   digitalWrite(in3, LOW);
   digitalWrite(in4, HIGH);
   // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
   motorSpeedA = map(yAxis, 550, 1023, 0, 255);
   motorSpeedB = map(yAxis, 550, 1023, 0, 255);
 }
 // If joystick stays in middle the motors are not moving
 else {
   motorSpeedA = 0;
   motorSpeedB = 0;
 }

 // X-axis used for left and right control
 if (xAxis < 470) {
   // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
   int xMapped = map(xAxis, 470, 0, 0, 255);
   // Move to left - decrease left motor speed, increase right motor speed
   motorSpeedA = motorSpeedA - xMapped;
   motorSpeedB = motorSpeedB + xMapped;
   // Confine the range from 0 to 255
   if (motorSpeedA < 0) {
     motorSpeedA = 0;
   }
   if (motorSpeedB > 255) {
     motorSpeedB = 255;
   }
 }
 if (xAxis > 550) {
   // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
   int xMapped = map(xAxis, 550, 1023, 0, 255);
   // Move right - decrease right motor speed, increase left motor speed
   motorSpeedA = motorSpeedA + xMapped;
   motorSpeedB = motorSpeedB - xMapped;
   // Confine the range from 0 to 255
   if (motorSpeedA > 255) {
     motorSpeedA = 255;
   }
   if (motorSpeedB < 0) {
     motorSpeedB = 0;
   }
 }
 // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
 if (motorSpeedA < 70) {
   motorSpeedA = 0;
 }
 if (motorSpeedB < 70) {
   motorSpeedB = 0;
 }
 analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
 analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

I started with up loading the original sketch which obviously worked fine, then i removed the x-axis and
motorSpeedB from the code, so that only the Y-axis drove motor A, which worked fine
then i copied the code for the Y-axis but changed the names to x-axis/motor b 
new code:
   #define enA 9
   #define in1 4
   #define in2 5
   #define enB 10
   #define in3 6
   #define in4 7
   int motorSpeedA = 0;
   int motorSpeedB = 0;
   void setup() {
     pinMode(enA, OUTPUT);
     pinMode(enB, OUTPUT);
     pinMode(in1, OUTPUT);
     pinMode(in2, OUTPUT);
     pinMode(in3, OUTPUT);
     pinMode(in4, OUTPUT);
   }
   void loop() {
     int xAxis = analogRead(A0); // Read Joysticks X-axis
     int yAxis = analogRead(A1); // Read Joysticks Y-axis
     // Y-axis used for forward and backward control
     if (yAxis < 470) {
       // Set Motor A backward
       digitalWrite(in1, HIGH);
       digitalWrite(in2, LOW);
       // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
       motorSpeedA = map(yAxis, 470, 0, 0, 255);
     }
     else if (yAxis > 550) {
       // Set Motor A forward
       digitalWrite(in1, LOW);
       digitalWrite(in2, HIGH);
       // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
       motorSpeedA = map(yAxis, 550, 1023, 0, 255);
     }
     // If joystick stays in middle the motors are not moving
     else {
       motorSpeedA = 0;
       motorSpeedB = 0;
     }
     }
     int xAxis = analogRead(A0); // Read Joysticks X-axis
     // Y-axis used for forward and backward control
     if (xAxis < 470) {
       // Set Motor B backward
       digitalWrite(in3, HIGH);
       digitalWrite(in4, LOW);
      
       motorSpeedB = map(xAxis, 470, 0, 0, 255);
     }
     else if (xAxis > 550) {
     
       // Set Motor B forward
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
       // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
      
       motorSpeedB = map(xAxis, 550, 1023, 0, 255);
     }
     // If joystick stays in middle the motors are not moving
     else {
       motorSpeedA = 0;
       motorSpeedB = 0;
     }
   
     analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
     analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
   }

this how ever does not operate correctly, if i wiggle the joystick around randomly it will motor somewhat randomly and ever continue running in the rest position until the joystick is flicked…
any suggestions?

thanks and i apologize for being a newbie-code copying-tool but its a means to an end

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

// If joystick stays in middle the motors are not moving
       {
        motorSpeedA = 0;
        motorSpeedB = 0;
      }

This is if BOTH x and y are in the middle, but if this is in the Xaxis code then ONLY the x axis motor speed should = 0.
Not the Yaxis as well.

The Y axis it should be taken care of in the y axis code.

Please change the variable names to Xaxis and Yaxis type terms, you will not need as many comments.

// If Xaxis joystick stays in middle the motors are not moving
        {
        XaxisMotorSpeed = 0;
       }


// If Yaxis joystick stays in middle the motors are not moving
        {
        YaxisMotorSpeed = 0;
       }

Thanks.. Tom... :slight_smile:

Legend Tom,
sorry with regards to the posting of code etc,
i was in a hurry to get this question up before work this morning and didnt go through the correct motions,
will correct this and add the info etc (quick lunchbreak check) when i get home tonight

cheers again
Joseph

well, tidied up my code a bit and it works... almost too easy, thank you @TomGeorge

My next mission is to add a third directional motor via H-bridge using the same joystick,
i.e move motor 'A' directionaly along a z axis, hit the joystick button, then move motors 'B' and 'C' directionaly along the x & y axis

il hav a play with this first then no doubt be back with queries
thanks again

im having abit of trouble structuring the code for this,
essentially i would like the joystick to control the direction of a single motor,
then, after pushing the joystick button, control a directional motor via the x axis and another directional motor with the y axis.

ive taken the code that works for 2 motors, one H-bridge
and added a second h-bridge/motor

  #define enA 9
    #define in1 4
    #define in2 5
    #define enB 10
    #define in3 6
    #define in4 7
    int motorSpeedA = 0;
    int motorSpeedB = 0;
    void setup() {
      pinMode(enA, OUTPUT);
      pinMode(enB, OUTPUT);
      pinMode(in1, OUTPUT);
      pinMode(in2, OUTPUT);
      pinMode(in3, OUTPUT);
      pinMode(in4, OUTPUT);
    }

and added

  #define enA 9
    #define in1 4
    #define in2 5
    #define enB 10
    #define in3 6
    #define in4 7
    #define enC 2
    #define in5 3
    #define in6 8
    #define sw 11
    int motorSpeedA = 0;
    int motorSpeedB = 0;
    int motorSpeedC = 0;
    void setup() {
      pinMode(enA, OUTPUT);
      pinMode(enB, OUTPUT);
      pinMode(enC, OUTPUT);
      pinMode(in1, OUTPUT);
      pinMode(in2, OUTPUT);
      pinMode(in3, OUTPUT);
      pinMode(in4, OUTPUT);
      pinMode(in5, OUTPUT);
      pinMode(in6, OUTPUT);
      pinMode(sw, INPUT);
    }

about here i get a bit stuck, do i need to introduce DigitalRead from the Switch first and monitor its state before proceding or introduce all Read’s

}
    void loop() {
        int sw    = digitalRead(10); // Read switch state
       // if joystick button is pressed, control switches to x & y axis
      { if (sw, LOW)
    }
      int zAxis = analogRead(A0); // Read Joysticks X-axis
      int xAxis = analogRead(A0); // Read Joysticks X-axis
      int yAxis = analogRead(A1); // Read Joysticks Y-axis

or am i better stating something like this

 void loop() {
      int sw    = digitalRead(10); // Read switch state
      int zAxis = analogRead(A0); // Read Joysticks X-axis
      int xAxis = analogRead(A0); // Read Joysticks X-axis
      int yAxis = analogRead(A1); // Read Joysticks Y-axis
    }
    {   // if joystick button is pressed, control switches to x & y axis
       if (sw, LOW) {
    }

my complete/incomplete code is

    #define enA 9
    #define in1 4
    #define in2 5
    #define enB 10
    #define in3 6
    #define in4 7
    #define enC 2
    #define in5 3
    #define in6 8
    #define sw 11
    int motorSpeedA = 0;
    int motorSpeedB = 0;
    int motorSpeedC = 0;
    void setup() {
      pinMode(enA, OUTPUT);
      pinMode(enB, OUTPUT);
      pinMode(enC, OUTPUT);
      pinMode(in1, OUTPUT);
      pinMode(in2, OUTPUT);
      pinMode(in3, OUTPUT);
      pinMode(in4, OUTPUT);
      pinMode(in5, OUTPUT);
      pinMode(in6, OUTPUT);
      pinMode(sw, INPUT);
    }
    void loop() {
      int sw    = digitalRead(10); // Read switch state
      int zAxis = analogRead(A0); // Read Joysticks X-axis
      int xAxis = analogRead(A0); // Read Joysticks X-axis
      int yAxis = analogRead(A1); // Read Joysticks Y-axis
    }
    {   // if joystick button is pressed, control switches to x & y axis
       if (sw, LOW) {
    }
    

      // z-axis used for forward and backward control of motor c
      if (zAxis < 470) {
        // Set Motor C backward
        digitalWrite(in5, HIGH);
        digitalWrite(in6, LOW);
        // Convert the declining z-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedC = map(zAxis, 470, 0, 0, 255);
      }
      else if (zAxis > 550) {
        // Set Motor c forward
        digitalWrite(in5, LOW);
        digitalWrite(in6, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedC = map(zAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedc = 0;
      } 
        
      // Y-axis used for forward and backward control
      if (yAxis < 470) {
        // Set Motor A backward
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 470, 0, 0, 255);
      }
      else if (yAxis > 550) {
        // Set Motor A forward
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedA = 0;
      // Y-axis used for forward and backward control
      if (yAxis < 470) {
        // Set Motor A backward
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 470, 0, 0, 255);
      }
      else if (yAxis > 550) {
        // Set Motor A forward
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedA = 0;
      }
      if (xAxis < 470) {
        // Set Motor B backward
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
       
        motorSpeedB = map(xAxis, 470, 0, 0, 255);
      }
      else if (xAxis > 550) {
      
        // Set Motor B forward
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
       
        motorSpeedB = map(xAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedB = 0;
      }
    
      analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
      analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
      analogWrite(enC, motorSpeedC); // Send PWM signal to motor C
    }

so im stuck somewhere in the middle there really…

any help greatly appreciated, sorry for the lack of understanding im really just tryna hammer fist my way through coding

       if (sw, LOW) {

I think you meant to use == there.

Leave this code for a moment and work on the button alone. Look in your Arduino Example folder for StateChange. Load that up and make it work with your button.

MorganS:
I think you meant to use == there.

Leave this code for a moment and work on the button alone. Look in your Arduino Example folder for StateChange. Load that up and make it work with your button.

haha thanks, i started 'thinking' i was just going to spew out some appropriate code but my brain melted and I went to bed instead,
thank you for the suggestion, i will work with that...... after work :[
cheers

ok this is what i have so far,

const int  4 = 2;    // the pin that the pushbutton is attached to

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
 pinMode(4, INPUT);
 Serial.begin(9600);
}

void loop() {
    buttonState = digitalRead(buttonPin);

    if (buttonState != lastButtonState) {
      if (buttonState == LOW) {
        buttonPushCounter++;
        Serial.println("on");
        Serial.print("number of button pushes: ");
        Serial.println(buttonPushCounter);
      }else{
        Serial.println("off");
      }
      delay(50);
    }
    lastButtonState = buttonState;
    if (buttonPushCounter % 1 == 0) }
        }

im guessing it would go something like

if (buttonPushCounter % 1 == 0)}
     analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
      analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}else{
   analogWrite(enC, motorSpeedC); // Send PWM signal to motor C

am i on the right track?

something along these lines

   #define enA 9
    #define in1 4
    #define in2 5
    #define enB 10
    #define in3 6
    #define in4 7
    #define enC 2
    #define in5 3
    #define in6 8
    
    const int  buttonPin = 2;    // the pin that the pushbutton is attached to

    int buttonPushCounter = 0;   // counter for the number of button presses
    int buttonState = 0;         // current state of the button
    int lastButtonState = 0;     // previous state of the button
    int motorSpeedA = 0;
    int motorSpeedB = 0;
    int motorSpeedC = 0;
    void setup() {
      pinMode(4, INPUT);
      Serial.begin(9600);
      pinMode(enA, OUTPUT);
      pinMode(enB, OUTPUT);
      pinMode(enC, OUTPUT);
      pinMode(in1, OUTPUT);
      pinMode(in2, OUTPUT);
      pinMode(in3, OUTPUT);
      pinMode(in4, OUTPUT);
      pinMode(in5, OUTPUT);
      pinMode(in6, OUTPUT);
    }
    void loop() {
      int sw    = digitalRead(10); // Read switch state
      int zAxis = analogRead(A0); // Read Joysticks X-axis
      int xAxis = analogRead(A0); // Read Joysticks X-axis
      int yAxis = analogRead(A1); // Read Joysticks Y-axis
    }
    
   
      // z-axis used for forward and backward control of motor c
     if (zAxis < 470) {
        // Set Motor C backward
        digitalWrite(in5, HIGH);
        digitalWrite(in6, LOW);
        // Convert the declining z-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedC = map(zAxis, 470, 0, 0, 255);
      }
      else if (zAxis > 550) {
        // Set Motor c forward
        digitalWrite(in5, LOW);
        digitalWrite(in6, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedC = map(zAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedc = 0;
      } 
        
      // Y-axis used for forward and backward control
}      if (yAxis < 470) {
        // Set Motor A backward
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 470, 0, 0, 255);
      }
      else if (yAxis > 550) {
        // Set Motor A forward
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedA = 0;
      // Y-axis used for forward and backward control
      if (yAxis < 470) {
        // Set Motor A backward
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 470, 0, 0, 255);
      }
      else if (yAxis > 550) {
        // Set Motor A forward
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
        motorSpeedA = map(yAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedA = 0;
      }
      if (xAxis < 470) {
        // Set Motor B backward
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
       
        motorSpeedB = map(xAxis, 470, 0, 0, 255);
      }
      else if (xAxis > 550) {
      
        // Set Motor B forward
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
       
        motorSpeedB = map(xAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedB = 0;
      }
    if (buttonState != lastButtonState) {
      if (buttonState == LOW) {
        buttonPushCounter++;
        Serial.println("on");
        Serial.print("number of button pushes: ");
        Serial.println(buttonPushCounter);
      }else{
        Serial.println("off");
      }
      delay(50);
    }
    if (buttonPushCounter % 1 == 0)}
     analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
      analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}else{
   analogWrite(enC, motorSpeedC); // Send PWM signal to motor C
    }

You could at least try compiling these things before posting them. Code that won't even compile never works.

I ignored the first 2 bits of code. Anything that tries to set "4 = 2" is obviously rubbish and not worth posting.

In the 3rd why is there no pinMode() for buttonPin and why is pin 4 set as INPUT and then a few lines later (when you call it "in1") as OUTPUT?

Why do you read the same pin A0 twice into two different variables, one of which apparently controls Motor C and the other Motor A?

BTW buttonPushCounter is an integer. So "buttonPushCounter % 1 == 0" is ALWAYS going to be true. The remainder when you divide an integer by 1 can't be anything but 0. But anyway an if statement immediately followed by } was never going to do anything usefu and there's not going to be anything useful in that counter because you don't ever read buttonPin.

In general your {} curly brackets are in a mess. loop() ends after about 4 statements and everything after that is outside any function so illegal. Try using Ctrl-T in the IDE and take note of the indenting and what the compiler is trying to tell you.

Steve

if (buttonPushCounter % 1 == 0)}

analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
      analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}else{
  analogWrite(enC, motorSpeedC); // Send PWM signal to motor C

Yes, but...

  1. You used } where there should be a {

  2. You could use %2 instead of %1. But you should not have used a counter in the first place. Have the button code save a true/false value back when the button was pushed.

Steve - i appreciate your time, but a little aggressive dont ya think... rubbish? kinda sound like a tired school teacher.
I generally do attempt to 'verify' or 'compile' but i made the '4 = 2' mod after the fact, this was a typo when i realised my button was attached to pin 4 not pin 2 and i changed the wrong comment, it was late.

In the 3rd why is there no pinMode() for buttonPin and why is pin 4 set as INPUT and then a few lines later (when you call it "in1") as OUTPUT?

good point, and thank you, i assume this is generally the point of a 'guidance forum' something i can now work on

Why do you read the same pin A0 twice into two different variables, one of which apparently controls Motor C and the other Motor A?

Im trying to make the same joystick, input A0 control 2 seperate motors in seperate scenarios?

wow Ctrl-T tidies things up a bit

MorganS

  1. You used } where there should be a {

  2. You could use %2 instead of %1. But you should not have used a counter in the first place. Have the button code save a true/false value back when the button was pushed.

thanks for pointing out the bracket :smiley: as for not using the counter i will look into this, it was just what i had gathered from the 'StateChange' example

Righto,
my plan has changed slightly as the transmission of drive, from the DC motor that i have to the screw shaft thingy, was causing more hassle than it needed to at this point.
so, Ive changed to a stepper motor (NEMA 17) as it has a nice 5mm output shaft with a flat and a 5mm to 8mm flex coupler is cheap as.

I am still using the L298N as the stepper controller.

Ive managed to work my way through various examples and have come up with a sketch to control forward and reverse with slight speed control via a joystick which im somewhat happy with.

However, I have three concerns

  1. my stepper seems particularly noisy and im unsure if this is normal or not

  2. my stepper moves fast/smooth in one direction but not as smoothly in the other, I wondered if this was wiring related but I have tried every combination of Stepper wiring with no difference, if i swap the inputs from the Arduino the same happens in the opposite direction,
    in one case clockwise is smooth, anti-clockwise is not, if i swap the inputs clockwise is not smooth but anti is… this also seems to be input related, if i move the joystick in very slight increments the sound of the motor appears smooth until a certain point at which the tone changes…
    I will try add a video

  3. Can I increase the speed/torque with the L298N, it appears very easy to stall

Thoughts, queries, theories, concerns?
cheers all
Joseph

#include <Stepper.h>

const int stepsPerRevolution = 180;


Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int yAxis = analogRead(A1); // Read Joysticks Y-axis
  int motorSpeed = map(yAxis, 0, 1023, 0, 100);
  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeed = map(yAxis, 470, 0, 0, 100);
    myStepper.step(stepsPerRevolution / 100);
  }
  else if (yAxis > 550) {
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeed = map(yAxis, 550, 1023, 0, 100);
    myStepper.step(-stepsPerRevolution / 100);
  }
  else if (yAxis = 470, 550) {
    motorSpeed = map(yAxis, 470, 550, 0, 0);
  }
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:

  }
  Serial.println(yAxis);
  delay(1);
}

Hi,

  else if (yAxis = 470, 550) {

Not sure what you are trying to do here. == for a start?

I think

if (yAxis > 470 && yAxis < 550)
{
motorSpeed = 0;
}

May be what you are trying to do?
Tom… :slight_smile:

Hey Tom,
the stepper was continuing to motor with the joystick at rest so i was attempting to instuct the controller that readings between 470 and 550 should be mapped to 0 and 0, this has done what i intended it to do as the stepper no longer motors with the joystick at rest.
What is the difference between a single '=' and a double?

i'l try your suggested code and see what it does,
thanks again

Switched my piece of code with yours and it made zero noticeable difference.

cheers
Joseph

Hi,

x = 470 assigns the value 470 to the variable x
if x was 100 it will be changed to 470.

x == 470 compares the values of x and 470 to see if they are equal
if x was 100 it will stay 100.

Where did you find this if statement structure?

if (yAxis = 470, 550)

Tom... :slight_smile:

Hi Tom,
sorry i just made some assumptions really and tried it out....
obviously wanting the motor speed to equal zero if the input was between 470 and 550 (at rest)

cheers
Joseph

Joe_duino:
I am still using the L298N as the stepper controller.

However, I have three concerns

  1. my stepper seems particularly noisy and im unsure if this is normal or not

  2. my stepper moves fast/smooth in one direction but not as smoothly in the other, I wondered if this was wiring related but I have tried every combination of Stepper wiring with no difference, if i swap the inputs from the Arduino the same happens in the opposite direction,
    in one case clockwise is smooth, anti-clockwise is not, if i swap the inputs clockwise is not smooth but anti is… this also seems to be input related, if i move the joystick in very slight increments the sound of the motor appears smooth until a certain point at which the tone changes…
    I will try add a video

  3. Can I increase the speed/torque with the L298N, it appears very easy to stall.

All of those concerns will be removed by using a proper stepper driver. The modules from Pololu are very easy to use and very efficient.

Morgan, i wondered that. only utillised the L298N as its what i had on hand
cheers

Joseph

Wow, the A4988 certainly made things tidier.
Been playing around with a couple example sketches and have managed to throw together this

#define ENABLE 6
#define STEP 4
#define DIR 2
#define YAXIS A0
#define STEPS_PER_ROTATION 200
void setup() {

  pinMode(ENABLE, OUTPUT);
  pinMode(STEP, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(YAXIS, INPUT);

}
void stepNow(int totalSteps) {
  Serial.print(totalSteps);
  Serial.println(F(" steps."));

  int i;
  for (i = 0; i < totalSteps; ++i) {
    digitalWrite(STEP, HIGH);
    delayMicroseconds(700);
    digitalWrite(STEP, LOW);
    delayMicroseconds(700);
  }
}

void clockwise() {
  digitalWrite(DIR, LOW);
  stepNow(STEPS_PER_ROTATION);
}

void anticlockwise() {
  digitalWrite(DIR, HIGH);
  stepNow(STEPS_PER_ROTATION);
}

void loop() {
  int YAXIS = analogRead(A0);
  if (YAXIS < 450) {
    digitalWrite (ENABLE, LOW);
    clockwise();
 
    
  }
  else if (YAXIS > 570) {
    digitalWrite (ENABLE, LOW);
    anticlockwise();

   
  }
  else {
    digitalWrite (ENABLE, HIGH);
  }
  }

It gives me directional control of my stepper which is a good start,
going further from here tho some variance in speed would be nice,
its just full tit for complete rotations,
no ‘soft start’ as such
ive had a few plays with getting speed mapping with no luck, any suggestions for examples etc

cheers
Joseph