Advertisement
Guest User

Arduino compressed

a guest
Feb 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo servo1;
  4. Servo servo2;
  5.  
  6. int x_pos;
  7. int y_pos;
  8.  
  9. int x_key = A1;
  10. int y_key = A0;
  11. int servo1_pin = 10;
  12. int servo2_pin = 9;
  13. int initial_position = 90;
  14. int initial_position1 = 90;
  15.  
  16. const int delayConst = 100;
  17.  
  18. void setup()
  19. {
  20.   Serial.begin(9600);
  21.   servo1.attach(servo1_pin);
  22.   servo2.attach(servo2_pin);
  23.   servo1.write(initial_position);
  24.   servo2.write(initial_position1);
  25.   pinMode(x_key, INPUT);
  26.   pinMode(y_key, INPUT);
  27. }
  28.  
  29. void loop ( ) {
  30.   x_pos = analogRead(x_key) ;
  31.   x_pos = map(x_pos, 0, 1023, 0, 180);
  32.   y_pos = analogRead(y_key) ;
  33.   y_pos = map(y_pos, 0, 1023, 0, 90);
  34.  
  35.   checkX(70, 10, "normal");
  36.   checkX(100, 180, "reverse");
  37.   checkY(35, 0, "normal");
  38.   checkY(50, 89, "reverse");
  39.  
  40.   /*if (x_pos < 70 && initial_position >= 10)
  41.   {
  42.     initial_position = initial_position - 8;
  43.     servo1.write(initial_position);
  44.     delay(100);
  45.   }
  46.   if (x_pos > 100 && initial_position <= 180)
  47.   {
  48.     initial_position = initial_position + 8;
  49.     servo1.write(initial_position);
  50.     delay(100);
  51.   }
  52.   if (y_pos < 35 && initial_position1 >= 0)
  53.   {
  54.     initial_position1 = initial_position1 - 8;
  55.     servo2.write(initial_position1);
  56.     delay(100);
  57.   }
  58.   if (y_pos > 50 && initial_position1 <= 89)
  59.   {
  60.     initial_position1 = initial_position1 + 8;
  61.     servo2.write(initial_position1);
  62.     delay(100);
  63.   }*/
  64. }
  65.  
  66. void checkX(int x, int initial, String mode)
  67. {
  68.   if (mode.equals("normal"))
  69.   {
  70.     if (x_pos < x && initial_position >= initial)
  71.     {
  72.       initial_position = initial_position - 8;
  73.       servo1.write(initial_position);
  74.       delay(delayConst);
  75.     }
  76.   }
  77.   else if (mode.equals("reverse"))
  78.   {
  79.     if (x_pos > x && initial_position <= initial)
  80.     {
  81.       initial_position = initial_position + 8;
  82.       servo1.write(initial_position);
  83.       delay(delayConst);
  84.     }
  85.   }
  86. }
  87.  
  88. void checkY(int y, int initial, String mode)
  89. {
  90.   if (mode.equals("normal"))
  91.   {
  92.     if (y_pos < y && initial_position1 >= initial)
  93.     {
  94.       initial_position1 = initial_position1 - 8;
  95.       servo2.write(initial_position1);
  96.       delay(delayConst);
  97.     }
  98.   }
  99.   else if (mode.equals("reverse"))
  100.   {
  101.     if (y_pos > y && initial_position1 <= initial)
  102.     {
  103.       initial_position1 = initial_position1 + 8;
  104.       servo2.write(initial_position1);
  105.       delay(delayConst);
  106.     }
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement