L298N with RC Transmitter Coding

Hello everyone. I'm making a 3D printed RC excavator. I'd like to control 2 dc motors seperately (speed and direction) via RC transmitter on 2 different channels and I have one L298N module.

I've found this code but its only for 1 dc motor. It works fine but could you update the code for 2 dc motors please. (I don't know anything about coding.)

Thanks.


int in1 = 2;
int in2 = 3;
int enable1 = 5;  // pin with ~ symbol

int channel_2 = 6;  // pin with ~ symbol

void setup() 
{
  pinMode(channel_2, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enable1, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  
  int pwm = 0;
  int value = pulseIn(channel_2, HIGH, 25000);
  
  if(value==0)
  {
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      analogWrite(enable1, 0);
  }
  
  else if(value > 1530)
  {
      pwm = map(value, 1530, 2000, 0, 255); 
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(enable1, pwm);
  }
  
  else if(value < 1460)
  {
      pwm = map(value, 1460, 1000, 0, 255); 
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      analogWrite(enable1, pwm);
  }
  
  else
  {
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      analogWrite(enable1, 0);
  }
  
  delay(10);
}

this is not a programming question but a job question

  • define pins for a 2nd motor and 2nd channel
  • create sub-function which is passed the channel and pins
  • doesn't the else case handle the value == 0 case?
  • what do you need the 10 msec delay for?

If you are not interested in learning, and just want to hire someone to help, use the "flag" button to ask a moderator to move your post to the Jobs and Paid Consultancies forum section.

Ok guys thanks for your answers.

I've tried some codes and redesigned them. Finally I got this code, working fine for 2 dc motors seperately. But now, the problem is that when I try to control 2 motors at the same time, I need to run motorA firstly and then motorB. So, if I run motorB firstly, I can't run motorA at the same time.

What is the problem? I'd like to run 2 dc motors via 2 rc channels independently at the same time.

Thanks.

//motor pinleri
#define MotorL1 9
#define MotorL2 10 
#define MotorLE 6

#define MotorR1 7
#define MotorR2 8
#define MotorRE 5
//alıcıdan arduinoya aldığımız kanallar
const int ch1=A1;  
const int ch2=A2; 
const int ch6=A4; 

//gerekli değişkenleri tanımladık
int leddeger;  
int ilerideger, motorileri, motorgeri;  
int donusdeger, motorsag, motorsol;  


void setup()
{
  
  Serial.begin(9600);
  //kanalları tanımladık
  pinMode(ch1,INPUT);
  pinMode(ch2,INPUT);
  pinMode(ch6,INPUT);
  pinMode(2,OUTPUT);

}


void loop()
{
  //değerleri pulsein komutu ile okuyup değişkenlere atadık
  leddeger = pulseIn (ch6,HIGH); 
  donusdeger = pulseIn (ch1,HIGH); 
  ilerideger = pulseIn (ch2,HIGH); 
  motorileri=map(ilerideger,1920,1080,0,255);
  motorgeri=map(ilerideger,1080,1920,0,255);
  motorsag=map(donusdeger,1080,1920,0,255);
  motorsol=map(donusdeger,1920,1080,0,255);
  if (leddeger > 1300) {digitalWrite(2,HIGH);}
  else digitalWrite(2,LOW);

  if (ilerideger > 1550){ geri(motorgeri);}
  else if (ilerideger < 1450){ ileri(motorileri);}
  else if (donusdeger < 1450) {yerindesol(motorsol);}
  else if (donusdeger > 1550) {yerindesag(motorsag);}  
  else dur();

}

void ileri(int ilerigeri){  // Robotun ileri yönde hareketi için fonksiyon tanımlıyoruz.


  digitalWrite(MotorR1, HIGH); // Sağ motorun ileri hareketi aktif
  digitalWrite(MotorR2, LOW); // Sağ motorun geri hareketi pasif
  analogWrite(MotorRE, ilerigeri); // Sağ motorun hızı 
  
}
void geri(int ilerigeri){ // Robotun geri hareketi için fonksiyon tanımlıyoruz.

  digitalWrite(MotorR1, LOW); // Sağ motorun ileri hareketi pasif
  digitalWrite(MotorR2, HIGH); // Sağ motorun geri hareketi aktif
  analogWrite(MotorRE, ilerigeri); // Sağ motorun hızı 

}
void yerindesag(int sag){ // Robotun yerinde sağa dönme hareketi için fonksiyon tanımlıyoruz.

  digitalWrite(MotorL1, LOW); // Sol motorun ileri hareketi aktif
  digitalWrite(MotorL2, HIGH); // Sol motorun geri hareketi pasif
  analogWrite(MotorLE, sag); // Sol motorun hızı 
  
}
void yerindesol(int sol){ // Robotun yerinde sola dönme hareketi için fonksiyon tanımlıyoruz.

  digitalWrite(MotorL1, HIGH); // Sol motorun ileri hareketi pasif
  digitalWrite(MotorL2, LOW); // Sol motorun geri hareketi aktif
  analogWrite(MotorLE, sol); // Sol motorun hızı 
  
}

void dur(){ // Robotun durma hareketi için fonksiyon tanımlıyoruz.

  digitalWrite(MotorR1, HIGH);
  digitalWrite(MotorR2, LOW);
  digitalWrite(MotorRE, LOW);

  digitalWrite(MotorL1, HIGH);
  digitalWrite(MotorL2, LOW);
  digitalWrite(MotorLE, LOW);
  
}

create a sub-function that has arguments for the different pins for each motor that determines the motor speed/direction for the channel.

call that sub-function for each channel/motor

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.