Hi, Everyone!
Here is another Arduino sketch. This time for
TMP Starfleet Starship Photon Torpedo Effect - Two LEDs.
First, if you need to install an Arduino board and the Arduino Software (IDE), you can check out my previous post for a guide:
http://scifimodelaction.com/sfmaforum/index.php?topic=4793.msg64348#msg64348.
This Arduino sketch will have the Nano control two LEDs. When a momentary switch is pressed and released, the corresponding LED ramps up to a certain level and then a brighter flash burst occurs just a bit afterwards. When the momentary switch is held down, the LED effect loops until the switch is released.
The code allows one to change both the ramp up increment steps and the length of the burst rate.
This is intended for any
TMP-era starship. However, it can also be used for anything that requires this kind of effect. I posted a demo video on YouTube:
https://youtu.be/ETzkUzYfVHw.
Now onto programming the attached board...
1. Copy the following code:
/*
TMP Starfleet Starship Photon Torpedo Effect - Two LEDs:
- LED ramps up to a certain level and then a brighter flash burst
moments later.
- This particular version of the sketch supports two LEDs and two
momentary switches (one per LED).
- The momentary switches can use digital pins, but the LEDs need to
be on PWM pins.
- 1 second = 1000 milliseconds
- Based on original Arduino sketch by 'STDummy'
https://forum.arduino.cc/index.php?topic=353727.msg2439994#msg2439994 .
*/
// switchCall allocations
const byte switchPin = 2; // Momentary switch #1 on pin 2 (digital)
boolean switchFlag = false; // switch not triggered
byte oldSwitchState = HIGH; // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10; // milliseconds debounce setting
unsigned long switchPressTime; // when the switch last changed state
boolean reader = true; // switch blocker (switch routine not to be called during led flash)
const byte switchPin2 = 4; // Momentary switch #2 on pin 4 (digital)
boolean switchFlag2 = false; // switch not triggered
byte oldSwitchState2 = HIGH; // assume switch open because of pull-up resistor
const unsigned long debounceTime2 = 10; // milliseconds debounce setting
unsigned long switchPressTime2; // when the switch last changed state
boolean reader2 = true; // switch blocker (switch routine not to be called during led flash)
// rampUp allocations
const int ledPin = 3; // LED #1 on pin 3 (PWM)
boolean activate = false; // ramp up not active
unsigned long currentTime; // for tracking time
unsigned long loopTime; // during ramp up
int brightness = 0; // brightness during ramp up
int fadeAmount = 1; // increments to ramp up by
const int ledPin2 = 5; // LED #2 on pin 5 (PWM)
boolean activate2 = false; // ramp up not active
unsigned long currentTime2; // for tracking time
unsigned long loopTime2; // during ramp up
int brightness2 = 0; // brightness during ramp up
int fadeAmount2 = 1; // increments to ramp up by
// flashLed allocations
const int interval = 150; // 150ms flash time
static unsigned long previousMillis; // flash time counter
static boolean firstRun = true; // start of flash process
const int interval2 = 150; // 150ms flash time
static unsigned long previousMillis2; // flash time counter
static boolean firstRun2 = true; // start of flash process
void setup ()
{
pinMode (switchPin, INPUT_PULLUP);
pinMode (ledPin, OUTPUT);
pinMode (switchPin2, INPUT_PULLUP);
pinMode (ledPin2, OUTPUT);
} // end of setup
void loop ( )
{
switchCall ();
rampUp ();
flashLed ();
switchCall2 ();
rampUp2 ();
flashLed2 ();
} // end of loop
void flashLed ()
{
unsigned long currentMillis = millis();
if (firstRun == true) { // If start of flash
previousMillis = currentMillis; // advance timer
firstRun = false; // flash now in progress
reader = false; // set switch blocking flag
}
if (currentMillis - previousMillis >= interval) { // If led has been HIGH for enough time
previousMillis = currentMillis; // advance timer
digitalWrite(ledPin, LOW); // switch off led
firstRun = true; // reset flash process indicator
brightness = 0; // reset ready for next rampUp
reader = true; // unblock switch
}
}
void rampUp ()
{
if (switchFlag == true) // Has switch been pressed?
activate = true; { // ramp up underway
while (activate == true) {
currentTime = millis();
if (currentTime >= (loopTime + 20)) { // while rampup underway, increment time
analogWrite (ledPin, brightness); // write current brightness
brightness = (brightness + fadeAmount); // fadeup the led
if (brightness < 50)
activate = true; // still below 50?
else {
activate = false; // above 50 so reset flag and switch led on full
digitalWrite (ledPin, HIGH);
}
loopTime = currentTime; // update time
}
}
}
activate = false; // process complete so reset flag
}
void switchCall ()
{
if (reader == true) { // Has previous FlashLed activation completed?
byte switchState = digitalRead (switchPin); // see if switch is open or closed
if (switchState != oldSwitchState) { // has it changed since last time?
if (millis () - switchPressTime >= debounceTime) { // debounce
switchPressTime = millis (); // when we closed the switch
oldSwitchState = switchState; // remember for next time
if (switchState == LOW)
switchFlag = true; // end if switchState is LOW
else
switchFlag = false; // end if switchState is HIGH
} // end if debounce time up
} // end of state change
} // end of function
}
void flashLed2 ()
{
unsigned long currentMillis2 = millis();
if (firstRun2 == true) { // If start of flash
previousMillis2 = currentMillis2; // advance timer
firstRun2 = false; // flash now in progress
reader2 = false; // set switch blocking flag
}
if (currentMillis2 - previousMillis2 >= interval2) { // If led has been HIGH for enough time
previousMillis2 = currentMillis2; // advance timer
digitalWrite(ledPin2, LOW); // switch off led
firstRun2 = true; // reset flash process indicator
brightness2 = 0; // reset ready for next rampUp
reader2 = true; // unblock switch
}
}
void rampUp2 ()
{
if (switchFlag2 == true) // Has switch been pressed?
activate2 = true; { // ramp up underway
while (activate2 == true) {
currentTime2 = millis();
if (currentTime2 >= (loopTime2 + 20)) { // while rampup underway, increment time
analogWrite (ledPin2, brightness2); // write current brightness
brightness2 = (brightness2 + fadeAmount2); // fadeup the led
if (brightness2 < 50)
activate2 = true; // still below 50?
else {
activate2 = false; // above 50 so reset flag and switch led on full
digitalWrite (ledPin2, HIGH);
}
loopTime2 = currentTime2; // update time
}
}
}
activate2 = false; // process complete so reset flag
}
void switchCall2 ()
{
if (reader2 == true) { // Has previous FlashLed activation completed?
byte switchState2 = digitalRead (switchPin2); // see if switch is open or closed
if (switchState2 != oldSwitchState2) { // has it changed since last time?
if (millis () - switchPressTime2 >= debounceTime2) { // debounce
switchPressTime2 = millis (); // when we closed the switch
oldSwitchState2 = switchState2; // remember for next time
if (switchState2 == LOW)
switchFlag2 = true; // end if switchState is LOW
else
switchFlag2 = false; // end if switchState is HIGH
} // end if debounce time up
} // end of state change
} // end of function
}
2. Replace the existing code in the Arduino software window with the copied code.
3. Save the sketch. NOTE: for your reference, saved Arduino sketches are located in the C:\Users\<Windows user name>\Documents\Arduino folder.
4. Click the 'Verify' check-mark icon to allow the Arduino software to compile the code and check for issues. When finished, the software status bar will say 'Done compiling.'
5. Click the 'Upload' right-arrow icon to allow the Arduino software to program the Nano. When finished, the software status bar will say 'Done uploading.'
And that's it! Easy!
Here is a pin diagram picture that will show where to make all your connections:

Don't forget to use resistors for your setup! You should be able to use a 9- or 12-volt power supply. For the purposes of my testing, I used a 5-volt "power bank" external rechargeable battery.
