PART 3 || PWM CONTROL WITH JOYSTICK || DC MOTORS SPEED CONTROL WITH POTE...


PWM CONTROL WITH JOYSTICK
( DC MOTORS SPEED CONTROL WITH POTENTIOMETER )


SOURCE CODE:

int EnableA = 3;
int EnableB = 5;
int LMT1 = 4;//LEFT MOTOR TERMINAL
int LMT2 = 6;
int RMT1 = 7;//RIGHT MOTOR TERMINAL
int RMT2 = 8;

int potValue = 0;
int motorValue = 0;

void setup() {
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);
pinMode(EnableA, OUTPUT);
pinMode(EnableB, OUTPUT);

  pinMode(A0, INPUT);//POTENTIOMETER
  pinMode(A1, INPUT);//JOYSTICK1
  pinMode(A2, INPUT);//JOYSTICK2
}

void loop(){
int potValue = analogRead(A0);  
int motorValue = map(potValue, 0, 1023, 0, 255);

int JOYSTICK1 = analogRead(A1);//READ JOYSTICK1
int JOYSTICK2 = analogRead(A2);//READ JOYSTICK2

  
  if(JOYSTICK1 > 600){//MOTORS RUN FORWARD
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
analogWrite(EnableA, motorValue);
analogWrite(EnableB, motorValue);
}

  else if(JOYSTICK1 < 400){//MOTORS RUN BACKWARD
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
analogWrite(EnableA, motorValue);
analogWrite(EnableB, motorValue);
}


  else if(JOYSTICK2 > 600){//MOTORS RUN LEFT
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
analogWrite(EnableA, motorValue);
analogWrite(EnableB, motorValue);
}

  else if(JOYSTICK2 < 400){//MOTORS RUN RIGHT
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
analogWrite(EnableA, motorValue);
analogWrite(EnableB, motorValue);
}

else{
  //ALL MOTORS STOP
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);
analogWrite(EnableA, motorValue);
analogWrite(EnableB, motorValue);
}
}

Comments