L293D Arduino Motor Driver Shield Problem and new eyes on problem

GOOD DAY. I HOPE SOMEONE CAN HELP ME WITH THIS PROBLEM. I WONT TO CONNECT 2 MOTORS ON M3 AND M4 AND FOR SOME REASONS I CAN'T FIND THE PROBLEM WITH THE CODE . EVERYTHING IS WORKING 80% BUT I CANT SEEM TO FIGURE IT OUT. IM VERY NEW TO THIS AND I HOPE A FRESH PEAR OF EYES CAN HELP.

BLESSINGS

![DC MOTOR X2 PROBLEM|636x500]

DC Motor with Arduino UNO using L293D motor Driver shield


*/
#include <AFMotor.h>
  // Library to run DC Motor Using Motor Driver Shield  
#include <LiquidCrystal_I2C.h> 
  // Library to Run I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Format -> (Address,Columns,Rows )
// Create the motor object connected to M3
AF_DCMotor motor(3);
// Define button pins
const int forwardButtonPin = A1;
const int reverseButtonPin = A2;
const int stopButtonPin    = A3;
// Define potentiometer pin
const int potPin = A0;
// Variables to store motor state and direction
bool motorRunning  = false;
int motorDirection = BACKWARD;  // FORWARD or BACKWARD
// Read the potentiometer value
int potValue;
int motorSpeed;
// Variable to store button states
bool forwardButtonState;
bool stopButtonState;
bool reverseButtonState;
// Inline function to check if button is pressed packed with debouncing logic
inline bool chkButtonState(int pinNum, int checkState, int debounceDelay) {
  if (((digitalRead(pinNum) == checkState) ? true : false) == true) {
    delay(debounceDelay);
    return (((digitalRead(pinNum) == checkState) ? (true) : (false)) == true);
  } else {
    return false;
  }
}
void setup() {
  // initialize the lcd
  lcd.init();
  // Turn on the Backlight
  lcd.backlight();
  // Clear the display buffer
  lcd.clear();
  // Set cursor (Column, Row)
  lcd.setCursor(0, 0);
  lcd.print("DC Motor using");
  lcd.setCursor(0, 1);
  lcd.print("L293D Shield");
  // Set button pins as inputs
  pinMode(forwardButtonPin, INPUT_PULLUP);
  pinMode(stopButtonPin   , INPUT_PULLUP);
  pinMode(reverseButtonPin, INPUT_PULLUP);
  // Start with motor off
  motor.setSpeed(0);
  motor.run(RELEASE);
  delay(500);
  // Clear the display buffer
  lcd.clear();
  // Set cursor (Column, Row)
  lcd.setCursor(0, 0);
  lcd.print("Motor Direction:");
  lcd.setCursor(0, 1);
  lcd.print("Stopped   ");
}
void loop() {
  // Read the potentiometer value for changing speed as per Analog input
  potValue   = analogRead(potPin);
  motorSpeed = map(potValue, 0, 1023, 0, 255);
  // Read the button states
  forwardButtonState = chkButtonState(forwardButtonPin, LOW, 20);
  reverseButtonState = chkButtonState(reverseButtonPin, LOW, 20);
  stopButtonState    = chkButtonState(stopButtonPin,    LOW, 20);
  // check for Forward run
  if (forwardButtonState && (!motorRunning || motorDirection == BACKWARD)) {
    // Set cursor (Column, Row)
    lcd.setCursor(0, 1);
    lcd.print("Close");
    if (motorDirection == BACKWARD) {
      motor.setSpeed(0);
      motor.run(RELEASE);
      delay(1000);
    }
    motorRunning = true;
    motorDirection = FORWARD;
    motor.setSpeed(motorSpeed);
    motor.run(FORWARD);
  }
  
  // check for Reverse run
  else if (reverseButtonState && (!motorRunning || motorDirection == FORWARD)) {
    // Set cursor (Column, Row)
    lcd.setCursor(0, 1);
    lcd.print("open");
    if (motorDirection == FORWARD) {
      motor.setSpeed(0);
      motor.run(RELEASE);
      delay(1000);
    }
    motorRunning = true;
    motorDirection = BACKWARD;
    motor.setSpeed(motorSpeed);
    motor.run(BACKWARD);
  }
  
  // Stop motor
  else if (stopButtonState && motorRunning) {
    // Set cursor (Column, Row)
    lcd.setCursor(0, 1);
    lcd.print("Stopped         ");
    motorRunning = false;
    motor.setSpeed(0);
    motor.run(RELEASE);
  }
   
  // Adjust motor speed if running and display speed on LCD
  if (motorRunning) {
    motor.setSpeed(motorSpeed);
    // Set cursor (Column, Row)
    lcd.setCursor(9, 1);
    lcd.print("SPD:");
    lcd.print(((motorSpeed*100)/255));
    lcd.print("%  ");
  }
}
  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch. Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.
  1. Don't shout.
  2. Present your code as code. Edit your post, select all the code and click on the <code> button.

