This sketch can't run the motor in 'forward', need help please

The sketch run the motor wired to L293d shield, doesn't run for 'forward' where the motor wired to pin18-21, why?
Thanks
Adam


////////////////////////////////// MDF FROM : [gps_robot_car_M1_waypoint] at: LINE 63 and 174 of 'CONFIG.h'
int LeftMotorForward = 18; // IN1 to the driver  WAS 10 reverse the moving direction:
int LeftMotorBackward = 19; // IN2 to the driver  WAS 11
int RightMotorForward = 20; // WAS 5 IN3 to the driver WAS 12
int RightMotorBackward = 21 ; // WAS 6 IN4 to the driver WAS 13
/*
const int  ENABLE_A = 8;   /////WAS 6 mdfd 3 may conflict to NewPing
const int  ENABLE_B = 9;   /////WAS 8

#define BUZZER_PIN 15          // WAS 7Output pin for the buzzer
*/

int speedAH=200;
int speedBH=200;

int speedAL=100;
int speedBL=100;

int LED =15;

#include <AFMotor.h>//made by hirusha nirmal
AF_DCMotor R2(1);
AF_DCMotor R1(2);
AF_DCMotor L2(3);
AF_DCMotor L1 (4);

char bt='s';

void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);

R1.setSpeed(255);
R2.setSpeed(255);
L1.setSpeed(255);
L2.setSpeed(255);

// Initialize control pins for the motor driver
  pinMode (LeftMotorForward, OUTPUT);
  pinMode (LeftMotorBackward, OUTPUT);
  pinMode (RightMotorForward, OUTPUT);
  pinMode (RightMotorBackward, OUTPUT);

  pinMode(LED, OUTPUT);
  
Stop();

}

void loop() {
 bt=Serial3.read();

 if (bt=='F')
 {
  forward();
 }

 if (bt=='B')
 {
  backward();
 }

 if (bt=='R')
 {
  right();
 }

 if  (bt=='L')
 {
  left();
 }

 if (bt=='S')
 {
  Stop();
 }
 }
void forward()
{
  /*
  R1.run(FORWARD);
  R2.run(FORWARD);
  L1.run(FORWARD);
  L2.run(FORWARD);
  */

   ////////////////////////////////// MDF FROM : [gps_robot_car_M1_waypoint] at: LINE 368 'void Forward(unsigned char vel)'

  digitalWrite (RightMotorForward, HIGH);
  digitalWrite (RightMotorBackward, LOW);
  digitalWrite (LeftMotorForward, HIGH);
  digitalWrite (LeftMotorBackward, LOW);
  delay(500);

  ////////////////////////////////// MDF FROM : [gps_robot_car_M1_waypoint] at:
  
}

void backward()
{
  R1.run(BACKWARD);
  R2.run(BACKWARD);
  L1.run(BACKWARD);
  L2.run(BACKWARD); 
}

void right()
{
  R1.run(BACKWARD);
  R2.run(BACKWARD);
  L1.run(FORWARD);
  L2.run(FORWARD); 
}

 void left()
 {
  R1.run(FORWARD);
  R2.run(FORWARD);
  L1.run(BACKWARD);
  L2.run(BACKWARD); 
}

void Stop()
{
  R1.run(RELEASE);
  R2.run(RELEASE);
  L1.run(RELEASE);
  L2.run(RELEASE); 
}//made by hirusha nirmall

Swap the motor wires.

1 Like

why do you drive the pins directly in forward() but presumably use a library function to run the unit backwards?

1 Like

Thanks.
The reason is, I need drive 6 motors, 4 by L293d shield, the others 2 very small motors need hook to pins. I'll need two sets moving F/B/L/R for all directions, this is for testing only.

why are forward() and backward() done differently? why doesn't backward() use digitalWrite()?

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