safwan092

car-robot-bt-safwan-code-test

Sep 30th, 2018
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. unsigned char Bluetooth_val;       //defining variable val
  3. #define Lpwm_pin  10 //enB
  4. #define Rpwm_pin  11 //enA
  5. int pinLB = 6; //in4
  6. int pinLF = 7; //in3
  7. int pinRB = 8; //in2
  8. int pinRF = 9; //in1
  9. unsigned char Lpwm_val = 255;
  10. unsigned char Rpwm_val = 255;
  11.  
  12. int Car_state = 0;
  13.  
  14. void M_Control_IO_config(void)
  15. {
  16.   pinMode(pinLB, OUTPUT);
  17.   pinMode(pinLF, OUTPUT);
  18.   pinMode(pinRB, OUTPUT);
  19.   pinMode(pinRF, OUTPUT);
  20.   pinMode(Lpwm_pin, OUTPUT); // pin 11 (PWM)
  21.   pinMode(Rpwm_pin, OUTPUT); // pin 10 (PWM)
  22. }
  23. void Set_Speed(unsigned char Left, unsigned char Right)
  24. {
  25.   analogWrite(Lpwm_pin, Left);
  26.   analogWrite(Rpwm_pin, Right);
  27. }
  28. void turnR()     //  going forward
  29. {
  30.   digitalWrite(pinRB, LOW); // making motor move towards right rear
  31.   digitalWrite(pinRF, HIGH);
  32.   digitalWrite(pinLB, LOW); // making motor move towards left rear
  33.   digitalWrite(pinLF, HIGH);
  34.   Car_state = 1;
  35. }
  36. void back()        //turning right(dual wheel)
  37. {
  38.   digitalWrite(pinRB, LOW); //making motor move towards right rear
  39.   digitalWrite(pinRF, HIGH);
  40.   digitalWrite(pinLB, HIGH);
  41.   digitalWrite(pinLF, LOW); //making motor move towards left front
  42.   Car_state = 4;
  43. }
  44. void advance()        //turning left(dual wheel)
  45. {
  46.   digitalWrite(pinRB, HIGH);
  47.   digitalWrite(pinRF, LOW );  //making motor move towards right front
  48.   digitalWrite(pinLB, LOW);  //making motor move towards left rear
  49.   digitalWrite(pinLF, HIGH);
  50.   Car_state = 3;
  51. }
  52. void stopp()         //stop
  53. {
  54.   digitalWrite(pinRB, HIGH);
  55.   digitalWrite(pinRF, HIGH);
  56.   digitalWrite(pinLB, HIGH);
  57.   digitalWrite(pinLF, HIGH);
  58.   Car_state = 5;
  59. }
  60. void turnL()          //back up
  61. {
  62.   digitalWrite(pinRB, HIGH); //making motor move towards right rear
  63.   digitalWrite(pinRF, LOW);
  64.   digitalWrite(pinLB, HIGH); //making motor move towards left rear
  65.   digitalWrite(pinLF, LOW);
  66.   Car_state = 2;
  67. }
  68.  
  69. void forwardLeft() {
  70.   advance();
  71.   delay(50);
  72.   turnL();
  73.   delay(100);
  74.   advance();
  75.   delay(50);
  76. } //G
  77. void forwardRight() {
  78.   advance();
  79.   delay(50);
  80.   turnR();
  81.   delay(100);
  82.   advance();
  83.   delay(50);
  84. } //I
  85. void backLeft() {
  86.   back();
  87.   delay(50);
  88.   turnR();
  89.   delay(100);
  90.   back();
  91.   delay(50);
  92. } //H
  93. void backRight() {
  94.   back();
  95.   delay(50);
  96.   turnL();
  97.   delay(100);
  98.   back();
  99.   delay(50);
  100. } //J
  101.  
  102. void hornON(){digitalWrite(4,1);}
  103. void hornOFF(){digitalWrite(4,0);}
  104. void setup()
  105. {
  106.   pinMode(4,OUTPUT);
  107.   M_Control_IO_config();
  108.   Set_Speed(Lpwm_val, Rpwm_val);
  109.   Serial.begin(9600);   //initialized serial port , using Bluetooth as serial port, setting baud at 9600
  110.   stopp();
  111. }
  112.  
  113. void loop()
  114. {
  115.   if (Serial.available()) //to judge whether the serial port receives the data.
  116.   {
  117.     Bluetooth_val = Serial.read(); //reading (Bluetooth) data of serial port,giving the value of val;
  118.     switch (Bluetooth_val)
  119.     {
  120.       case 'F': advance(); //UP
  121.         break;
  122.       case 'B': back();   //back
  123.         break;
  124.       case 'L': turnL();  //Left
  125.         break;
  126.       case 'R': turnR(); //Right
  127.         break;
  128.       case 'S': stopp();   //stop
  129.         break;
  130.       case 'G': stopp();
  131.         break;
  132.       case 'I': stopp();
  133.         break;
  134.       case 'H': stopp();
  135.         break;
  136.       case 'J': stopp();
  137.         break;
  138.       case 'W': stopp();
  139.         break;
  140.       case 'w': stopp();
  141.         break;
  142.       case 'U': stopp();
  143.         break;
  144.       case 'u': stopp();
  145.         break;
  146.       case 'V': hornON();
  147.         break;
  148.       case 'v': hornOFF();
  149.         break;
  150.       case 'X': stopp();
  151.         break;
  152.       case 'x': stopp();
  153.         break;
  154.       case 'D': stopp();
  155.         break;
  156.     }
  157.   }
  158. }
RAW Paste Data