done it.
Blessings

If there's a problem with the code, then next sentence everything is working 100%, I'm a bit lost at that. Could you clarify?

Well if i try in included in the code af_dcmotor(1); then only one motor is working as it must going forward and backwards when pushing the bottons . but if i at a nother motor like #include <AFMotor.h>
AF_DCMotor motor(1,2);
then still only one motor is working . its not working on m2 . only on m1 point .
Im still new at getting it right to a pount but i have hit a brick wall here .
the i.d is the there's two motors on both sides of the platform and when the forward button is pushed the the hole draw opens up and when you press the close boton then the draw closes . the stop button is as an emergency stop as if i can say it like that.

Great, always nice to see a problem solved.

Sorry for the mixup.
I will not say thatits at 100%.
Im at 80% and i have hit a wall .
Im still new at this and any help on this would realy work.

  • When something is not working as expected, use Serial.print( ) on variables to check they are what you think they are.

  • If things work 100% then there’s no problem.

Edit:

  • This sends shivers down my back.

if (((digitalRead(pinNum) == checkState) ? true : false) == true) {

  • Why use inline ?

That called for a LOL.

I don't know about others, but I still don't know exactly what is wrong. Maybe try a truth table or some simple way of telling us what works, and what does not work.
Another thing to try is to use the sample code to do the movements and make some modifications for the other stuff. Also using a button library gets rid of half your code.

You made it a distance to the finish line, congrats.

As @LarryD said, Serial.print is your friend when debugging code. You catch a lot with that.

sarcasm is not needed .
everyone started somewhere and all im asking is for some help.
nothing more and nothing less.

  • Trying to solicit answers or an explanation . . .

  • Have you confirmed your variable values are as you think they are ?

Edit:

  • You say:
AF_DCMotor motor(1,2);

Did you try:


AF_DCMotor motorOne(1);
AF_DCMotor motorTwo(2);

If i try to put it in . this is what happens

exit status 1

Compilation error: 'motor' was not declared in this scope
On motor.setSpeed(0);
and if i change it back to AF_DCMotor(1,2);
Then it works .no error

  • In your OP, you have:
AF_DCMotor motor(3);
  • You are creating a motor object on channel 3.
    Hence you can then refer to motor in your sketch as you did.



AF_DCMotor motorOne(1);
AF_DCMotor motorTwo(2);
  • Above we are creating 2 objects, motorOne on channel 1 and motorTwo
    on channel 2
    We can then refer to motorOne and motorTwo in the sketch to control the motors.

  • You do know that the object names need to be adjusted throughout to rest of the sketch ?

As examples:
motorOne.setSpeed(255)
motorOne.run(FORWARD)
OR
motorTwo.setSpeed(127);
motorTwo.run(FORWARD)

  • BTW, when it comes to simple switches, you can debounce them by checking for a change in state every 20 to 50ms.

  • Also we avoid using delay( ) in our sketches as it stops program execution.

I moved your topic to an appropriate forum category @Davidcencon.

In the future, when creating a topic please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

  • I don't have that PCB but you can check to see if motors on 3 and 4 will operate.
//Adafruit Motor shield library
//copyright Adafruit Industries LLC, 2009


#include <AFMotor.h>

AF_DCMotor motorThree(3);
AF_DCMotor motorFour(4);

//===================================================================================
void setup()
{
  Serial.begin(9600);          
  Serial.println("Motor test!");

  //turn on motor
  motorThree.setSpeed(127);
  motorThree.run(RELEASE);

  motorFour.setSpeed(200);
  motorFour.run(RELEASE);

} //END of   setup()


//===================================================================================
void loop()
{
  uint8_t i;

  Serial.print("tick");

  motorThree.run(FORWARD);
  motorFour.run(FORWARD);
  
  for (i = 0; i < 255; i++)
  {
    motorThree.setSpeed(i);
    motorFour.setSpeed(i);
    
    delay(10);
  }

  for (i = 255; i != 0; i--)
  {
    motorThree.setSpeed(i);
    motorFour.setSpeed(i);
  
    delay(10);
  }

  Serial.print("tock");

  motorThree.run(BACKWARD);
  motorFour.run(BACKWARD);
  
  for (i = 0; i < 255; i++)
  {
    motorThree.setSpeed(i);
    motorFour.setSpeed(i);
    
    delay(10);
  }

  for (i = 255; i != 0; i--)
  {
    motorThree.setSpeed(i);
    motorFour.setSpeed(i);

    delay(10);
  }

  Serial.print("tech");

  motorThree.run(RELEASE);
  motorFour.run(RELEASE);

  delay(1000);

} //END of   loop()