Advertisement
kackle1998

Wireless Nerf Gun Arduino

Dec 5th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo panServo; Servo tiltServo; Servo fireServo; // create servo object to control a servo
  4.  
  5. int tiltSpeed = 3; int panSpeed = 3;
  6. int tiltPin = 10; int panPin = 9; int firePin = 8;
  7. int tiltAngle; int panAngle; int fireAngle; //119~150
  8. int command;
  9.  
  10. void setup() {
  11. Serial.begin(9600);
  12. panServo.attach(panPin);
  13. tiltServo.attach(tiltPin);
  14. fireServo.attach(firePin);
  15. tiltAngle=5; panAngle=5; fireAngle = 133;
  16. fireServo.write(fireAngle);
  17. }
  18.  
  19. void loop() {
  20. command = Serial.read();
  21.  
  22. if (command == 0){
  23. moveUp();
  24. }
  25.  
  26. if (command == 1){
  27. moveDown();
  28. }
  29.  
  30. if (command == 2){
  31. moveLeft();
  32. }
  33.  
  34. if (command == 3){
  35. moveRight();
  36. }
  37.  
  38. if (command == 4){
  39. shootGun();
  40. }
  41.  
  42. tiltServo.write(tiltAngle);
  43. panServo.write(panAngle);
  44. }
  45.  
  46. void moveUp(){
  47. if (tiltAngle < 180)
  48. tiltAngle+=tiltSpeed;
  49. }
  50.  
  51. void moveDown(){
  52. if (tiltAngle > 0)
  53. tiltAngle-=tiltSpeed;
  54. }
  55.  
  56. void moveLeft(){
  57. if (panAngle < 180)
  58. panAngle+=panSpeed;
  59. }
  60.  
  61. void moveRight(){
  62. if (panAngle > 0)
  63. panAngle-=panSpeed;
  64. }
  65.  
  66. void shootGun(){
  67. fireServo.write(150);
  68. delay(500);
  69. fireServo.write(133);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement