SHARE
TWEET

Untitled

a guest Aug 19th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2.  
  3. Servo myservo;
  4.  
  5.  
  6. void forward(void);
  7. void backward(void);
  8. void hardleft(void);
  9. void hardright(void);
  10. void left(void);
  11. void right(void);
  12. void backleft(void);
  13. void backright(void);
  14. void do_nothing(void);
  15.  
  16. char data=0;
  17.  
  18. int flag=0;
  19. int x=255;
  20. int angle=0;
  21.  
  22. int motorleft_forward = 8;
  23. int motorleft_backward = 9;
  24. int motorright_forward = 13;
  25. int motorright_backward = 12;
  26. int weapon = 4;
  27.  
  28. int pwmleft = 10;
  29. int pwmright = 11;
  30.  
  31. void setup()
  32. {
  33. Serial.begin(9600);
  34.  
  35. pinMode(motorleft_forward,OUTPUT);
  36. pinMode(motorleft_backward,OUTPUT);
  37. pinMode(motorright_forward,OUTPUT);
  38. pinMode(motorright_backward,OUTPUT);
  39. pinMode(weapon,OUTPUT);
  40.  
  41. myservo.attach(3);
  42.  
  43. myservo.write(70);
  44. flag=1;
  45. }
  46.  
  47. void loop()
  48. {
  49. if (Serial.available()>0)
  50. {
  51.     data = Serial.read();
  52.     switch(data)
  53.       {
  54.         case 'F':
  55.         forward();
  56.         break;
  57.         case 'B':
  58.         backward();
  59.         break;
  60.         case 'L':
  61.         hardleft();
  62.         break;
  63.         case 'R':
  64.         hardright();
  65.         break;
  66.         case 'G':
  67.         left();
  68.         break;
  69.         case 'I':
  70.         right();
  71.         break;
  72.         case 'H':
  73.         backright();
  74.         break;
  75.         case 'J':
  76.         backleft();
  77.         break;
  78.         case '0':
  79.         x=80;
  80.         break;
  81.         case '1':
  82.         x=90;
  83.         break;
  84.          case '2':
  85.         x=100;
  86.         break;
  87.          case '3':
  88.         x=120;
  89.         break;
  90.          case '4':
  91.         x=160;
  92.         break;
  93.          case '5':
  94.         x=180;
  95.         break;
  96.          case '6':
  97.         x=200;
  98.         break;
  99.          case '7':
  100.         x=220;
  101.         break;
  102.          case '8':
  103.         x=240;
  104.         break;
  105.          case '9':
  106.         x=255;
  107.         break;
  108.          case 'q':
  109.         x=255;
  110.         break;
  111.         case 'W':
  112.         angle=70;
  113.         break;
  114.         case 'w':
  115.         angle=0;
  116.         break;
  117.         case 'U':
  118.         flag=0;
  119.         break;
  120.         case 'u':
  121.         flag=1;
  122.         break;
  123.         case 'S':
  124.         do_nothing();
  125.         break;
  126.       }
  127.  
  128.       if(flag)
  129.       {
  130.       digitalWrite(weapon, HIGH);
  131.       }
  132.       else
  133.       {
  134.       digitalWrite(weapon, LOW);  
  135.       }
  136. myservo.write(angle);
  137. }
  138.  
  139. }
  140.  
  141. void forward()
  142. {
  143.   analogWrite(pwmleft,x);
  144.   analogWrite(pwmright,x);
  145.   digitalWrite(motorleft_forward,HIGH);
  146.   digitalWrite(motorleft_backward,LOW);
  147.   digitalWrite(motorright_forward,HIGH);
  148.   digitalWrite(motorright_backward,LOW);
  149.   }
  150.   void backward()
  151.   {
  152.     analogWrite(pwmleft,x);
  153.   analogWrite(pwmright,x);
  154.   digitalWrite(motorleft_forward,LOW);
  155.   digitalWrite(motorleft_backward,HIGH);
  156.   digitalWrite(motorright_forward,LOW);
  157.   digitalWrite(motorright_backward,HIGH);
  158.     }
  159.       void hardleft()
  160.   {
  161.     analogWrite(pwmleft,x);
  162.   analogWrite(pwmright,x);
  163.   digitalWrite(motorleft_forward,LOW);
  164.   digitalWrite(motorleft_backward,HIGH);
  165.   digitalWrite(motorright_forward,HIGH);
  166.   digitalWrite(motorright_backward,LOW);
  167.     }
  168.        void hardright()
  169.   {
  170.     analogWrite(pwmleft,x);
  171.   analogWrite(pwmright,x);
  172.   digitalWrite(motorleft_forward,HIGH);
  173.   digitalWrite(motorleft_backward,LOW);
  174.   digitalWrite(motorright_forward,LOW);
  175.   digitalWrite(motorright_backward,HIGH);
  176.     }
  177.      void left()
  178.   {
  179.     analogWrite(pwmleft,x);
  180.   analogWrite(pwmright,x);
  181.   digitalWrite(motorleft_forward,LOW);
  182.   digitalWrite(motorleft_backward,LOW);
  183.   digitalWrite(motorright_forward,HIGH);
  184.   digitalWrite(motorright_backward,LOW);
  185.     }
  186.     void right()
  187.   {
  188.     analogWrite(pwmleft,x);
  189.   analogWrite(pwmright,x);
  190.   digitalWrite(motorleft_forward,HIGH);
  191.   digitalWrite(motorleft_backward,LOW);
  192.   digitalWrite(motorright_forward,LOW);
  193.   digitalWrite(motorright_backward,LOW);
  194.     }
  195.     void do_nothing()
  196.       {
  197.     analogWrite(pwmleft,255);
  198.   analogWrite(pwmright,255);
  199.   digitalWrite(motorleft_forward,LOW);
  200.   digitalWrite(motorleft_backward,LOW);
  201.   digitalWrite(motorright_forward,LOW);
  202.   digitalWrite(motorright_backward,LOW);
  203.     }
  204.  
  205.  
  206.  
  207.     void backleft()
  208.   {
  209.     analogWrite(pwmleft,x);
  210.   analogWrite(pwmright,x);
  211.   digitalWrite(motorleft_forward,LOW);
  212.   digitalWrite(motorleft_backward,LOW);
  213.   digitalWrite(motorright_forward,LOW);
  214.   digitalWrite(motorright_backward,HIGH);
  215.     }
  216.     void backright()
  217.   {
  218.     analogWrite(pwmleft,x);
  219.   analogWrite(pwmright,x);
  220.   digitalWrite(motorleft_forward,LOW);
  221.   digitalWrite(motorleft_backward,HIGH);
  222.   digitalWrite(motorright_forward,LOW);
  223.   digitalWrite(motorright_backward,LOW);
  224.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top