Notifications
Clear all

Controlling a third DC motor with Arduino fails

16 Posts
4 Users
0 Likes
3,502 Views
(@marcelinnorway)
Member
Joined: 4 years ago
Posts: 9
Topic starter  

I made a model dragline. Hoist- and dragmotor are controlled by two joysticks (those small Arduino-mushrooms), an Arduino and L298N motor driver. Using Bill's schematics and Arduino sketch he uses to drive two DC motors with those components and steers his small robot platform through the shop. Works allright with the dragline too. Now I added a third motor to rotate the dragline and copy pasted the sketch. The dragline rotates allright but hoist and dragmotors are silent, no reaction on moving the joysticks. Disabeling the program lines in the sketch that controls the rotating motor and voila, the hoist and dragmotors work again. Somebody here who can help me with that, and what do you need to help me out ?


   
Quote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @marcelinnorway

and what do you need to help me out ?

If you could post the code that would help. ? 

It sounds like there's something in the new code that is interfering with the original code.

Are you possibly using the same variable names?  That can easily mess things up.   Each code would run ok by itself, but when put together they interfere with each others variable.  Just guessing that would be my first guess.  There could be other issues as well, but without seeing the code it's hard to guess from here.

 

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

Just a tip on posting code if you're interested.

In the Arduino IDE right-click and choose "Select All" from the pop-up menu

Then right-click again and choose "Copy as HTML"

Then come back over to the forum post and choose the {;} source code button at the top of the editor.

Paste the HTML code from your clipboard into the pop up window and click on OK.

Then when you post  your code it will look real pretty and retain all the Arduino formatting.

 

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
(@marcelinnorway)
Member
Joined: 4 years ago
Posts: 9
Topic starter  

Thanks for the reply. Tried to follow your instructions on sending code, but it was not quite like you said. Was a lot of code allright, but not neatly arranged.

Tried to attach the sketch to the post, see if that works.

I don't have a schematic but here's a description

Two joysticks connected to Arduino. Using up-down movement on each one to activate either hoist motor or dragmotor. Joysticks connected to A0 and A1. That worked fine.

Now I took the left-right potmeter on one of the joysticks and used this to rotate the dragline. connected this one to A2 input on Arduino. Works fine that too.

Separately that is. Putting all together in one sketch is another story. No Go on that one. see sketch. Everything with the letters "Rot" is related to the rotating movement. Rotating with motor 3.


   
ReplyQuote
(@marcelinnorway)
Member
Joined: 4 years ago
Posts: 9
Topic starter  

Seriously.....looking at the skecth now.....am I missing an else statement after text "rotating the dragline" ?


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@marcelinnorway

A tip from me when copying and pasting code. Before opening the source code window add a couple of new lines in the message window, then make sure you place the cursor between </p> and <p> before you click paste!

Otherwise, you will get a real mess!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@marcelinnorway

Could you now comment out the lines you have added for the third motor? So the sketch is back in its original working state. Then repost the sketch.

You do know that you can drag and drop any file you post directly on to the message window, just place the cursor where you want the file to appear first.


   
ReplyQuote
(@marcelinnorway)
Member
Joined: 4 years ago
Posts: 9
Topic starter  

@pugwash

Disabeling the 3rd motor lines you mean? Or writing text in the sketch to explain what I've done?

 

 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@marcelinnorway

Disabling the 3rd motor lines you mean?

Yes, or adding a comment "//new line added" to the lines added to the sketch to control the third motor. Your original post says the things went haywire after adding the third motor. Therefore I assume, as does @robo-pi, that that the changes are the culprit. So to see where changes have been made would make debugging easier. 


   
ReplyQuote
(@marcelinnorway)
Member
Joined: 4 years ago
Posts: 9
Topic starter  

 

@pugwash

 

Ok added text NEW LINE to distinguish eh.. new lines. Tried to copy like you said but end up with this mess, see below. I attached the sketch to the post as well. You might notice I use :

MotorSpeed3 = map(joyposRot, 564, 1023, 0, 50);

