Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <VarSpeedServo.h>
- // Arduino-Based Gigameter Prop - Code Version Public 1.2
- //
- // Code written by Josh Teasdale - WindDrake @ GBFans.com Forums
- //
- // This sketch requires VarSpeedServo.h library!
- // https://github.com/netlabtoolkit/VarSpeedServo
- //
- // Program released under Creative Commons - Attribution - Share Alike License
- //
- // Default pin setup is for an Arduino Nano. Do not use a Micro as it cannot sink as much current as the Nano.
- //
- // This sketch allows for Ears Only and Ears & Dome mode operation via the normally unused pot on the left side of the handle.
- // 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.
- //
- // You may have to change the throw angle on the servo. Look for the Servo Sweeping Routine and follow the comments.
- //
- // Used Digital IO: 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2
- // Used Analog IO: A0, A4, A5
- //
- //Analog Inputs
- int modepot = A0;
- //Digital Outputs
- int debugled = 13;
- int powerup = 12;
- int scanning = 11;
- int motordome = 10;
- int ear1 = 9;
- int ear2 = 8;
- int ear3 = 7;
- int ear11 = 4;
- int ear22 = 3;
- int ear33 = 2;
- int DisplayDI = A4;
- int DisplayCLK = A5;
- //Digital Inputs
- int scanswitch = 5; //SCAN Switch to Pin 5
- //Servo Setup
- VarSpeedServo servo1; //Servo PWM Output
- //Timer Variables
- unsigned long previousMillis1 = 0; //Ear LED LastUpdated Variable
- unsigned long previousMillis2 = 0; //Ear LED LastUpdated Variable
- unsigned long previousMillis3 = 0; //Ear LED LastUpdated Variable
- unsigned long previousMillis4 = 0; //Ear Sweep LastUpdated Variable
- unsigned long previousMillis5 = 0; //Display LastUpdated Variable
- unsigned long currentMillis = 0; //Current Time
- //Ear LED Cycle Times
- long EarOnTime = 333;
- long EarOffTime = 666;
- //Ear State Variables
- int EarLed1 = HIGH;
- int EarLed2 = HIGH;
- int EarLed3 = HIGH;
- int EarsEnable = 0; //Ears Enabled or Not
- int EarsOpen = 0; //Ears Open (1) or Shut (0)
- //Motor Relay Variable
- int MotorEnable = 0; //Motor Relay
- //Mode Pot Value Variable
- int PotVal = 0; //Mode Pot value
- //Display Update Timer
- long DisplayTime = 1100;
- //Display Digits Conversion Table
- byte dec_digits[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b00111110,0b11100000,0b11111110,0b11100110};
- byte dec_digitsperiod[] = {0b11111101,0b01100001,0b11011011,0b11110011,0b01100111,0b00111111,0b11100001,0b11111111,0b11100111};
- unsigned int RNum1 = 0;
- unsigned int RNum2 = 0;
- unsigned int RNum3 = 0;
- // Startup Routine
- void setup() {
- // Initialize all pins!
- pinMode(modepot,INPUT);
- pinMode(scanswitch, INPUT_PULLUP);
- pinMode(powerup,OUTPUT);
- pinMode(scanning,OUTPUT);
- pinMode(motordome,OUTPUT);
- pinMode(ear1,OUTPUT);
- pinMode(ear2,OUTPUT);
- pinMode(ear3,OUTPUT);
- pinMode(ear11,OUTPUT);
- pinMode(ear22,OUTPUT);
- pinMode(ear33,OUTPUT);
- pinMode(DisplayDI,OUTPUT);
- pinMode(DisplayCLK,OUTPUT);
- //Setting up the Servo.
- servo1.attach(6);
- servo1.slowmove(90, 0);
- //Now setting all outputs to known state.
- digitalWrite(powerup,HIGH);
- digitalWrite(scanning,HIGH);
- digitalWrite(motordome,LOW);
- digitalWrite(ear1,HIGH);
- digitalWrite(ear2,HIGH);
- digitalWrite(ear3,HIGH);
- digitalWrite(ear11,HIGH);
- digitalWrite(ear22,HIGH);
- digitalWrite(ear33,HIGH);
- digitalWrite(DisplayDI,LOW);
- digitalWrite(DisplayCLK,HIGH);
- //Debug Serial Init
- //Serial.begin(9600);
- //Write Zeroes to the Display
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 00000001); //A 1 to start the display.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[0]); //Shift out a zero on digit 1.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digitsperiod[0]); //Shift out a zero with trailing period to digit 2.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[0]); //Shift out a zero to digit 3.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 11000000); //Bit 25 & 26 Enable (Power LED's) Only, Pin 4 & 5
- 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.
- //Starting Power-Up Routine!
- digitalWrite(powerup, LOW);
- delay(250);
- digitalWrite(powerup, HIGH);
- }
- // Here's our main loop.
- void loop() {
- //Holding on to Current Time
- currentMillis = millis();
- //Find out what the scanswitch is up to.
- int readscan = digitalRead(scanswitch);
- //Figure out what's going on @ the MODE Pot.
- PotVal = analogRead(modepot);
- //Serial.println(PotVal); //Serial Output for Debug
- //Checking the SCAN switch first.
- if (readscan == HIGH) {
- digitalWrite(motordome,LOW);
- digitalWrite(scanning,HIGH);
- digitalWrite(debugled,LOW);
- EarsEnable = 0;
- }
- else if ((readscan == LOW) && (PotVal >= 512)) {
- digitalWrite(motordome,HIGH);
- digitalWrite(scanning,LOW);
- digitalWrite(debugled,HIGH);
- EarsEnable = 1;
- }
- else if ((readscan == LOW) && (PotVal <= 511)) {
- EarsEnable = 1;
- digitalWrite(debugled,HIGH);
- digitalWrite(debugled,LOW);
- digitalWrite(debugled,HIGH);
- }
- //Now the rolling ear LED's.
- //LED Position 1 (Closest to Body)
- if ((EarLed1 == LOW) && (currentMillis - previousMillis1 >= EarOnTime)) //If ON (LOW) Beyond Time Limit
- {
- EarLed1 = HIGH; //Turn it OFF
- previousMillis1 = currentMillis;
- digitalWrite(ear1, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
- digitalWrite(ear11, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
- }
- else if ((EarLed1 == HIGH) && (currentMillis - previousMillis1 >= EarOffTime) && (EarsEnable == 1)) //If OFF (High) Beyond Time Limit AND Ear LED's are enabled by the ScanSwitch
- {
- EarLed1 = LOW; //Turn it ON
- previousMillis1 = currentMillis;
- digitalWrite(ear1, LOW); //SINK the LED to turn it ON
- digitalWrite(ear11, LOW); //SINK the LED to turn it ON
- }
- //LED Position 2 (Middle of Stalk)
- if ((EarLed2 == LOW) && (currentMillis - previousMillis2 >= EarOnTime)) //If ON (LOW) Beyond Time Limit
- {
- EarLed2 = HIGH; //Turn it OFF
- previousMillis2 = currentMillis;
- digitalWrite(ear2, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
- digitalWrite(ear22, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
- }
- 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
- {
- EarLed2 = LOW; //Turn it ON
- previousMillis2 = currentMillis;
- digitalWrite(ear2, LOW); //SINK the LED to turn it ON
- digitalWrite(ear22, LOW); //SINK the LED to turn it ON
- }
- //LED Position 3 (Furthest from Body)
- if ((EarLed3 == LOW) && (currentMillis - previousMillis3 >= EarOnTime)) //If ON (LOW) Beyond Time Limit
- {
- EarLed3 = HIGH; //Turn it OFF
- previousMillis3 = currentMillis;
- digitalWrite(ear3, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
- digitalWrite(ear33, HIGH); //We are running the LED's in SINK - Write HIGH to turn OFF
- }
- 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
- {
- EarLed3 = LOW; //Turn it ON
- previousMillis3 = currentMillis;
- digitalWrite(ear3, LOW); //SINK the LED to turn it ON
- digitalWrite(ear33, LOW); //SINK the LED to turn it ON
- }
- //Servo sweeping routine. If you need to edit thr throw, check the Open Ears comment ~11 lines down.
- if ((EarsOpen == 1) && (currentMillis - previousMillis4 >= 1200)) //If Ears Open Beyond Time Limit
- {
- EarsOpen = 0; //Ears no longer open.
- previousMillis4 = currentMillis; //Update Timer
- servo1.slowmove(90,24); //Close Ears at Quarter Speed
- }
- else if ((EarsOpen == 0) && (currentMillis - previousMillis4 >= 1200) && (EarsEnable == 1)) //If Closed Beyond Time Limit AND Ears are Enabled by ScanSwitch
- {
- EarsOpen = 1; //Ears are open.
- previousMillis4 = currentMillis; //Update Timer
- servo1.slowmove(40,24); //Open Ears at Quarter Speed
- }
- //Display Data Update Routine. Egon, give me strength.
- if ((EarsEnable == 1) && (currentMillis - previousMillis5 >= DisplayTime)) //If Ears Enabled (Convenient Variable) and we haven't updated the display in a while.
- {
- previousMillis5 = currentMillis; //Update Timer
- RNum1 = random(0,9); //Update Digit 1 Random Number
- RNum2 = random(0,9); //Digit 2
- RNum3 = random(0,9); //Digit 3
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 00000001); //A 1 to start the display.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[RNum1]); //Shift out random digit 1.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digitsperiod[RNum2]); //Shift out random digit 2 with a period trailing.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, dec_digits[RNum3]); //Shift out random digit 3.
- shiftOut(DisplayDI, DisplayCLK, MSBFIRST, 11000000); //Bit 25 & 26 Enable (Power LED's) Only, Pin 4 & 5
- 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.
- }
- }
- void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
- {
- uint8_t i;
- for (i = 0; i < 8; i++) {
- if (bitOrder == LSBFIRST){
- digitalWrite(dataPin, !!(val & (1 << i)));
- }
- else {
- digitalWrite(dataPin, !!(val & (1 << (7 - i))));
- }
- digitalWrite(clockPin, HIGH);
- delayMicroseconds(100);
- digitalWrite(clockPin, LOW);
- delayMicroseconds(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement