Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo servo1;
- Servo servo2;
- int x_pos;
- int y_pos;
- int x_key = A1;
- int y_key = A0;
- int servo1_pin = 10;
- int servo2_pin = 9;
- int initial_position = 90;
- int initial_position1 = 90;
- const int delayConst = 100;
- void setup()
- {
- Serial.begin(9600);
- servo1.attach(servo1_pin);
- servo2.attach(servo2_pin);
- servo1.write(initial_position);
- servo2.write(initial_position1);
- pinMode(x_key, INPUT);
- pinMode(y_key, INPUT);
- }
- void loop ( ) {
- x_pos = analogRead(x_key) ;
- x_pos = map(x_pos, 0, 1023, 0, 180);
- y_pos = analogRead(y_key) ;
- y_pos = map(y_pos, 0, 1023, 0, 90);
- checkX(70, 10, "normal");
- checkX(100, 180, "reverse");
- checkY(35, 0, "normal");
- checkY(50, 89, "reverse");
- /*if (x_pos < 70 && initial_position >= 10)
- {
- initial_position = initial_position - 8;
- servo1.write(initial_position);
- delay(100);
- }
- if (x_pos > 100 && initial_position <= 180)
- {
- initial_position = initial_position + 8;
- servo1.write(initial_position);
- delay(100);
- }
- if (y_pos < 35 && initial_position1 >= 0)
- {
- initial_position1 = initial_position1 - 8;
- servo2.write(initial_position1);
- delay(100);
- }
- if (y_pos > 50 && initial_position1 <= 89)
- {
- initial_position1 = initial_position1 + 8;
- servo2.write(initial_position1);
- delay(100);
- }*/
- }
- void checkX(int x, int initial, String mode)
- {
- if (mode.equals("normal"))
- {
- if (x_pos < x && initial_position >= initial)
- {
- initial_position = initial_position - 8;
- servo1.write(initial_position);
- delay(delayConst);
- }
- }
- else if (mode.equals("reverse"))
- {
- if (x_pos > x && initial_position <= initial)
- {
- initial_position = initial_position + 8;
- servo1.write(initial_position);
- delay(delayConst);
- }
- }
- }
- void checkY(int y, int initial, String mode)
- {
- if (mode.equals("normal"))
- {
- if (y_pos < y && initial_position1 >= initial)
- {
- initial_position1 = initial_position1 - 8;
- servo2.write(initial_position1);
- delay(delayConst);
- }
- }
- else if (mode.equals("reverse"))
- {
- if (y_pos > y && initial_position1 <= initial)
- {
- initial_position1 = initial_position1 + 8;
- servo2.write(initial_position1);
- delay(delayConst);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement