# Motor control methods help

Hi AC

Need a little help with writing methods. Basically I have a motor control board which connects motors and Inputs connected to an arduino. There are four inputs, IN1, IN2, IN3 and IN4. Different combo would turn forward or backwards. I am trying to create method for moving forward and backward. But it doesn't work and I'm new programming so hope someone can point out my mistake. Thanks..

int IN1 = 6;
int IN2 = 5;
int IN3 = 9;
int IN4 = 10;

void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}

void moveForward(){
//M1 CW=01 CCW=10
analogWrite(IN1, 150);
analogWrite(IN2, 0);

//M2 CW=01 CCW=10
analogWrite(IN3, 160);
analogWrite(IN4, 0);
}

void loop()
{
void moveForward();
}

Basically I have a motor control board which connects motors and Inputs connected to an arduino.

Which motor controller board?

``````int IN1 =  6;
int IN2 =  5;
int IN3 =  9;
int IN4 =  10;
``````

Don't some of these control direction and some control speed? Wouldn't it be useful if the names reflected that fact?

I am trying to create method for moving forward and backward.

You have one method called forward(). Where is the backward() one?

``````//M1 CW=01 CCW=10
analogWrite(IN1, 150);
analogWrite(IN2, 0);

//M2 CW=01 CCW=10
analogWrite(IN3, 160);
analogWrite(IN4, 0);
``````

The values for CW and CCW shown do not look right. It's hard to know which of the names correspond to direction pins and which correspond to speed pins.

But it doesn't work and I'm new programming so hope someone can point out my mistake.

Sure. You didn't tell us what does happen when that code runs.

Those pins probably have more descriptive names like Forward or speed1 or something. Naming them In1, In2,… looks really odd when you then use them as outputs in your Arduino sketch.

What exactly do the 4 inputs to the card do? are you just feeding the card 1 of 16 options? or do specific pins do a specific function? By putting more specific names it makes it easier to see if there is some problem with the logic.

Thanks for the reply. It is a Bidirectional DC Motor Control with a l298n(can't find a model of board) but have a picture:

Basically IN1 and IN2 is the control for the first motor. "150" is just the pwm value or the speed for the motors. IN1 would drive the motor clockwise and IN2 will drive it counter clockwise. IN3 and IN4 is the control for the other motor with same working principal.

MOTOR 2
analogWrite(IN1, 150);
analogWrite(IN2, 0);

MOTOR 2
analogWrite(IN3, 160);
analogWrite(IN4, 0);

But when I upload the code on the first post, the wheels does no turn but if I put the code in void loop method, it works. This is the code:

void loop()
{
//M1 CW=01 CCW=10
analogWrite(IN1, 150);
analogWrite(IN2, 0);

//M2 CW=01 CCW=10
analogWrite(IN3, 160);
analogWrite(IN4, 0);
}

So my goal is to create methods like moveForward(), moveBack and more... but the code at the first post didn't work so I was think it is a fault with my code..

int IN1 = 6;
int IN2 = 5;
int IN3 = 9;
int IN4 = 10;

These are just the pins on the board not speed or anything like that.