PWM based Vehicle Moment Control - Arduino by hdrohithd |

PWM based Vehicle Moment Control - Arduino

Author: hdrohithd


To control the Electric Vehicle Direction and speed, this is a simple code on Arduino.
If in case vehicle fails to turn right / left, use 
move_left_forward_n and  move_right_forward_n functions.

const int SPD = 300;

///////////////////////////////////
//Front Direction -- old driver
int f_left_dir_in1 = 4;
int f_left_dir_in2 = 6;

int f_right_dir_in1 = 2;
int f_right_dir_in2 = A0;

//Front Speed
int f_left_pwm = 3;
int f_right_pwm = 5;
///////////////////////////////////

///////////////////////////////////
//Back Direction -- new driver
int b_left_dir_in1 = 8;
int b_left_dir_in2 = 7;

int b_right_dir_in1 = 12;
int b_right_dir_in2 = 13;

//Back Speed
int b_left_pwm = 10;
int b_right_pwm = 11;

byte PWM_PIN = 9;
int pwm_value;
///////////////////////////////////

void move_forward(int _speed = SPD, int offset = 0);
void move_backward(int _speed = SPD, int offset = 0);

void move_left_backward(int _speed = SPD, int offset = 0);
void move_right_backward(int _speed = SPD, int offset = 0);

void move_left_forward(int _speed = SPD, int offset = 0);
void move_right_forward(int _speed = SPD, int offset = 0);

void move_left_forward_n(int _speed = SPD, int offset = 0);
void move_right_forward_n(int _speed = SPD, int offset = 0);

void setup() {
  pinMode(PWM_PIN, INPUT);
  Serial.begin(115200);
  // front
  pinMode(f_left_dir_in1, OUTPUT);
  pinMode(f_left_dir_in2, OUTPUT);
  
  pinMode(f_right_dir_in1, OUTPUT);
  pinMode(f_right_dir_in2, OUTPUT);
  
  pinMode(f_left_pwm, OUTPUT);
  pinMode(f_right_pwm, OUTPUT);

  //back
  pinMode(b_left_dir_in1, OUTPUT);
  pinMode(b_left_dir_in2, OUTPUT);
  
  pinMode(b_right_dir_in1, OUTPUT);
  pinMode(b_right_dir_in2, OUTPUT);
  
  pinMode(b_left_pwm, OUTPUT);
  pinMode(b_right_pwm, OUTPUT);
  
}

void loop() {
  
  pwm_value = pulseIn(PWM_PIN, HIGH);
  pwm_value = map(pwm_value,0,2557,0,5);
  Serial.println(pwm_value);

  //Extreme Left
  if(pwm_value==0){
    move_left_forward();
    Serial.println("Extreme LEFT");
    delay(500);
  }

  //Left
  if(pwm_value==1){
    move_left_forward();
    Serial.println("LEFT");
    delay(100);
  }

  //FRONT
  if(pwm_value==2){
    move_forward();
    Serial.println("FRONT");
    delay(500);
  }

  //Right
  if(pwm_value==3){
    move_right_forward();
    Serial.println("Right");
    delay(100);
  }

  //Extreme Right
  if(pwm_value==4 or pwm_value==5){
    move_right_forward();
    Serial.println("Extreme Right");
    delay(500);
  }

  
  // put your main code here, to run repeatedly:   
  // 0 REV , 1 FWD
  /*
  move_forward(80,1000);
  delay(100000);
  move_backward(200,0);
  delay(10000);
  
  move_left_forward(200,0);
  delay(10000);
  move_right_forward(200,0);
  delay(10000);

  move_left_backward(200,0);
  delay(10000);
  move_right_backward(200,0);
  delay(10000);
*/

}


void move_forward(int _speed = SPD, int offset = 0){
  //front right direction - fwd
  digitalWrite(f_right_dir_in2, 1);
  digitalWrite(f_right_dir_in1, 0);
  
  //back right  direction - fwd
  digitalWrite(b_right_dir_in2, 1);
  digitalWrite(b_right_dir_in1, 0);

  //front left direction - fwd
  digitalWrite(f_left_dir_in2, 1);
  digitalWrite(f_left_dir_in1, 1);
  
  //back left direction - fwd
  digitalWrite(b_left_dir_in2, 1);
  digitalWrite(b_left_dir_in1, 0);

  //speed
  analogWrite(f_right_pwm, _speed); 
  analogWrite(b_right_pwm, _speed+offset);
  analogWrite(f_left_pwm, _speed); 
  analogWrite(b_left_pwm, _speed+offset);
}