for mapping rotation. I use 0, 50 instead of 0, 255 to slow down the motor. It's a motor that uses another voltage (4,5 volts) instead of the 6-12V I used for drag and hoist motor. It is rude, I know, but have no better solution yet.

 

 [code] /* L298N Motor Control Demonstration with Joystick L298N-Motor-Control-Demo-Joystick.ino Demonstrates use of Joystick control with Arduino and L298N Motor Controller DroneBot Workshop 2017 http://dronebotworkshop.com */ // Motor A int enA = 9; int in1 = 8; int in2 = 7; // Motor B int enB = 3; int in3 = 5; int in4 = 4; // NEW LINE Motor bathtub (bathtub is nickname for the big rotating table under the dragline int enBA = 11; int in5 = 12; int in6 = 13; // Joystick Input int joyVert = A0; // Vertical int joyHorz = A1; // Horizontal //NEW LINE int joyRot = A2; // Motor Speed Values - Start at zero int MotorSpeed1 = 0; int MotorSpeed2 = 0; //NEW LINE int MotorSpeed3 = 0; // Joystick Values - Start at 512 (middle position) int joyposVert = 512; int joyposHorz = 512; //NEW LINE int joyposRot = 512; void setup() { // Set all the motor control pins to outputs pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); //NEW LINES pinMode(enBA, OUTPUT); pinMode(in5, OUTPUT); pinMode(in6, OUTPUT); // Start with motors disabled and direction forward // Motor A digitalWrite(enA, LOW); digitalWrite(in1, HIGH); digitalWrite(in2, LOW); // Motor B digitalWrite(enB, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); //NEW LINE // Motor Bathtub digitalWrite(enBA, LOW); digitalWrite(in5, HIGH); digitalWrite(in6, LOW); } void loop() { // Read the Joystick X and Y positions joyposVert = analogRead(joyVert); joyposHorz = analogRead(joyHorz); //NEW LINE joyposRot = analogRead(joyRot); // Determine if this is a forward or backward motion // Do this by reading the Verticle Value // Apply results to MotorSpeed and to Direction if (joyposVert < 460) { // This is Backward // Set Motor A backward digitalWrite(in1, LOW); digitalWrite(in2, HIGH); //Determine Motor Speeds // As we are going backwards we need to reverse readings joyposVert = joyposVert - 460; // This produces a negative number joyposVert = joyposVert * -1; // Make the number positive MotorSpeed1 = map(joyposVert, 0, 460, 0, 255); } else if (joyposVert > 564) { // This is Forward // Set Motor A forward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); //Determine Motor Speeds MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255); } else if (joyposHorz < 460) { // This is Backward // Set Motor B backward digitalWrite(in3, LOW); digitalWrite(in4, HIGH); //Determine Motor Speeds // As we are going backwards we need to reverse readings joyposHorz = joyposHorz - 460; // This produces a negative number joyposHorz = joyposHorz * -1; // Make the number positive MotorSpeed2 = map(joyposHorz, 0, 460, 0, 255); } else if (joyposHorz > 564) { // This is Forward // Set Motor B forward digitalWrite(in3, HIGH); digitalWrite(in4, LOW); //Determine Motor Speeds MotorSpeed2 = map(joyposHorz, 564, 1023, 0, 255); } //NEW LINES all the way to NEW LINES end //Rotate the dragline. In the sketch I send earlier it said "if (joyposRot < 460)" note I changed it to ELSE IF now. Haven't tried it yet. else if (joyposRot < 460) { // This is Backward // Set Motor BA backward digitalWrite(in5, LOW); digitalWrite(in6, HIGH); //Determine Motor Speeds // As we are going backwards we need to reverse readings joyposRot = joyposRot - 460; // This produces a negative number joyposRot = joyposRot * -1; // Make the number positive MotorSpeed3 = map(joyposRot, 0, 460, 0, 50); } else if (joyposRot > 564) { // This is Forward // Set Motor BA forward digitalWrite(in5, HIGH); digitalWrite(in6, LOW); //Determine Motor Speeds MotorSpeed3 = map(joyposRot, 564, 1023, 0, 50); } //NEW LINE end else { // This is Stopped MotorSpeed1 = 0; MotorSpeed2 = 0; //NEW LINE MotorSpeed3 = 0; } // Adjust to prevent "buzzing" at very low speed if (MotorSpeed1 < 8)MotorSpeed1 = 0; if (MotorSpeed2 < 8)MotorSpeed2 = 0; //NEW LINE if (MotorSpeed3 < 8)MotorSpeed3 = 0; // Set the motor speeds analogWrite(enA, MotorSpeed1); analogWrite(enB, MotorSpeed2); //NEW LINE analogWrite(enBA, MotorSpeed3); } [/code]


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@marcelinnorway

OK, we are going to have to work on posting code step by step.

In the Arduino IDE, select All, from the Edit menu Copy as HTML.

Now go back to your message in the forum and click the {;} icon, to bring up the source window.

Place the cursor between </p> and <p> or at the end of the source code and Paste (ctrl-V or cmd-V) depending on whether you are using Windows or Mac.

Before hitting the SAVE button, click on the "eye Preview" icon, to check it is formatted properly. If OK hit the SAVE button.

You might just want to delete your last post and do it again.


   
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1075
 

Actually, code posting issues aside, I can see the problem in the code you provided.

You've combined both the horizontal and vertical joystick reading into one huge "else if" statement, which is not how the original code worked.  You've left out the "else" statement".

Look at the original code.  You'll notice the following pattern:

  • First, we see if the vertical joystick is less than 460, if it is we run some code to move backwards.
  • Next, we see if the vertical joystick is greater than 564, if it is we run some code to move forward.
  • If it doesn't meet the above criteria then we use an "else" statement to stop the motor.

After that, we do the same thing for the horizontal joystick. 

But in your code that last "else" statement has been omitted. If one of the first "if" statements is true then none of your subsequent statements are ever executed, as one the "if" statements has been satisfied and the program exits the "if" loop.

Does that make sense? To correct your code you need to have three "if - else if - else" sections, one for each motor.

?

Bill

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@dronebot-workshop

Is the solution really that straight forward?

The way I read the code is that pushing the joystick backwards and forwards controls the direction and speed of one motor and sideways motion controls motor number two. Surely you can only add more possibilities by pushing the button and moving the joystick simultaneously to give yourself basically two more motor channels to work with. Therefore having two control loops, in the main loop, when either the button pressed or not pressed during the joystick movement!


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @marcelinnorway

Thanks for the reply. Tried to follow your instructions on sending code, but it was not quite like you said. Was a lot of code allright, but not neatly arranged.

I should have told you that you need to also click on the  "Preview" at the bottom of the post editor to see the pretty formatting.   Had  you actually posted the code it probably would have posted just fine.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1075
 
Posted by: @pugwash

Is the solution really that straight forward?

Actually it is. Without the "else" statement it will only evaluate ONE of the "if-else" conditions, then exit. You need to evaluate all three, so they need to be three separate evaluations.

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
Page 1 / 2