Advertisement
safwan092

Untitled

May 1st, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. #include <Wire.h>                //including the libraries of I2C
  2. #include <IRremote.h>           // https://github.com/z3t0/Arduino-IRremote/archive/master.zip
  3.  
  4. int RECV_PIN = 12;
  5. IRrecv irrecv(RECV_PIN);
  6. decode_results results;
  7. #define IR_Go      0x00ff629d
  8. #define IR_Back    0x00ffa857
  9. #define IR_Left    0x00ff22dd
  10. #define IR_Right   0x00ffc23d
  11. #define IR_Stop    0x00ff02fd
  12. #define IR_ESC     0x00ff52ad
  13.  
  14. unsigned char Bluetooth_val;       //defining variable val
  15.  
  16. #define Lpwm_pin  5     //adjusting speed  
  17. #define Rpwm_pin  10    //adjusting speed
  18.  
  19. int pinLB = 2;   // defining pin2 left rear
  20. int pinLF = 4;   // defining pin4 left front
  21. int pinRB = 7;  // defining pin7 right rear
  22. int pinRF = 8;  // defining pin8 right front
  23.  
  24. unsigned char Lpwm_val = 255;
  25. unsigned char Rpwm_val = 255;
  26.  
  27. int Car_state = 0;
  28.  
  29. void M_Control_IO_config(void)
  30. {
  31.   pinMode(pinLB, OUTPUT); // pin 2
  32.   pinMode(pinLF, OUTPUT); // pin 4
  33.   pinMode(pinRB, OUTPUT); // pin 7
  34.   pinMode(pinRF, OUTPUT); // pin 8
  35.   pinMode(Lpwm_pin, OUTPUT); // pin 11 (PWM)
  36.   pinMode(Rpwm_pin, OUTPUT); // pin 10 (PWM)
  37. }
  38. void Set_Speed(unsigned char Left, unsigned char Right)
  39. {
  40.   analogWrite(Lpwm_pin, Left);
  41.   analogWrite(Rpwm_pin, Right);
  42. }
  43.  
  44. void advance()     //  going forward
  45. {
  46.   digitalWrite(pinRB, LOW); // making motor move towards right rear
  47.   digitalWrite(pinRF, HIGH);
  48.   digitalWrite(pinLB, LOW); // making motor move towards left rear
  49.   digitalWrite(pinLF, HIGH);
  50.   Car_state = 1;
  51. }
  52.  
  53. void turnR()        //turning right(dual wheel)
  54. {
  55.   digitalWrite(pinRB, LOW); //making motor move towards right rear
  56.   digitalWrite(pinRF, HIGH);
  57.   digitalWrite(pinLB, HIGH);
  58.   digitalWrite(pinLF, LOW); //making motor move towards left front
  59.   Car_state = 4;
  60. }
  61.  
  62. void turnL()        //turning left(dual wheel)
  63. {
  64.   digitalWrite(pinRB, HIGH);
  65.   digitalWrite(pinRF, LOW );  //making motor move towards right front
  66.   digitalWrite(pinLB, LOW);  //making motor move towards left rear
  67.   digitalWrite(pinLF, HIGH);
  68.   Car_state = 3;
  69. }
  70.  
  71. void stopp()         //stop
  72. {
  73.   digitalWrite(pinRB, HIGH);
  74.   digitalWrite(pinRF, HIGH);
  75.   digitalWrite(pinLB, HIGH);
  76.   digitalWrite(pinLF, HIGH);
  77.   Car_state = 5;
  78. }
  79.  
  80. void back()          //back up
  81. {
  82.   digitalWrite(pinRB, HIGH); //making motor move towards right rear
  83.   digitalWrite(pinRF, LOW);
  84.   digitalWrite(pinLB, HIGH); //making motor move towards left rear
  85.   digitalWrite(pinLF, LOW);
  86.   Car_state = 2;
  87. }
  88.  
  89.  
  90. void IR_Control(void)
  91. {
  92.   unsigned long Key;
  93.   while (Key != IR_ESC)
  94.   {
  95.     if (irrecv.decode(&results)) //judging if serial port receives data
  96.     {
  97.       Key = results.value;
  98.       switch (Key)
  99.       {
  100.         case IR_Go: advance();  //UP
  101.           break;
  102.         case IR_Back: back();   //back
  103.           break;
  104.         case IR_Left: turnL();  //Left
  105.           break;
  106.         case IR_Right: turnR(); //Righ
  107.           break;
  108.         case IR_Stop: stopp();  //stop
  109.           break;
  110.         default:
  111.           break;
  112.       }
  113.       irrecv.resume(); // Receive the next value
  114.     }
  115.   }
  116.   stopp();
  117. }
  118.  
  119.  
  120. void setup()
  121. {
  122.  
  123.   irrecv.enableIRIn(); // Start the receiver
  124.  
  125.   M_Control_IO_config();
  126.  
  127.   Set_Speed(Lpwm_val, Rpwm_val);
  128.  
  129.   Serial.begin(9600);   //initialized serial port , using Bluetooth as serial port, setting baud at 9600
  130.  
  131.   stopp();
  132. }
  133.  
  134.  
  135. void loop()
  136. {
  137.  
  138.   if (irrecv.decode(&results)) {
  139.     if (results.value == IR_Stop )IR_Control();
  140.     irrecv.resume(); // Receive the next value
  141.   }
  142.  
  143.   if (Serial.available()) //to judge whether the serial port receives the data.
  144.   {
  145.     Bluetooth_val = Serial.read(); //reading (Bluetooth) data of serial port,giving the value of val;
  146.     switch (Bluetooth_val)
  147.     {
  148.       case 'U': advance(); //UP
  149.         break;
  150.       case 'D': back();   //back
  151.         break;
  152.       case 'L': turnL();  //Left
  153.         break;
  154.       case 'R': turnR(); //Right
  155.         break;
  156.       case 'S': stopp();   //stop
  157.         break;
  158.     }
  159.   }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement