Also, i assume once i know how to control everything, i can "duplicate" that, change the setup and loop names add "setup () { setup1 setup 2}" and same for loop (not quite right i know) and use the same code essentialy for next sequence?
Not really. Just add more states to the state machine. Think of a state as a point in time, a line in your list you published about 10 pages back of what turns on when.
At a particular state transition you:
- Turn on stuff
- Turn off other stuff
You can always switch states (jump) by doing something like:
state = wantImpulsered;
However if you jump around like that you may need to make the state you choose turn things off which might be on.
Think of a state engine as a recipe in a cooking book. It might read:
- Put roast on, wait 10 minutes.
- Put potatoes on, wait 5 minutes.
- Set table.
- Put carrots on, wait 3 minutes.
- Make gravy.
- Take roast out to rest.
You glance at the clock (ie. you read millis) and see if it is time for the next state. Nothing complicated about it. If you want to make another roast tomorrow you go back to the first state. In between states (while stuff is cooking) you can do other things like watch the TV.
My only concern with that is the impulse mode and warp mode have to cycle through each other in repetition until the shutdown sequence is triggers with a push button.
so its the impulse and warp loop that will give me trouble.
However, I will add in the other stages just to see how things run their course.
thank you again! Nick, i've given you credit for the code on my youtube page, im sorry its not much but i do think its important to give you credit for it!
The states can go backwards you know. So after state 50 the "next" state can be 10. So that way it loops. Just make sure that state 10 turns off anything that might be left over after state 50.
Um, not sure I follow entirely.
The only other thing is I'm getting errors because of duplicate uses. Is there an easy way around that without having lists of defines and so on longer than the operating code?
guessing not then?
Nick what did you mean about the state 50 and so on?
secretreeve:
The only other thing is I'm getting errors because of duplicate uses. Is there an easy way around that without having lists of defines and so on longer than the operating code?
What do you mean by defines? My code didn't have a single one, so I don't see where "lists of defines" comes from.
secretreeve:
Nick what did you mean about the state 50 and so on?
If you have 50 states the next state doesn't have to be the 51st. one. States can loop.
For example, if your daily routine is:
-
- Wake up
-
- Get out of bed
-
- Have breakfast
-
- Go to work
-
- Have dinner
-
- Go to bed
-
- Go to state 1.
The 7th state in my example "goes backwards" (loops) so next day you do the same thing.
wantimpulsered
if used more than once, would return a duplication error when called for twice, once to turn on and once to turn off. obviously at different stages in the "list of events"
Time for you to start thinking about this. You need two states, right? One to turn impulsered on, and one to turn impulsered off. Any ideas for their names?
I had tried that, But for some reason the newer entry looped the fade.
I'll take another look at it tomorow though and see what happens.
The only other thing is, I know you said about the states, but still not sure on "down fading".
Also, the "startup sequence" stage is knocked out, so for the impulse mode sequence, would i want to put that where the note says //what next?
or after the break;
I'm thinking it would be where "whats next?" is. but just want to get that confirmed.
Also, progress report with code!
// The Enterprise
// Author: Nick Gammon
// Date: 27 December 2012
#include <LedFader.h>
#include <LedFlasher.h>
// pin assignments
const byte StrobesPin = 13;
const byte NavigationPin = 12;
const byte DeflectorbluePin = 11; // PWM
const byte DeflectororangePin = 10; // PWM
const byte NacellsbluePin = 9; // PWM
const byte ShuttlebayPin = 8;
const byte DoorsPin = 7;
const byte NacellsvioletPin = 6; // PWM
const byte ImpulseorangePin = 5; // PWM
const byte TorpedoPin = 4;
const byte ImpulseredPin = 3; // PWM
const byte RCSPin = 2;
const byte CabinPin = 1;
// Faders
// pin min max millis on? stop?
LedFader impulseredFader (ImpulseredPin, 0, 40, 3000, false, true);
LedFader impulseorangeFader (ImpulseorangePin, 0, 40, 3000, false, true);
LedFader deflectororangeFader (DeflectororangePin, 0, 40, 3000, false, true);
LedFader deflectorblueFader (DeflectorbluePin, 0, 40, 3000, false, true);
LedFader nacellsvioletFader (NacellsvioletPin, 0, 40, 3000, false, true);
LedFader nacellsblueFader (NacellsbluePin, 0, 40, 3000, false, true);
// Flashers
// pin off-time on-time on?
LedFlasher strobes (StrobesPin, 8000, 100, false);
LedFlasher navigation (NavigationPin, 2000, 500, false);
// states for the state machine
typedef enum
{
initialState,
wantCabin,
wantRCS,
wantDeflectororangeon, // impulse mode
wantDeflectorblueon, // warp mode
wantDeflectororangeoff, // warp mode
wantDeflectorblueoff, // impulse mode
wantNacellsvioleton, // impulse mode
wantNacellsblueon, // warp mode
wantNacellsvioletoff, // warp mode
wantNacellsblueoff, // impulse mode
wantImpulseredon, // impulse mode
wantImpulseorangeon, // warp mode
wantImpulseredoff, // warp mode
wantImpulseorangeoff, // impulse mode
wantShuttleBayopen // open in impulse
wantShuttleBayclosed // closed in warp
wantNavigation, // ALWAYS ON
wantStrobes, // ALWAYS ON
wantTorpedoon, // impulse mode
wantTorpedooff // warp mode
// more states here
} 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);
pinMode (NavigationPin, OUTPUT);
// set up faders, flashers
impulseredFader.begin ();
impulseredFader.begin ();
deflectororangeFader.begin ();
deflectorblueFader.begin ();
nacellsvioletFader.begin ();
nacellsblueFader.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 = wantDeflectororangeon;
break;
case wantDeflectororangeon:
deflectororangeFader.on ();
state = wantDeflectorblueon;
break;
case wantDeflectorblueon:
deflectorblueFader.off();
state = wantNacellsvioleton;
break;
case wantNacellsvioleton:
nacellsvioletFader.on();
state = wantImpulseorangeon;
break;
case wantImpulseorangeon:
impulseorangeFader.on();
state = wantShuttleBay;
break;
case wantShuttleBay:
digitalWrite (ShuttlebayPin, HIGH);
state = wantStrobes;
break;
case wantStrobes:
strobes.on();
state = wantNavigation;
break;
case wantNavigation:
navigation.on();
state = wantTorpedo;
break;
case wantTorpedo:
digitalWrite (TorpedoPin, HIGH);
timeInThisState = 5000; // no rush
// what next?
break;
} // end of switch on state
} // end of doStateChange
void loop ()
{
if (millis () - lastStateChange >= timeInThisState)
doStateChange ();
// update faders, flashers
impulseorangeFader.update ();
impulseredFader.update ();
deflectororangeFader.update ();
deflectorblueFader.update ();
nacellsvioletFader.update ();
nacellsblueFader.update ();
navigation.update ();
strobes.update ();
// other stuff here like testing switches
} // end of loop
The notes in the want list are there to help me keep organised, so pay them little mind.
Okay, here is the (almost) complete code!
It has all 3 sequences in there for the startup mode, impulse mode and warp mode.
// The Enterprise
// Author: Nick Gammon
// Date: 27 December 2012
#include <LedFader.h>
#include <LedFlasher.h>
// pin assignments
const byte StrobesPin = 13;
const byte NavigationPin = 12;
const byte DeflectorbluePin = 11; // PWM
const byte DeflectororangePin = 10; // PWM
const byte NacellsbluePin = 9; // PWM
const byte ShuttlebayPin = 8;
const byte DoorsPin = 7;
const byte NacellsvioletPin = 6; // PWM
const byte ImpulseorangePin = 5; // PWM
const byte TorpedoPin = 4;
const byte ImpulseredPin = 3; // PWM
const byte RCSPin = 2;
const byte CabinPin = 1;
// Faders
// pin min max millis on? stop?
LedFader impulseredFader (ImpulseredPin, 0, 40, 3000, false, true);
LedFader impulseorangeFader (ImpulseorangePin, 0, 40, 3000, false, true);
LedFader deflectororangeFader (DeflectororangePin, 0, 40, 3000, false, true);
LedFader deflectorblueFader (DeflectorbluePin, 0, 40, 3000, false, true);
LedFader nacellsvioletFader (NacellsvioletPin, 0, 40, 3000, false, true);
LedFader nacellsblueFader (NacellsbluePin, 0, 40, 3000, false, true);
// Flashers
// pin off-time on-time on?
LedFlasher strobes (StrobesPin, 8000, 100, false);
LedFlasher navigation (NavigationPin, 2000, 500, false);
// states for the state machine
typedef enum
{
initialState,
wantCabin,
wantRCS,
wantDeflectororangestartup,// startup mode
wantDeflectororangeon, // impulse mode
wantDeflectorblueon, // warp mode
wantDeflectororangeoff, // warp mode
wantDeflectorblueoff, // impulse mode
wantNacellvioletstartup, // startup mode
wantNacellsvioleton, // impulse mode
wantNacellsblueon, // warp mode
wantNacellsvioletoff, // warp mode
wantNacellsblueoff, // impulse mode
wantImpulseredon, // impulse mode
wantImpulseorangeon, // warp mode
wantImpulseredoff, // warp mode
wantImpulseorangeoff, // impulse mode
wantImpulseorangestartup, // startup mode
wantShuttleBayon, // impulse mode
wantShuttleBaystartup, // startup mode
wantShuttleBayoff , // warp mode
wantNavigation, // ALWAYS ON
wantStrobes, // ALWAYS ON
wantTorpedoon, // impulse mode
wantTorpedooff // warp mode
// more states here
} 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);
pinMode (NavigationPin, OUTPUT);
// set up faders, flashers
impulseredFader.begin ();
impulseredFader.begin ();
deflectororangeFader.begin ();
deflectorblueFader.begin ();
nacellsvioletFader.begin ();
nacellsblueFader.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 = wantNacellvioletstartup;
break;
case wantNacellvioletstartup:
nacellsvioletFader.on();
state = wantImpulseorangestartup;
break;
case wantImpulseorangestartup:
impulseorangeFader.on();
state = wantShuttleBaystartup;
break;
//doors open here
case wantShuttleBaystartup:
digitalWrite (ShuttlebayPin, HIGH);
state = wantStrobes;
break;
case wantStrobes:
strobes.on();
state = wantNavigation;
break;
case wantNavigation:
navigation.on();
state = wantTorpedoon;
break;
case wantTorpedoon:
digitalWrite (TorpedoPin, HIGH);
timeInThisState = 20000; // no rush 10minutes, "600,000"
//impulse mode
case wantImpulseorangeoff:
impulseorangeFader.off();
state = wantImpulseredon;
break;
case wantImpulseredon:
impulseredFader.on();
state = wantDeflectororangeon;
break;
case wantDeflectororangeon:
deflectororangeFader.on();
state = wantNacellsvioleton;
break;
case wantNacellsvioleton:
nacellsvioletFader.on();
state = wantShuttleBayon;
break;
//doors need to open here
case wantShuttleBayon:
digitalWrite (ShuttlebayPin, HIGH);
timeInThisState = 20000; //10 minutes, "600,000"
//warp mode
case wantImpulseredoff:
impulseredFader.off();
state = wantImpulseorangeon;
break;
case wantImpulseorangeon:
impulseorangeFader.on();
state = wantDeflectororangeoff;
break;
case wantDeflectororangeoff:
deflectororangeFader.off();
state = wantDeflectorblueon;
break;
case wantDeflectorblueon:
deflectorblueFader.on();
state = wantNacellsvioletoff;
break;
case wantNacellsvioletoff:
nacellsvioletFader.off();
state = wantNacellsblueon;
break;
case wantNacellsblueon:
nacellsblueFader.on();
state = wantShuttleBayoff;
break;
case wantShuttleBayoff:
digitalWrite(ShuttlebayPin, LOW);
timeInThisState = 20000;
// what next?
break;
} // end of switch on state
} // end of doStateChange
void loop ()
{
if (millis () - lastStateChange >= timeInThisState)
doStateChange ();
// update faders, flashers
impulseorangeFader.update ();
impulseredFader.update ();
deflectororangeFader.update ();
deflectorblueFader.update ();
nacellsvioletFader.update ();
nacellsblueFader.update ();
navigation.update ();
strobes.update ();
// other stuff here like testing switches
} // end of loop
Problem is, theres a bug in the impulse lights (the 3rd and 4th from the right in the below video).
video: http://www.youtube.com/watch?v=f-trRl2oFaw&feature=youtu.be
As you can see, its not staying on when it is supposed to. its the first blue led from the right. "impulseorange". It is MEANT to stay on until later on in the sequence when "impulsered" comes on and not turn off when the white one next to it comes on. Also i cant work out why the red (impulsered) is turning off as that is meant to stay on until the blue one (impulseorange) comes back on.
Can anyone see anything in the code which would cause it to be doing this?
Also, I still need a little more help in "down fading" the leds instead of them just "turning off" and getting impulse mode and warp mode to run in loops.
note: the led right in the middle at the lowest point is not meant to come on at any time. so thats not a problem.
So i guess I'll have to try and work this out.
Can someone tell me the output specs of the pins please? i.e voltage and current.
Can someone tell me the output specs of the pins please? i.e voltage and current
HIGH voltage is supply minus a tiny bit, LOW is ground plus a tiny bit.
Current is an ABSOLUTE maximum of 40mA per pin, with a per-package limit of something like 200mA.
Or you could read the spec for your board.
Well I would do if it had a specs sheet with in, but instead just had some legal stuff in it.
I can't remember what board you're using, but if you follow the links here you should find the specs for whatever you have.
Thanks AWOL. Its the UNO i think. It's got uno printed on it anyway lol.
I've deleted four or five off-topic, pointless recent replies to this thread.
Please, you know who you are - just don't.
Appreciated AWOL.
I dont suppose you have any ideas on the interaction between the torpedo pin (4) and the impulse pins? (3 and 5)
No, I'm afraid I don't and short of building your hardware, I don't suppose I'll be finding out any time soon.