Advertisement
safwan092

4WD Car Robot BT (HC-05)

May 1st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. unsigned char Bluetooth_val;       //defining variable val
  2.  
  3. #define Lpwm_pin  5     //adjusting speed  
  4. #define Rpwm_pin  10    //adjusting speed
  5.  
  6. int pinLB=2;     // defining pin2 left rear
  7. int pinLF=4;     // defining pin4 left front
  8. int pinRB=7;    // defining pin7 right rear
  9. int pinRF=8;    // defining pin8 right front
  10.  
  11. unsigned char Lpwm_val = 255;
  12. unsigned char Rpwm_val = 255;
  13.  
  14. int Car_state=0;
  15.  
  16. void M_Control_IO_config(void)
  17. {
  18.   pinMode(pinLB,OUTPUT); // pin 2
  19.   pinMode(pinLF,OUTPUT); // pin 4
  20.   pinMode(pinRB,OUTPUT); // pin 7
  21.   pinMode(pinRF,OUTPUT); // pin 8
  22.   pinMode(Lpwm_pin,OUTPUT); // pin 11 (PWM)
  23.   pinMode(Rpwm_pin,OUTPUT); // pin 10 (PWM)  
  24. }
  25. void Set_Speed(unsigned char Left,unsigned char Right)
  26. {
  27.   analogWrite(Lpwm_pin,Left);
  28.   analogWrite(Rpwm_pin,Right);
  29. }
  30.  
  31. void advance()     //  going forward
  32.     {
  33.      digitalWrite(pinRB,LOW);  // making motor move towards right rear
  34.      digitalWrite(pinRF,HIGH);
  35.      digitalWrite(pinLB,LOW);  // making motor move towards left rear
  36.      digitalWrite(pinLF,HIGH);
  37.      Car_state = 1;
  38.     }
  39.    
  40. void turnR()        //turning right(dual wheel)
  41.     {
  42.      digitalWrite(pinRB,LOW);  //making motor move towards right rear
  43.      digitalWrite(pinRF,HIGH);
  44.      digitalWrite(pinLB,HIGH);
  45.      digitalWrite(pinLF,LOW);  //making motor move towards left front
  46.      Car_state = 4;
  47.     }
  48.    
  49. void turnL()        //turning left(dual wheel)
  50.     {
  51.      digitalWrite(pinRB,HIGH);
  52.      digitalWrite(pinRF,LOW );   //making motor move towards right front
  53.      digitalWrite(pinLB,LOW);   //making motor move towards left rear
  54.      digitalWrite(pinLF,HIGH);
  55.      Car_state = 3;
  56.     }    
  57.    
  58. void stopp()         //stop
  59.     {
  60.      digitalWrite(pinRB,HIGH);
  61.      digitalWrite(pinRF,HIGH);
  62.      digitalWrite(pinLB,HIGH);
  63.      digitalWrite(pinLF,HIGH);
  64.      Car_state = 5;
  65.     }
  66.    
  67. void back()          //back up
  68.     {
  69.      digitalWrite(pinRB,HIGH);  //making motor move towards right rear
  70.      digitalWrite(pinRF,LOW);
  71.      digitalWrite(pinLB,HIGH);  //making motor move towards left rear
  72.      digitalWrite(pinLF,LOW);
  73.      Car_state = 2;
  74.     }
  75.    
  76.      
  77. void setup()
  78. {
  79.    M_Control_IO_config();
  80.    
  81.    Set_Speed(Lpwm_val,Rpwm_val);
  82.    
  83.    Serial.begin(9600);   //initialized serial port , using Bluetooth as serial port, setting baud at 9600
  84.    
  85.    stopp();
  86. }
  87. void loop()
  88. {  
  89.  
  90.    if(Serial.available()) //to judge whether the serial port receives the data.
  91.     {
  92.      Bluetooth_val=Serial.read();  //reading (Bluetooth) data of serial port,giving the value of val;
  93.     switch(Bluetooth_val)
  94.      {
  95.        case 'U':advance(); //UP
  96.        break;
  97.        case 'D': back();   //back
  98.        break;
  99.        case 'L':turnL();   //Left
  100.        break;
  101.        case 'R':turnR();  //Right
  102.        break;
  103.        case 'S':stopp();    //stop
  104.        break;  
  105.      }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement