tripishadow

servos modificados

Nov 29th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. /*
  2. This code runs vixen through the arduino, usining pins 3, 5, and 6 with
  3. 3 seperate servos; pins 9, 10, and 11 are used for 3 LED circuits with PWM;
  4. pins 12, 13 are extra digital pins.
  5.  
  6. Adapted from code by GSRVAhiker17, zparticle, victor_pv, Si_champion, and Neil Tapp.
  7. */
  8.  
  9. #include <Servo.h> // Servos
  10. #include <avr/wdt.h> // Raw AVR watchdog timer
  11.  
  12. // Testing mode (treat servos as simple PWM)
  13. const bool TESTINGMODE = false;
  14.  
  15. // Vixen header (identifies a light sequence)
  16. const int HEADER_LEN = 2;
  17. const char seqHeader[] = {'~','!'};
  18.  
  19. // Timeout waiting for serial input before going to random mode (in milliseconds).
  20. const int TIME_OUT = 1000;
  21.  
  22. // Channels mapped to pin numbers
  23. const int BOCA_BOLA1 = 9;
  24. const int BOCA_BOLA2 = 10;
  25. const int BOCA_BOLA3 = 11;
  26. const int BOCA_BOLA4 = 2;
  27. const int OJOS_BOLA1 = 3;
  28. const int OJOS_BOLA2 = 5;
  29. const int OJOS_BOLA3 = 6;
  30. const int OJOS_BOLA4 = 4;
  31. const int LUCES1 = 12;
  32. const int LUCES2 = 13;
  33. const int CAN11 = 5;
  34. const int CAN12 = 7;
  35. const int CAN13 = A0;
  36. const int CAN14 = A1;
  37. const int CAN15 = A2;
  38. const int CAN16 = A3;
  39. const int CAN17 = A4;
  40. const int CAN18 = A5;
  41.  
  42. // List of active channels
  43. const int channels[] = {BOCA_BOLA1, BOCA_BOLA2, BOCA_BOLA3,BOCA_BOLA4,OJOS_BOLA1, OJOS_BOLA2, OJOS_BOLA3, OJOS_BOLA4, LUCES1, LUCES2,
  44. CAN11, CAN12, CAN13, CAN14, CAN15, CAN16, CAN17, CAN18};
  45.  
  46. // Number of active channels
  47. const int NUM_ACTIVE_CHANNELS = 4;
  48.  
  49. // PWM map
  50. const int isPWM[] = {true, true, true, true, false, false, false, false, false, false,
  51. false, false, false, false, false, false, false, false
  52. };
  53.  
  54. // Servos
  55. const int SERVO_DELAY = 15; // delay after servo is activated (allow it to move)
  56. const int NUM_SERVOS = 1;
  57. const int NEUTRAL = 90; // Neutral position
  58. Servo servos[NUM_SERVOS];
  59.  
  60. // Min servo opening in degrees from neutral position
  61. const int servoMin[] = {0, 0, 0, 0};
  62.  
  63. // Max servo opening in degrees from neutral position
  64. const int servoMax[] = {90, 45, 45, 45};
  65. //const int servoMax[] = {45, 15, 15, 15};
  66.  
  67. // Servo channel map
  68. const int NO_SERVO = -1;
  69. const int servoNumber[] = {0,1, 2, 3, NO_SERVO, NO_SERVO, NO_SERVO, NO_SERVO,
  70. NO_SERVO, NO_SERVO, NO_SERVO, NO_SERVO, NO_SERVO, NO_SERVO, NO_SERVO, NO_SERVO,
  71. NO_SERVO, NO_SERVO
  72. };
  73.  
  74. // Servo direction
  75. const int CLOCKWISE = 1;
  76. const int COUNTERCLOCKWISE = -1;
  77. const int servoDirection[] = {COUNTERCLOCKWISE, CLOCKWISE, CLOCKWISE};
  78.  
  79. // Serial
  80. const long COM_SPEED = 115200;
  81. int incomingByte[NUM_ACTIVE_CHANNELS]; // array to store the channel values from the serial port
  82.  
  83. // Misc
  84. int i = 0; // Loop counter
  85. int j = 0; // Loop counter
  86. volatile unsigned long timer_a = 0; // Timer
  87.  
  88.  
  89. //setup the pins/ inputs & outputs
  90. void setup()
  91. {
  92. // enable the watchdog timer with a time of 1 second. If the board freezes, it will reset itself after 1 second.
  93. wdt_enable(WDTO_1S);
  94.  
  95. // specifically for the UNO
  96. sei();
  97.  
  98. // initalize PWM Channels / Pins
  99. for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
  100. pinMode(channels[i], OUTPUT);
  101. if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) {
  102. servos[servoNumber[i]].attach(channels[i]);
  103. }
  104. }
  105.  
  106. // set all the channels off to begin
  107. for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
  108. digitalWrite(channels[i], LOW);
  109. if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) {
  110. servos[servoNumber[i]].write(NEUTRAL);
  111. }
  112. }
  113.  
  114. test_sequence(); // brief test
  115. Serial.begin(COM_SPEED); // set up Serial
  116. }
  117.  
  118. void loop()
  119. {
  120. if (Serial.available() >= NUM_ACTIVE_CHANNELS + HEADER_LEN) {
  121. wdt_reset(); // resets the watchdog (prevents board lockup)
  122. timer_a = millis (); // Mark the time when a message was received
  123.  
  124. // read the header to verify this is in fact a light sequence
  125. // probably overkill, but borrowing from the above sources...
  126. for (int i = 0; i < HEADER_LEN; i++) {
  127. if (seqHeader[i] != Serial.read()) { return; }
  128. //Serial.read();
  129. }
  130.  
  131. // read the oldest byte in the serial buffer:
  132. for (int i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
  133. // read each byte
  134. incomingByte[i] = Serial.read();
  135.  
  136. if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) {
  137. // SERVOS ------------------------------
  138. int angle = map(incomingByte[i], 0, 255, servoMin[servoNumber[i]], servoMax[servoNumber[i]]);
  139. angle *= servoDirection[servoNumber[i]];
  140. servos[servoNumber[i]].write(NEUTRAL + angle);
  141. delay(SERVO_DELAY);
  142. } else if (isPWM[i]) {
  143. // PWM ---------------------------------
  144. analogWrite(channels[i], incomingByte[i]);
  145. } else {
  146. // DIGITAL (A2D) -----------------------
  147. if (incomingByte[i] <= 127) {
  148. digitalWrite(channels[i], LOW);
  149. } else {
  150. digitalWrite(channels[i], HIGH);
  151. }
  152. }
  153. }
  154.  
  155. } else {
  156. // Random mode starts if no serial input has been received in TIME_OUT milliseconds
  157. wdt_reset(); // resets the watchdog (prevents board lockup)
  158. unsigned long diff = millis() - timer_a;
  159. if (diff >= TIME_OUT) {
  160. timer_a = millis ();
  161. int random_a = 0;
  162. for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
  163. if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) continue;
  164. random_a = random(0, 2);
  165. if (random_a == 0) {
  166. digitalWrite(channels[i], LOW);
  167. } else {
  168. digitalWrite(channels[i], HIGH);
  169. }
  170. }
  171. }
  172. }
  173. }
  174.  
  175. // Test the setup briefly
  176. void test_sequence() {
  177. for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
  178. wdt_reset(); // resets the watchdog
  179. digitalWrite(channels[i], HIGH);
  180. delay (500);
  181. digitalWrite(channels[i], LOW);
  182. }
  183. }
Add Comment
Please, Sign In to add comment