Notifications
Clear all

[Solved] L298N+arduino+flysky remote

9 Posts
4 Users
2 Likes
1,803 Views
(@neil52)
Member
Joined: 5 years ago
Posts: 6
Topic starter  

Trying to control 2 12v dc motors using arduino and a flysky remote have 1 motor functioning can't get the code to work on 2nd. following is the code I have commented out the code that won't work[error messages while compiling;BTW am really green at this coding.

 


//motor 1

int in1 = 12;
int in2 = 13;
// motor 2
int in3 = 7;
int in4 = 8;
//the following are all ~PWM capable ports
int enable1 = 11;
int rc_channel4 = 3;
//int switchD = 5;
int enable3 = 9;
int rc_channel1 = 6;
void setup() {
pinMode(rc_channel4, INPUT);
// pinMode(switchD, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enable1, OUTPUT);
pinMode [rc_channel1, INPUT];
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enable3, OUTPUT);
Serial.begin(9600);

}

void loop() {

int pwm = 0;
// int swd = pulseIn(switchD, HIGH, 25000);
int rc4 = pulseIn(rc_channel4, HIGH, 25000);

int rc1 = pulseIn(rc_channel1, HIGH, 25000);

//Serial.print("switch: ");
// Serial.print(swd);
Serial.print(" raw channel4: ");
Serial.print(rc4);

if (rc4 == 0) {
Serial.println(" no signal");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enable1, 0);
}
else if (rc4 > 1530) { //right stick
pwm = map(rc4, 1530, 2000, 0, 255); //map our speed to 0-255 range
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enable1, pwm);
Serial.print(" right stick speed: ");
Serial.println(pwm);
}
else if (rc4 < 1460) {
pwm = map(rc4, 1460, 1000, 0, 255); //map our speed to 0-255 range
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enable1, pwm);
Serial.print(" left stick speed: ");
Serial.println(pwm);
} else {
Serial.println(" stick centered");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enable1, 0);
// int swd = pulseIn(switchD, HIGH, 25000);
}

delay(10);
}/*

Serial.print(" raw channel1: ");
Serial.print(rc1);
if (rc1 == 0){

Serial.println(" no signal");
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enable3, 0);
}
{
else if (rc1 > 1530) { //right stick
pwm = map(rc1, 1530, 2000, 0, 255); //map our speed to 0-255 range
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enable3, pwm);
Serial.print(" right stick speed: ");
Serial.println(pwm);
}
{
else if (rc1 < 1460) {
pwm = map(rc1, 1460, 1000, 0, 255); //map our speed to 0-255 range
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enable3, pwm);
Serial.print(" left stick speed: ");
Serial.println(pwm);
} else {
Serial.println(" stick centered");
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

analogWrite(enable3, 0);
}
{

{
delay(10);
}*/


Thanks for any help,Neil


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

@neil52  Just FYI this forum doesn't use BB Code, so your post didn't format correctly.  I modified it for you.

You might want to look at the instructions for Adding code to your post.

😎

Bill

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


   
neil52 reacted
ReplyQuote
(@neil52)
Member
Joined: 5 years ago
Posts: 6
Topic starter  

@dronebot-workshop Thanks!!


   
ReplyQuote
(@neil52)
Member
Joined: 5 years ago
Posts: 6
Topic starter  

@dronebot-workshop I guess I don't understand  BB code should I just have copied text and pasted?

 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

   
ReplyQuote
(@neil52)
Member
Joined: 5 years ago
Posts: 6
Topic starter  

@robotbuilder Thanks I couldn't get it to compile but what you posted did so I will try it out in tomorrow.

What did you change maybe I can learn something. Thanks again!Neil

Just compared the 2 side by side and removed brackets to match yours and it works.I will try it on the motor s tomorrow!

This post was modified 3 years ago by neil52

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

@neil52 - Again, there is an article and video about doing this right here on the site.  You just need to highlight your code and use the "Code" button on the toolbar.  The method @robotbuilder uses is even better, although a bit more complex, as it will color-code the entries for easier reading.

BB Code is used on many forums, but not this one. You used it when posting your code originally, the [CODE] and [/CODE] symbols are BB Code.  I removed them and used the code (<>) button to format your code in your original post.

You can always see how it looks before you post by clicking the Preview link.

Now back to your problem, I have two questions:

  1.  Did you try reversing the inputs to the L298N (i.e. swapping the enable and input pins) to see if the problem is with the Arduino or the L298N itself?
  2. You say you commented out the stuff that didn't compile, most of what I see commented out seems to do with a switch input. What compile error did you receive? They usually are pretty informative.

 

And I have to admit that I'm curious why you named the EN inputs "enable1" and "enable3" - what happened to "enable2" LOL?  Not an issue at all as far as the operation goes, of course, I'm just curious.

😎

Bill

 

 

 

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


   
ReplyQuote
(@neil52)
Member
Joined: 5 years ago
Posts: 6
Topic starter  

@dronebot-workshop  as for the enable 3 I am not sure must have been a brain fart. As for the code I cleaned up the errant brackets by comparing to @robotbuilder posted code and was able to compile,  tried it out this AM and all functions like it should .I commented everything after 1st delay to the end with /*   */

Neil


   
ReplyQuote
jBo
 jBo
(@jbo)
Member
Joined: 3 years ago
Posts: 100
 

In the setup() routine, for the line:

pinMode [rc_channel1, INPUT];

it has square brackets. I would change these to just regular parentheses ( )
Cheers, John

In theory, theory and practice are the same.
In practice, they're different.


   
ReplyQuote