Motor Control Problem

Hello, good afternoon everyone, thanks for taking some time to read my query.

I am working with an Arduino Nano and an L293. With which I control two cc motor:

Motor 1

Digital Pin 4
PWM Pin 5

Motor 2
Digital Pin 7
PWM Pin 6

Using the arduino ide there are no problems, everything works fine, they turn and stop as expected.

The problem is in a library that I created, in which I can’t find the error, that’s why I go to you:


#ifndef DCMotor_h
#define DCMotor_h

#include "Arduino.h"

class DCMotor
{
 public:
   DCMotor();
   void motorGrande(int puerto, int potencia);
   void motorAccionar(int _puerto, int _potencia, int _ground);
   void motorStopG();
   void motorStopH();
 private:
   int _potencia;
   int _puerto;
   int _ground;


};

#endif

#include "Arduino.h"
#include "DCMotor.h"

DCMotor::DCMotor() {}

void DCMotor::motorGrande(int puerto, int potencia) {

 _ground = LOW ;
 _puerto = puerto;
 if(puerto == 0){
                 if(potencia == 0){
                 motorStopG();
                                    }
                  }
                  else{
                  if(potencia < 0) {
                    _potencia = - potencia + 150;
                    _ground = LOW;
                                    }
                  if(potencia > 0){
                  _potencia = 255 - potencia - 150;
                  _ground = HIGH;
                  }
                }
 if(puerto == 1){
 if(potencia == 0){
   motorStopH();
   }
   }
   else{
   if(potencia < 0) {
     _potencia = - potencia + 150;
     _ground = LOW;
                     }
   if(potencia > 0){
   _potencia = 255 - potencia - 150;
   _ground = HIGH;
   }
}
motorAccionar(_puerto, _potencia, _ground);
}


void DCMotor::motorAccionar(int puerto, int potencia, int ground) {

if(puerto == 0){
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 digitalWrite(4, ground);
 analogWrite(5, potencia);
 delay(100);
}

if(puerto == 1){
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 analogWrite(6, potencia);
 digitalWrite(7, ground);
 delay(100);}
}


void DCMotor::motorStopG() {
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 digitalWrite(4, LOW);
 analogWrite(5, 0);
 delay(100);
}

void DCMotor::motorStopH() {
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 analogWrite(6, 0);
 digitalWrite(7, LOW);
 delay(100);
}

Arduino Code

#include <DCMotor.h>
DCMotor motorGrande = DCMotor();

void setup() {

motorGrande.motorGrande(0,100);
delay(3000);
motorGrande.motorGrande(0,0);
delay(3000);

motorGrande.motorGrande(0,-100);
delay(3000);
motorGrande.motorGrande(0,0);
delay(3000);

}

void loop() {

 

}
motorGrande.motorGrande(0,100);
delay(3000);
motorGrande.motorGrande(0,0);
delay(3000);

This part works perfectly, the motor rotates in a 100% power direction and then stops.

But in this part the engine does not stop

motorGrande.motorGrande(0,-100);
delay(3000);
motorGrande.motorGrande(0,0);
delay(3000);

In fact, every time I put a negative parameter in motorGrande.motorGrande(Port, -X), the next motorGrande.motorGrande(Port, 0) stops working.

I have no clue about the mistake, I suspect of MotorStopH y MotorStopG

Thanks a lot

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Thanks a lot for the advice!

maurif2:
In fact, every time I put a negative parameter in motorGrande.motorGrande(Port, -X), the next motorGrande.motorGrande(Port, 0) stops working.

I have no clue about the mistake, I suspect of MotorStopH y MotorStopG

Thanks a lot

Are you trying to use 0 to 100 to indicate % power? Does the sign of the power indicate direction?

Yes, I´m triying to do that, in fact for backwars and forward works fine, the problem is when I wanna stop after a - power.

Thaks