Guest User

Arduino Gigameter v1.0

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