Notifications
Clear all

[Solved] L298N+arduino+flysky remote


neil52
(@neil52)
Active Member
Joined: 2 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

This topic was modified 4 months ago by DroneBot Workshop

Quote
Topic Tags
DroneBot Workshop
(@dronebot-workshop)
Workshop Guru Admin
Joined: 2 years ago
Posts: 1004
 

@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 liked
ReplyQuote
neil52
(@neil52)
Active Member
Joined: 2 years ago
Posts: 6
Topic starter  

@dronebot-workshop Thanks!!


ReplyQuote
neil52
(@neil52)
Active Member
Joined: 2 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)
Prominent Member
Joined: 2 years ago
Posts: 883
 

@neil52

If you are using the Arduino IDE then right mouse button down on your source code in the Arduino IDE and Select All, right mouse button down, Copy as HTML.

In your post make sure you have hit the Enter key a few times where you want to insert your source code. Select {;} from the top bar of your post then between a <p> and </p> right mouse button down and paste.

You need to tell us what those errors are. I didn't have time to work out your curly brackets placements which looked strange so the source code below, although it compiles, is probably wrong.

 

 

//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);
  
}


 

 


ReplyQuote
neil52
(@neil52)
Active Member
Joined: 2 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 4 months ago by neil52

ReplyQuote
DroneBot Workshop
(@dronebot-workshop)
Workshop Guru Admin
Joined: 2 years ago
Posts: 1004
 

@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

 

 

 

This post was modified 4 months ago by DroneBot Workshop

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


ReplyQuote
neil52
(@neil52)
Active Member
Joined: 2 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)
Trusted Member
Joined: 10 months ago
Posts: 74
 

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