Last active
September 14, 2020 03:37
-
-
Save ledlogic/534b7ff62fdd9d55521bce5918fe3565 to your computer and use it in GitHub Desktop.
Arduino script for lighting of a Klingon D7 model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Klingon-D7 | |
1) Light up impulse lights (pin 11) | |
2) Detect button press (pin 2) | |
3) Fire torpedo (pin 9) | |
*/ | |
int PIN_TORPEDO_BUTTON = 2; | |
int PIN_TORPEDO = 9; | |
int PIN_IMPULSE = 11; | |
int TORPEDO_MODE = 2; // 0 - off, 1 - pressing, 2 - firing | |
float torpedoRatio = 0.1; | |
void setup() { | |
pinMode(PIN_TORPEDO, OUTPUT); | |
pinMode(PIN_IMPULSE, OUTPUT); | |
Serial.begin(9600); | |
// Impulse engines are always on | |
digitalWrite(PIN_IMPULSE, HIGH); | |
} | |
void loop() { | |
renderTorpedo(); | |
updateTorpedo(); | |
// delay 40 ms | |
delay(40); | |
} | |
// light up the d7 photon torpedo | |
void renderTorpedo() { | |
if (TORPEDO_MODE == 0) { | |
analogWrite(PIN_TORPEDO, 0); | |
} else { | |
// use exponential brightness, scaled to 255. | |
// torpedoRatio must be between 0.0 and 1.0 | |
// 255 is maximum PWM output | |
float torpedoLevel = 255.0 * pow(torpedoRatio, 3.0); | |
int pinLevel = (int) torpedoLevel; | |
analogWrite(PIN_TORPEDO, pinLevel); | |
} | |
} | |
void updateTorpedo() { | |
// TORPEDO_MODE 2 is firing | |
if (TORPEDO_MODE == 2) { | |
// d7 photon torpedo - 20 steps per cycle. | |
// 20 steps at 20 ms, one cycle is half a second? | |
torpedoRatio += 0.05; | |
if (torpedoRatio > 1.0) { | |
torpedoRatio = 0.0; | |
TORPEDO_MODE = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment