DC Motors speed and direction issue

What's app guys !
I'm a new user in the arduino's world and I've just registered to this forum , any welcomes ? :stuck_out_tongue:
Well,
I'm trying to make my first artificial intelligent project realising a robot which can move in four directions using two DC motors I used a L293D as well,
while programming this stuff I met a problem I donno why something went wrong with it,
so the picture contains a simulation of my system and this is the code of my program
I'm using a bluetooth commande so that's why I set a virtual terminal !

int x,vi1=50,vi2=50;
void accelerer(int vi,int ep){
vi+=20;
if(vi>255)vi=200;
analogWrite(ep,vi);
delay(100);
}
void ralentir(int vi,int ep){
vi-=20;
if(vi<0)vi=20;
analogWrite(ep,vi);
delay(100);
}
int motorPin;
void setup() {
  pinMode(13,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(5,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()>0){
  x=Serial.read();
  }
  if(x=='A'){
    digitalWrite(6,HIGH);
    analogWrite(13,50);
    digitalWrite(9,LOW);
    if(x=='F')accelerer(vi1,13);
    else if(x=='G')ralentir(vi1,13);
    }
       
  if(x=='B'){
    analogWrite(9,HIGH);
  digitalWrite(13,LOW);
  digitalWrite(6,50);
    if(x=='F')accelerer(vi1,6);
    else if(x=='G')ralentir(vi1,6);
       }
    if(x=='C'){
  analogWrite(5,HIGH);  
  digitalWrite(2,LOW);
  digitalWrite(3,50);

    if(x=='H')accelerer(vi2,4);
    else if(x=='I')ralentir(vi2,4);
      }
    if(x=='D'){ 
    analogWrite(5,HIGH); 
    digitalWrite(2,50);
    digitalWrite(3,LOW); 
    if(x=='H')accelerer(vi2,4);
    else if(x=='I')ralentir(vi2,4);
      }
  if(x=='E'){
  digitalWrite(13,LOW);
  digitalWrite(9,LOW);
       }
  if(x=='J'){
  digitalWrite(3,LOW);
  digitalWrite(2,LOW);
       }
}

I tried the digitalWrite function with En1 and En2 and with expected HIGH voltage MotorPin and got no results :cold_sweat:

thank you in advance and sorry for the bad accent haha :blush:

if(x=='A'){
    digitalWrite(6,HIGH);
    analogWrite(13,50);
    digitalWrite(9,LOW);
    if(x=='F')
        accelerer(vi1,13);
    else if(x=='G')
        ralentir(vi1,13);
}

If x equals 'A', how can it possibly ever equal either 'F' or 'G'??

Regards,
Ray L.

you're totally right so the bluetooth signal should send two variables ?
but this part manage acceleration. :slight_smile:
and motors can't even move,so I can't see what happens when clicking A or H :confused:

Did you connect the GND line to all devices?

I'm trying it on Proteus.
The L293D and the battery are connected to the ground of course .

here is another version of my program I think I corrected the variable problem
this version move the first motor and variates its speed
(it supposed to do that) :disappointed_relieved:

int x,y,z;                               //1
int motorPin;                            //2
void setup() {
  pinMode(13,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(5,OUTPUT);
  Serial.begin(9600);                   //10
}
void loop() {
  if(Serial.available()>0){
  x=Serial.read();
  }                                    //15
  if(x=='A'){ //enable pin 6 and pin 5 turned on 
    digitalWrite(6,HIGH);
    y=1;
    digitalWrite(5,HIGH);
    z=1;
    }
  else if(x=='B'){ //enable pin 6 turned off 
  digitalWrite(5,LOW);
  y=0;  
  digitalWrite(6,LOW); 
  z=0;
      }
  while(y==1){
                if(x==1){
                    analogWrite(13,50);
                    digitalWrite(9,LOW);
                          }
                if(x==2){
                    analogWrite(13,70);
                    digitalWrite(9,LOW);
                          }
                if(x==3){
                    analogWrite(13,90);
                    digitalWrite(9,LOW);
                          }
                if(x==4){
                    analogWrite(13,110);
                    digitalWrite(9,LOW);
                          }
                if(x==5){
                    analogWrite(13,130);
                    digitalWrite(9,LOW);
                          }
                if(x==6){
                    analogWrite(13,150);
                    digitalWrite(9,LOW);
                          }
                if(x==7){
                    analogWrite(13,170);
                    digitalWrite(9,LOW);
                          }
                if(x==8){
                    analogWrite(13,190);
                    digitalWrite(9,LOW);
                          }
                if(x==9){
                    analogWrite(13,210);
                    digitalWrite(9,LOW);
                          }
                if(x=='a'){
                    analogWrite(9,50);
                    digitalWrite(13,LOW);
                          }
                if(x=='b'){
                    analogWrite(9,70);
                    digitalWrite(13,LOW);
                          }
                if(x=='c'){
                    analogWrite(9,90);
                    digitalWrite(13,LOW);
                          }
                if(x=='d'){
                    analogWrite(9,110);
                    digitalWrite(13,LOW);
                          }
                if(x=='e'){
                    analogWrite(9,130);
                    digitalWrite(13,LOW);
                          }
                if(x=='f'){
                    analogWrite(9,150);
                    digitalWrite(13,LOW);
                          }
                if(x=='g'){
                    analogWrite(9,170);
                    digitalWrite(13,LOW);
                          }
                if(x=='h'){
                    analogWrite(9,190);
                    digitalWrite(13,LOW);
                          }
                if(x=='i'){
                    analogWrite(9,190);
                    digitalWrite(13,LOW);
                          }
                              }
}

nothing happens again in the simulation you can see the circuit above everything about electronics part is acceptable I guess
thank you once again for your help :grin:

analogWrite(13,110);

On a Uno, pin 13 is not PWM.

okay thank you I'll see :slight_smile:

I corrected the mistake about PWM
I'm sure the problem is in the order of events in the code and I can't make it
I uploaded a screenshot of Proteus Window

int x,y,z;                               //1
int motorPin;                            //2
void setup() {
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(2,OUTPUT);
  Serial.begin(9600);                   //10
}
void loop() {
  if(Serial.available()>0){
  x=Serial.read();
  }                                    //15
                if(x==1){
                    analogWrite(6,100);y=1;
                        }
                if(x==2){
                    analogWrite(6,110);y=1;
                        }
                if(x==3){
                    analogWrite(6,120);y=1;
                          }
                if(x==4){
                    analogWrite(6,130);y=1;
                          }
                if(x==5){
                    analogWrite(6,140);y=1;
                          }
                if(x==6){
                    analogWrite(6,150);y=1;
                          }
                if(x==7){
                    analogWrite(6,160);y=1;
                          }
                if(x==8){
                    analogWrite(6,170);y=1;
                          }
                if(x==9){
                    analogWrite(6,180);y=1;
                          }
                if(x=='a'){
                    analogWrite(5,100);z=1;
                          }
                if(x=='b'){
                    analogWrite(5,110);;z=1;
                          }
                if(x=='c'){
                    analogWrite(5,120);;z=1;
                          }
                if(x=='d'){
                    analogWrite(5,130);;z=1;
                          }
                if(x=='e'){
                    analogWrite(5,140);;z=1;
                          }
                if(x=='f'){
                    analogWrite(5,150);;z=1;
                          }
                if(x=='g'){
                    analogWrite(5,160);;z=1;
                          }
                if(x=='h'){
                    analogWrite(5,170);;z=1;
                          }
                if(x=='i'){
                    analogWrite(5,180);;z=1;
                          }
  if(y==1){ //enable pin 8 and pin 5 turned on 
    digitalWrite(13,HIGH);
    digitalWrite(12,LOW);
    }
  if(z==1){ //enable pin 8 turned off 
  digitalWrite(13,LOW);
  digitalWrite(12,HIGH); 
      }        
}

What have I to do to fix this stuff :-\
Thank you guys !

Serial.read() returns a character (even though x is declared int) so in your if statements where you compare x to a constant make them character constants. if(x == 1) becomes if(x == '1').

Hi,
I know you have been asked but the reply did not answer the question completely.

Have you got all the gnds connected together, the Battery supplying the motor gnd and the gnd of the arduino, your diagram does not show this.

Tom..... :slight_smile:

Thank you #Groundfungus now the code reacts better I can see red light in motors poles
But En1 and En2 still in blue so motors cannot move if En1 and En2 are in blue (disabled)
#Tom: as you can see in the screenshot :slight_smile: all L293D GND are connected to the GND and the battery's GND as well :slight_smile: did you mean another ground ? virtual terminal doesn't need to be connected to a GND
thank you ;D
Imad

You seem not to understand the GND issue. All connected devices must have one common line, which allows current to return from all other pins. Without such a return line no current can flow. This applies to the Arduino and your virtual terminal as well!

The label "GND" on many boards is somewhat misleading. It does not mean that this pin is connected to the ground (earth), like also the ground symbol in your image indicates. Instead GND means that the pin must be connected to the common line for all connected devices.

virtual terminal doesn't need to be connected to a GND in proteus

#DrDeittrich : the problem is not related to GND even though I change the common line to GND pin of arduino UNO nothing happens you can see that on the screenshot :cold_sweat:
#siutoejai : yeah you're right ! my virtual terminal works without any problems

But I'm still having a doubt about En1 and En2 cause they accept 2 values zero and one (5V-0V).
so giving a value between 0 and 255 couldn't be read as an order (enable/disable) is that right ?
so what can I do to solve this problem ?
Thank you !