void move_backward(int _speed = SPD, int offset = 0){
  //front right direction - back
  digitalWrite(f_right_dir_in2, 0);
  digitalWrite(f_right_dir_in1, 1);
  
  //back right  direction - back
  digitalWrite(b_right_dir_in2, 0);
  digitalWrite(b_right_dir_in1, 1);

  //front left direction - back
  digitalWrite(f_left_dir_in2, 0);
  digitalWrite(f_left_dir_in1, 1);
  
  //back left direction - back
  digitalWrite(b_left_dir_in2, 0);
  digitalWrite(b_left_dir_in1, 1);

  //speed
  analogWrite(f_right_pwm, _speed); 
  analogWrite(b_right_pwm, _speed+offset);
  analogWrite(f_left_pwm, _speed); 
  analogWrite(b_left_pwm, _speed+offset);
}


////back reverses
void move_left_backward(int _speed = SPD, int offset = 0){
  //front right direction - back
  offAll();
  digitalWrite(f_right_dir_in2, 0);
  digitalWrite(f_right_dir_in1, 1);
  
  //back right  direction - back
  digitalWrite(b_right_dir_in2, 0);
  digitalWrite(b_right_dir_in1, 1);

  //speed
  analogWrite(f_right_pwm, _speed); 
  analogWrite(b_right_pwm, _speed+offset);
  analogWrite(f_left_pwm, 0); 
  analogWrite(b_left_pwm, 0);
}

void move_left_forward(int _speed = SPD, int offset = 0){
  offAll();
  //front right direction - fwd
  digitalWrite(f_right_dir_in2, 1);
  digitalWrite(f_right_dir_in1, 0);
  
  //back right  direction - fwd
  digitalWrite(b_right_dir_in2, 1);
  digitalWrite(b_right_dir_in1, 0);

  //speed
  analogWrite(f_right_pwm, _speed); 
  analogWrite(b_right_pwm, _speed+offset);
  analogWrite(f_left_pwm, 0); 
  analogWrite(b_left_pwm, 0);
}

void move_right_backward(int _speed = SPD, int offset = 0){
  offAll();
  //front left direction - back
  digitalWrite(f_left_dir_in2, 0);
  digitalWrite(f_left_dir_in1, 1);
  
  //back left direction - back
  digitalWrite(b_left_dir_in2, 0);
  digitalWrite(b_left_dir_in1, 1);

  //speed
  analogWrite(f_right_pwm, 0); 
  analogWrite(b_right_pwm, 0);
  analogWrite(f_left_pwm, _speed); 
  analogWrite(b_left_pwm, _speed+offset);
}

void move_right_forward(int _speed = SPD, int offset = 0){
   offAll();
  //front left direction - fwd
  digitalWrite(f_left_dir_in2, 1);
  digitalWrite(f_left_dir_in1, 0);
  //back left direction - fwd
  digitalWrite(b_left_dir_in2, 1);
  digitalWrite(b_left_dir_in1, 0);

  //speed
  analogWrite(f_right_pwm, 0); 
  analogWrite(b_right_pwm, 0);
  analogWrite(f_left_pwm, _speed); 
  analogWrite(b_left_pwm, _speed+offset);
}
//in2 ==high==front




void offAll(){
  digitalWrite(f_left_pwm, 0);
  digitalWrite(f_right_pwm, 0);
  digitalWrite(b_left_pwm, 0);
  digitalWrite(b_right_pwm, 0);
}




void move_right_forward_n(int _speed = SPD, int offset = 0){
   offAll();
  //back left direction - fwd
  digitalWrite(b_left_dir_in2, 1);
  digitalWrite(b_left_dir_in1, 0);

  //back right direction - rev
  digitalWrite(b_right_dir_in2, 0);
  digitalWrite(b_right_dir_in1, 1);

  
  //speed

  analogWrite(b_right_pwm,  _speed); 
  analogWrite(b_left_pwm, _speed);
}



void move_left_forward_n(int _speed = SPD, int offset = 0){
   offAll();
  //back left direction - rev
  digitalWrite(b_left_dir_in2, 0);
  digitalWrite(b_left_dir_in1, 1);

  //back right direction - fwd
  digitalWrite(b_right_dir_in2, 1);
  digitalWrite(b_right_dir_in1, 0);

  
  //speed
  analogWrite(b_right_pwm,  _speed); 
  analogWrite(b_left_pwm, _speed);
}

Comments: (0)

Please loging to post comment