1#include <Servo.h>
2
3Servo servo1;
4Servo servo2;
5
6int pos1 = 0;
7int pos2 = 0;
8int joyUp = 0;
9int joyDown = 0;
10int joyLeft = 0;
11int joyRight = 0;
12
13void setup() {
14 for(int i = 2; i < 6; i++){
15 pinMode(i, OUTPUT);
16 digitalWrite(i, HIGH);
17 }
18
19 for(int j = 6; j < 9; j++){
20 pinMode(j, INPUT);
21 }
22 pinMode(11, INPUT);
23
24 servo1.attach(9);
25 servo2.attach(10);
26}
27
28void loop() {
29 joyUp = digitalRead(6);
30 joyDown = digitalRead(7);
31 joyLeft = digitalRead(8);
32 joyRight = digitalRead(11);
33
34 if (joyUp == 1){
35
36 if (pos2 <= 180){
37 servo2.write(pos2);
38 pos2++;
39 delay(10);
40 }
41 }
42
43 if (joyDown == 1){
44
45 if (pos2 >= 0){
46 servo2.write(pos2);
47 pos2--;
48 delay(10);
49 }
50 }
51
52 if (joyRight == 1){
53
54 if (pos1 <= 180){
55 servo1.write(pos1);
56 pos1++;
57 delay(10);
58 }
59 }
60
61 if (joyLeft == 1){
62
63 if (pos1 >= 0){
64 servo1.write(pos1);
65 pos1--;
66 delay(10);
67 }
68 }
69}
Anonymous user
4 years ago
The wiring to the joystick can be halved and the pulldown resistors removed. Use the built-in pullup resistors in the microcontroller. Steps to make the changes: 1. Connect one side of each of the four limit switches to ground. 2. Connect the other side to Uno pins 6, 7, 8, and 11 as they were originally, but without the resistors. 3. In setup() initialize pins 6,,7,8, and 11 to INPUT_PULLUP. Remove code for pins 2,3,4, 5. 4. When you read the state of the switches, they are the complement of the original (LOW is closed, HIGH is open). So change, for instance "if (joyUp == 1)" to "if (joyUp == LOW)"