hi guys, i have to sets of code that i need to combine into one;
the first, this is the one that needs to be included into the second, its basically an led random breathing program. For those star trek fans out there, im using this for the bussard collectors on the nacelles.
heres the code
//random led breathing
#include <math.h> // mathematical functions
#define ledNumber 2 // number of leds used
int ledPins[ledNumber] = { 1, 0, }; // assign the PWM pins
float fadingInterval[ledNumber]; // fading time variable
int remainingIntervals[ledNumber];
int ledPin1 = 2;
int ledPin2 = 3;
//------------------------------------------------------------------------------
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
//Serial.begin(115200);
randomSeed(analogRead(0));
for (int i = 0; i < ledNumber; i++)
{
fadingInterval[i] = random(1000, 3000); // assign random fading time
remainingIntervals[i] = fadingInterval[i];
// Serial.print("remainingIntervals[i] "); Serial.println(remainingIntervals[i]);
}
}
//------------------------------------------------------------------------------
void loop() {
analogWrite(ledPin1, 135);
analogWrite(ledPin2, 135);
for ( int i = 0; i < ledNumber; i++)
{
float b = (exp(sin(remainingIntervals[i] / fadingInterval[i] * PI)) - 0.975) * 145.0;
//Serial.print("b "); Serial.println(b);
if (i == 1 || i == 5) // if led on pin 10 or 9
b = b * 2.0; // bright 0.2x less
//Serial.print("b2 "); Serial.println(b);
analogWrite(ledPins[i], b);
if (!--remainingIntervals[i]) {
fadingInterval[i] = random(1000, 3000);
remainingIntervals[i] = fadingInterval[i];
}
}
}
now heres the core code, everything in this code currently works exactly as it should, as you can see its gone through multiple revisions to get it cooperating and as condensed as possible, but its a nice bit of coding. it has 4 spare pwm pins available and the code to be integrated only needs 3 pins (only 2 are pwm)
here it is
// The Enterprise revision 2.48
#include <LedFader.h>
#include <LedFlasher.h>
// pin assignments
const byte StrobesPin = 13;
const byte NavigationPin = 12;
const byte DeflectorbluePin = 11; // PWM controls nacelles and impulse engines
const byte DeflectororangePin = 10; // PWM controls nacelles and impulse engines
//unused pin 9 pwm
const byte CabinPin = 8;
const byte ShuttlebayPin = 7;
//unused pin 6 pwm
//unused pin 5 pwm
const byte TorpedoPin = 4;
//unused pin 3 pwm
const byte RCSPin = 2;
// Faders pin min max millis on? stop?
LedFader deflectororangeFader (DeflectororangePin, 0, 40, 3000, false, true);
LedFader deflectorblueFader (DeflectorbluePin, 0, 40, 3000, false, true);
// Flashers pin off-time on-time on?
LedFlasher strobes (StrobesPin, 1700, 100, false);
LedFlasher navigation (NavigationPin, 3500, 100, false);
// states for the state machine
typedef enum
{
initialState,
wantCabin, // ALWAYS ON
wantRCS, // ALWAYS ON
wantStrobes, // ALWAYS ON
wantTorpedostartup, // ALWAYS ON
wantDeflectororangestartup,// Startup mode
wantShuttleBaystartup, // Startup mode
wantImpulseorangestartup, // Startup mode
wantNacellvioletstartup, // Startup mode
wantShuttleBayon, // Impulse mode
wantTorpedoon, // Impulse mode
wantShuttleBayoff, // Warp mode
wantTorpedooff, // Warp mode
wantImpulseMode,
wantWarpMode,
} states;
// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;
void setup ()
{
pinMode (CabinPin, OUTPUT);
pinMode (RCSPin, OUTPUT);
pinMode (ShuttlebayPin, OUTPUT);
pinMode (NavigationPin, OUTPUT);
pinMode (StrobesPin, OUTPUT);
pinMode (TorpedoPin, OUTPUT);
// set up faders, flashers
deflectororangeFader.begin ();
deflectorblueFader.begin ();
strobes.begin ();
navigation.begin ();
} // end of setup
void doStateChange ()
{
lastStateChange = millis (); // when we last changed states
timeInThisState = 1000; // default one second between states
switch (state)
{
case initialState:
state = wantCabin;
break;
case wantCabin:
digitalWrite (CabinPin, HIGH);
state = wantRCS;
break;
case wantRCS:
digitalWrite (RCSPin, HIGH);
state = wantDeflectororangestartup;
break;
case wantDeflectororangestartup:
deflectororangeFader.on();
state = wantShuttleBaystartup;
break;
//doors open here
case wantShuttleBaystartup:
digitalWrite (ShuttlebayPin, HIGH);
state = wantStrobes;
break;
case wantStrobes:
strobes.on();
navigation.on();
state = wantTorpedostartup;
break;
case wantTorpedostartup:
digitalWrite (TorpedoPin, HIGH);
state = wantImpulseMode;
timeInThisState = 30000;
break;
//impulse mode
case wantImpulseMode:
deflectorblueFader.off();
deflectororangeFader.on();
state = wantShuttleBayon;
break;
//doors need to open here
case wantShuttleBayon:
digitalWrite (ShuttlebayPin, HIGH);
state = wantTorpedoon;
break;
case wantTorpedoon:
digitalWrite (TorpedoPin, HIGH);
state = wantWarpMode;
timeInThisState = 30000;
break;
//warp mode
case wantWarpMode:
deflectororangeFader.off();
deflectorblueFader.on();
state = wantShuttleBayoff;
break;
//doors need to close here
case wantShuttleBayoff:
digitalWrite(ShuttlebayPin, LOW);
state = wantTorpedooff;
break;
case wantTorpedooff:
digitalWrite(TorpedoPin, LOW);
state = wantImpulseMode;
timeInThisState = 30000;
// what next?
break;
} // end of switch on state
} // end of doStateChange
void loop ()
{
if (millis () - lastStateChange >= timeInThisState)
doStateChange ();
// update faders, flashers
deflectororangeFader.update ();
deflectorblueFader.update ();
navigation.update ();
strobes.update ();
// other stuff here like testing switches
} // end of loop
now i have no idea where to begin combining the two, essentially, the bussards (led breathing program) needs to activate at the same time as the nacelles in the startup section of the code and continue indefinatly.
i could use your guys expertise here and really appreciate the help