Complete novice without a clue needs help , please

Complete newbie here so please be nice

i am trying to control 2 x l298n motor drivers from my flysky rc receiver ,

i am wanting to have 4 separate channels controlling 4 separate motors , no mixing or anything , dont need to have any speed control , just need the motors to go forward and reverse at max stick travel and off in center

i have searched everywhere and cant find a code for this application so i attempted to take bits from a code and rewrite to get what i need ,

This is the rubbish code i came up with its probably completely wrong so please can anyone help me , i need it for a big project im working on

double ch1=1;
int a=5; int b=6;
 
double ch2=2;
int c=7; int d=8;

double ch3=3;
int e=9; int f=10;
 
double ch4=4;
int g=11; int h=12;


void setup()
{
  Serial.begin(9600);
  
  pinMode(1,INPUT);
  pinMode(5,OUTPUT); pinMode(6,OUTPUT);
  
  pinMode(2,INPUT);
  pinMode(7,OUTPUT); pinMode(8,OUTPUT);
  
  pinMode(3,INPUT);
  pinMode(9,OUTPUT); pinMode(10,OUTPUT);
  
  pinMode(4,INPUT);
  pinMode(11,OUTPUT); pinMode(12,OUTPUT);
  
}

void loop()
{
ch1 = pulseIn(1,HIGH);
ch2 = pulseIn(2,HIGH);
ch3 = pulseIn(3,HIGH);
ch4 = pulseIn(4,HIGH);

{
(ch1==0)

digitalWrite(a,LOW); digitalWrite(b,HIGH);

(ch1>1530)
  
digitalWrite(a,HIGH); digitalWrite(b,LOW);
}
{
(ch2==0)
  
digitalWrite(c,LOW); digitalWrite(d,HIGH);
 
(ch2>1530)

digitalWrite(c,HIGH); digitalWrite(d,LOW);
}
{
(ch3==0)
  
digitalWrite(e,LOW); digitalWrite(f,HIGH);
 
(ch3>1530)
  
digitalWrite(e,HIGH); digitalWrite(f,LOW);
}
{
(ch4==0)
  
digitalWrite(g,LOW); digitalWrite(h,HIGH);
 
(ch4>1530)
  
digitalWrite(g,HIGH); digitalWrite(h,LOW);
}

Got a schematic? hand drawn is fine.

This is an in-depth guide about the L298N motor driver including its specifications, pinout, interfacing with Arduino board and eventually learning how to control DC motors with it.

https://microcontrollerslab.com/l298n-dc-motor-driver-module-arduino-tutorial/#:~:text=The%20table%20shows%20some%20specifications%20of%20the%20L298N,%20%202A%20%203%20more%20rows%20

Refer to the topic:

Arduino Sketch: Controlling DC Motors using L298N Motor Driver

Please give variables a reasonable name. A b, c, etc. tells nothing.
It seems you are looking for a switch case. Look that up in arduino reference.
L298 is supposed to control 1 motor in 2 directions or
2 in 1 direction not 2 motors in 2 directionss.
So I think you need 4 L298.
You should find many example.

Also: get 1 motor working first. Add the others later (preferrably using arrays). It will save you a lot of typing...
A double represents a floating point variable. That seems to be inappropriate in your sketch.

What does the magic function pulseIn do?

i know its not a full proper schematic but shows what pins go to what digital pins

i have no idea sorry , as i say i edited someone else's code and that's how they had the inputs written :confused:

for reference , i started with this guys code and tried editing it to have the 4 independent controls i am after

Welcome to the forum. Well done for using code tags.

Best if you want to learn and understand your code, especially if part of a bigger project, is to break things down and build from scratch rather than copy and paste. Large, complex projects are just lots of small simple projects put together. You should always be working on the most simple chunk possible in a completely separate sketch with an appropriate name. So, for example you should have a sketch that only controls one motor and does nothing else. You combine working sketches at the end. On e you code a motor to “do something” you can put this into a function and call it at an appropriate time. For example if x is detected “do something”. Your “do something” code is reusable and nicely compact.

Rule of thumb, if you need to copy and paste code multiple times with minor variations then there will be a better way.

I think this is what you were aiming for. I moved the radio inputs to A0, A1, A2, and A3 because Pin 1 is used by Serial which will be important later for debug.

// The pulseIn() function returns 'unsigned long', not 'double'.

unsigned long ch1;
unsigned long ch2;
unsigned long ch3;
unsigned long ch4;

// You can use A0 through A5 for digital inputs
// Let's use those for the radio channels
const byte Channel1Pin = A0;
const byte Channel2Pin = A1;
const byte Channel3Pin = A2;
const byte Channel4Pin = A3;


// Each motor has an A and a B pin
const byte Motor1APin = 5;
const byte Motor1BPin = 6;

const byte Motor2APin = 7;
const byte Motor2BPin = 8;

const byte Motor3APin = 9;
const byte Motor3BPin = 10;

const byte Motor4APin = 11;
const byte Motor4BPin = 12;


void setup()
{
  Serial.begin(115200);
  delay(200);

  pinMode(Channel1Pin, INPUT);
  pinMode(Channel2Pin, INPUT);
  pinMode(Channel3Pin, INPUT);
  pinMode(Channel4Pin, INPUT);

  pinMode(Motor1APin, OUTPUT);
  pinMode(Motor1BPin, OUTPUT);

  pinMode(Motor2APin, OUTPUT);
  pinMode(Motor2BPin, OUTPUT);
  \
  pinMode(Motor3APin, OUTPUT);
  pinMode(Motor3BPin, OUTPUT);

  pinMode(Motor4APin, OUTPUT);
  pinMode(Motor4BPin, OUTPUT);
}

void SetSpeed(unsigned long speed, const byte MotorPinA, const byte MotorPinB)
{
  // Center Stick should be about 1500
  if (speed < 1470)
  {
    // Full speed reverse
    digitalWrite(MotorPinA, LOW);
    digitalWrite(MotorPinB, HIGH);
  }
  else  if (speed > 1530)
  {
    // Full speed forward
    digitalWrite(MotorPinA, HIGH);
    digitalWrite(MotorPinB, LOW);
  }
  else
  {
    // Stop
    digitalWrite(MotorPinA, LOW);
    digitalWrite(MotorPinB, LOW);
  }
}

void loop()
{
  // Read pulse lengths. Set timeout to 30 milliseconds instead of 1 second.
  ch1 = pulseIn(Channel1Pin, HIGH, 30000ul);
  ch2 = pulseIn(Channel2Pin, HIGH, 30000ul);
  ch3 = pulseIn(Channel3Pin, HIGH, 30000ul);
  ch4 = pulseIn(Channel4Pin, HIGH, 30000ul);

  SetSpeed(ch1, Motor1APin, Motor1BPin);
  SetSpeed(ch2, Motor2APin, Motor2BPin);
  SetSpeed(ch3, Motor3APin, Motor3BPin);
  SetSpeed(ch4, Motor4APin, Motor4BPin);
}
1 Like

you sir are an absolute legend , that works exactly how i wanted , i been trying to get this sorted for weeks and you have solved it instatntly , seriously can not thank you enough :slight_smile:

now if i understand the pins this wont work with a nano will it cos you cant use the analog pins as digital ? no worries tho ill be able to squeeze in my uno into my project :slight_smile:

You will be getting high praise on my instagram and facebook posts for your help

HUGE thank you again :slight_smile:

It's only A6 and A7 that can't be used for digital I/O.

1 Like

Ah ok i thought it was all analog , well code works awesome on the uno , will give it a go on the nano later :slight_smile:

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