Guest User

Gigameter 1.2

a guest
Apr 11th, 2017
196
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <VarSpeedServo.h>
  2.  
  3. // Arduino-Based Gigameter Prop - Code Version Public 1.2
  4. //
  5. // Code written by Josh Teasdale - WindDrake @ GBFans.com Forums
  6. //
  7. // This sketch requires VarSpeedServo.h library!
  8. // https://github.com/netlabtoolkit/VarSpeedServo
  9. //
  10. // Program released under Creative Commons - Attribution - Share Alike License
  11. //
  12. // Default pin setup is for an Arduino Nano. Do not use a Micro as it cannot sink as much current as the Nano.
  13. //
  14. // This sketch allows for Ears Only and Ears & Dome mode operation via the normally unused pot on the left side of the handle.
  15. // Otherwise it should be close to the screen prop - 3 lights in each ear, servo sweep, a display that picks a random number while scanning, sound, and a spinning dome with lights.
  16. //
  17. // You may have to change the throw angle on the servo. Look for the Servo Sweeping Routine and follow the comments.
  18. //
  19. // Used Digital IO: 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2
  20. // Used Analog IO: A0, A4, A5
  21. //
  22. //Analog Inputs
  23. int modepot = A0;
  24. //Digital Outputs
  25. int debugled = 13;
  26. int powerup = 12;
  27. int scanning = 11;
  28. int motordome = 10;
  29. int ear1 = 9;
  30. int ear2 = 8;
  31. int ear3 = 7;
  32. int ear11 = 4;
  33. int ear22 = 3;
  34. int ear33 = 2;
  35. int DisplayDI = A4;
  36. int DisplayCLK = A5;
  37. //Digital Inputs
  38. int scanswitch = 5; //SCAN Switch to Pin 5
  39. //Servo Setup
  40. VarSpeedServo servo1; //Servo PWM Output
  41. //Timer Variables
  42. unsigned long previousMillis1 = 0; //Ear LED LastUpdated Variable
  43. unsigned long previousMillis2 = 0; //Ear LED LastUpdated Variable
  44. unsigned long previousMillis3 = 0; //Ear LED LastUpdated Variable
  45. unsigned long previousMillis4 = 0; //Ear Sweep LastUpdated Variable
  46. unsigned long previousMillis5 = 0; //Display LastUpdated Variable
  47. unsigned long currentMillis = 0; //Current Time
  48. //Ear LED Cycle Times
  49. long EarOnTime = 333;
  50. long EarOffTime = 666;
  51. //Ear State Variables
  52. int EarLed1 = HIGH;
  53. int EarLed2 = HIGH;
  54. int EarLed3 = HIGH;
  55. int EarsEnable = 0; //Ears Enabled or Not
  56. int EarsOpen = 0; //Ears Open (1) or Shut (0)
  57. //Motor Relay Variable
  58. int MotorEnable = 0; //Motor Relay
  59. //Mode Pot Value Variable
  60. int PotVal = 0; //Mode Pot value
  61. //Display Update Timer
  62. long DisplayTime = 1100;
  63. //Display Digits Conversion Table
  64. byte dec_digits[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b00111110,0b11100000,0b11111110,0b11100110};
  65. byte dec_digitsperiod[] = {0b11111101,0b01100001,0b11011011,0b11110011,0b01100111,0b00111111,0b11100001,0b11111111,0b11100111};
  66. unsigned int RNum1 = 0;
  67. unsigned int RNum2 = 0;
  68. unsigned int RNum3 = 0;
  69.  
  70. // Startup Routine
  71. void setup() {
  72. // Initialize all pins!
  73. pinMode(modepot,INPUT);
  74. pinMode(scanswitch, INPUT_PULLUP);
  75. pinMode(powerup,OUTPUT);
  76. pinMode(scanning,OUTPUT);
  77. pinMode(motordome,OUTPUT);
  78. pinMode(ear1,OUTPUT);
  79. pinMode(ear2,OUTPUT);
  80. pinMode(ear3,OUTPUT);
  81. pinMode(ear11,OUTPUT);
  82. pinMode(ear22,OUTPUT);
  83. pinMode(ear33,OUTPUT);
  84. pinMode(DisplayDI,OUTPUT);
  85. pinMode(DisplayCLK,OUTPUT);
  86. //Setting up the Servo.
  87. servo1.attach(6);
  88. servo1.slowmove(90, 0);
  89. //Now setting all outputs to known state.
  90. digitalWrite(powerup,HIGH);
  91. digitalWrite(scanning,HIGH);
  92. digitalWrite(motordome,LOW);
  93. digitalWrite(ear1,HIGH);
  94. digitalWrite(ear2,HIGH);
  95. digitalWrite(ear3,HIGH);
  96. digitalWrite(ear11,HIGH);
  97. digitalWrite(ear22,HIGH);
  98. digitalWrite(ear33,HIGH);
  99. digitalWrite(DisplayDI,LOW);
  100. digitalWrite(DisplayCLK,HIGH);
  101. //Debug Serial Init
  102. //Serial.begin(9600);
  103. //Write Zeroes to the Display
  104. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 00000001); //A 1 to start the display.
  105. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[0]); //Shift out a zero on digit 1.
  106. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digitsperiod[0]); //Shift out a zero with trailing period to digit 2.
  107. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[0]); //Shift out a zero to digit 3.
  108. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 11000000); //Bit 25 & 26 Enable (Power LED's) Only, Pin 4 & 5
  109. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 00000000); //Don't need a whole byte, but this clocks in the last bits needed to get the display to auto latch (33, 34, 35) and 5 extra zeroes.
  110. //Starting Power-Up Routine!
  111. digitalWrite(powerup, LOW);
  112. delay(250);
  113. digitalWrite(powerup, HIGH);
  114. }
  115.  
  116. // Here's our main loop.
  117. void loop() {
  118. //Holding on to Current Time
  119. currentMillis = millis();
  120. //Find out what the scanswitch is up to.
  121. int readscan = digitalRead(scanswitch);
  122. //Figure out what's going on @ the MODE Pot.
  123. PotVal = analogRead(modepot);
  124. //Serial.println(PotVal); //Serial Output for Debug
  125. //Checking the SCAN switch first.
  126. if (readscan == HIGH) {
  127. digitalWrite(motordome,LOW);
  128. digitalWrite(scanning,HIGH);
  129. digitalWrite(debugled,LOW);
  130. EarsEnable = 0;
  131. }
  132. else if ((readscan == LOW) && (PotVal >= 512)) {
  133. digitalWrite(motordome,HIGH);
  134. digitalWrite(scanning,LOW);
  135. digitalWrite(debugled,HIGH);
  136. EarsEnable = 1;
  137. }
  138. else if ((readscan == LOW) && (PotVal <= 511)) {
  139. EarsEnable = 1;
  140. digitalWrite(debugled,HIGH);
  141. digitalWrite(debugled,LOW);
  142. digitalWrite(debugled,HIGH);
  143. }
  144.  
  145. //Now the rolling ear LED's.
  146. //LED Position 1 (Closest to Body)
  147. if ((EarLed1 == LOW) && (currentMillis - previousMillis1 >= EarOnTime)) //If ON (LOW) Beyond Time Limit
  148. {
  149. EarLed1 = HIGH; //Turn it OFF
  150. previousMillis1 = currentMillis;
  151. digitalWrite(ear1, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
  152. digitalWrite(ear11, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
  153. }
  154. else if ((EarLed1 == HIGH) && (currentMillis - previousMillis1 >= EarOffTime) && (EarsEnable == 1)) //If OFF (High) Beyond Time Limit AND Ear LED's are enabled by the ScanSwitch
  155. {
  156. EarLed1 = LOW; //Turn it ON
  157. previousMillis1 = currentMillis;
  158. digitalWrite(ear1, LOW); //SINK the LED to turn it ON
  159. digitalWrite(ear11, LOW); //SINK the LED to turn it ON
  160. }
  161. //LED Position 2 (Middle of Stalk)
  162. if ((EarLed2 == LOW) && (currentMillis - previousMillis2 >= EarOnTime)) //If ON (LOW) Beyond Time Limit
  163. {
  164. EarLed2 = HIGH; //Turn it OFF
  165. previousMillis2 = currentMillis;
  166. digitalWrite(ear2, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
  167. digitalWrite(ear22, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
  168. }
  169. else if ((EarLed2 == HIGH) && (EarLed1 == HIGH) && (currentMillis - previousMillis2 >= EarOffTime) && (EarsEnable == 1)) //If OFF (High) Beyond Time Limit AND Ear LED's are enabled by the ScanSwitch
  170. {
  171. EarLed2 = LOW; //Turn it ON
  172. previousMillis2 = currentMillis;
  173. digitalWrite(ear2, LOW); //SINK the LED to turn it ON
  174. digitalWrite(ear22, LOW); //SINK the LED to turn it ON
  175. }
  176. //LED Position 3 (Furthest from Body)
  177. if ((EarLed3 == LOW) && (currentMillis - previousMillis3 >= EarOnTime)) //If ON (LOW) Beyond Time Limit
  178. {
  179. EarLed3 = HIGH; //Turn it OFF
  180. previousMillis3 = currentMillis;
  181. digitalWrite(ear3, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
  182. digitalWrite(ear33, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
  183. }
  184. else if ((EarLed3 == HIGH) && (EarLed2 == HIGH) && (EarLed1 == HIGH) && (currentMillis - previousMillis3 >= EarOffTime) && (EarsEnable == 1)) //If OFF (High) Beyond Time Limit AND Ear LED's are enabled by the ScanSwitch
  185. {
  186. EarLed3 = LOW; //Turn it ON
  187. previousMillis3 = currentMillis;
  188. digitalWrite(ear3, LOW); //SINK the LED to turn it ON
  189. digitalWrite(ear33, LOW); //SINK the LED to turn it ON
  190. }
  191. //Servo sweeping routine. If you need to edit thr throw, check the Open Ears comment ~11 lines down.
  192. if ((EarsOpen == 1) && (currentMillis - previousMillis4 >= 1200)) //If Ears Open Beyond Time Limit
  193. {
  194. EarsOpen = 0; //Ears no longer open.
  195. previousMillis4 = currentMillis; //Update Timer
  196. servo1.slowmove(90,24); //Close Ears at Quarter Speed
  197. }
  198. else if ((EarsOpen == 0) && (currentMillis - previousMillis4 >= 1200) && (EarsEnable == 1)) //If Closed Beyond Time Limit AND Ears are Enabled by ScanSwitch
  199. {
  200. EarsOpen = 1; //Ears are open.
  201. previousMillis4 = currentMillis; //Update Timer
  202. servo1.slowmove(40,24); //Open Ears at Quarter Speed
  203. }
  204. //Display Data Update Routine. Egon, give me strength.
  205. if ((EarsEnable == 1) && (currentMillis - previousMillis5 >= DisplayTime)) //If Ears Enabled (Convenient Variable) and we haven't updated the display in a while.
  206. {
  207. previousMillis5 = currentMillis; //Update Timer
  208. RNum1 = random(0,9); //Update Digit 1 Random Number
  209. RNum2 = random(0,9); //Digit 2
  210. RNum3 = random(0,9); //Digit 3
  211. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 00000001); //A 1 to start the display.
  212. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[RNum1]); //Shift out random digit 1.
  213. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digitsperiod[RNum2]); //Shift out random digit 2 with a period trailing.
  214. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[RNum3]); //Shift out random digit 3.
  215. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 11000000); //Bit 25 & 26 Enable (Power LED's) Only, Pin 4 & 5
  216. shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 00000000); //Don't need a whole byte, but this clocks in the last bits needed to get the display to auto latch (33, 34, 35) and 5 extra zeroes.
  217. }
  218. }
  219.  
  220. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
  221. {
  222. uint8_t i;
  223.  
  224. for (i = 0; i < 8; i++) {
  225. if (bitOrder == LSBFIRST){
  226. digitalWrite(dataPin, !!(val & (1 << i)));
  227. }
  228. else {
  229. digitalWrite(dataPin, !!(val & (1 << (7 - i))));
  230. }
  231. digitalWrite(clockPin, HIGH);
  232. delayMicroseconds(100);
  233. digitalWrite(clockPin, LOW);
  234. delayMicroseconds(100);
  235. }
  236. }
RAW Paste Data