# Untitled

a guest
Feb 6th, 2018
136
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <Servo.h>
2. // min and max position for each of the 50 servo channels, each line consists of 10 channels
3. int min_max[51][2]  = {
4.   {0,0}, {0,0}, {15,170}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180},
5.   {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180},
6.   {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180},
7.   {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180},
8.   {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180}, {0,180},
9. };
10. Servo myservo;
11. int forwards_state = 0;
12. int old_forwards_state = 0;
13. int backwards_state = 0;
14. int old_backwards_state = 0;
15. int channel = 2;
16. int i = 2;
17. int val;
18. const int forwards_pin = 53;
19. const int backwards_pin = 52;
20. const int pot_pin = A0;
21. void setup() {
22.    pinMode(forwards_pin, INPUT);
23.    pinMode(backwards_pin, INPUT);
24.    pinMode(pot_pin, INPUT);
25.    Serial.begin(9600);
26.    do {
27.    pinMode(i, OUTPUT);
28.    i = i+1;
29.    delay(1);
30.    } while (i <= 51);
31. }
32. void loop() {
35.   if (forwards_state != old_forwards_state && forwards_state == true){
36.     if (channel < 51) {
37.     channel = channel+1;
38.     } else {
39.     channel = 2;
40.     }
41.   } else if (backwards_state != old_backwards_state && backwards_state == true){
42.      if (channel > 2) {
43.     channel = channel-1;
44.     } else {
45.     channel = 51;
46.     }
47.   }
48.   old_forwards_state =  forwards_state;
49.   old_backwards_state = backwards_state;
50.
52.   val = map(val, 0, 1023, 0, 180);
53. // constraint servo angles within the min_max positions defined above
54.   if (val > min_max[channel][1]) {
55.     val = min_max[channel][1];
56.   } else if (val < min_max[channel][0]) {
57.     val = min_max[channel][0];
58.   }
59.   myservo.attach(channel);
60.
61.   myservo.write(val);
62. //  myservo.detach();
63.
64.   Serial.println(channel);
65.   delay(5);
66.
67. }
RAW Paste Data