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