fustrating code

Thread Starter

c.marsh

Joined May 16, 2009
100
i dont get this,

it should be working so this is probably something stupidly simple im missing.

the code below all works fine except for the impulse state and strobe2 stage.

everything else works fine.

i've tested with different leds, tested on different pin assignments, tested with a stand-alone digitalwrite high test and stand alone fade test and those both work without issues on the 2 pins in question (2 and 11)

so i dont understand why they are failing to operate in the code

Rich (BB code):
// The Voyager

#include <LedFader.h>
#include <LedFlasher.h>

// pin assignments PMW 3,5,6,9,10,11
const byte StrobesPin = 1;      //FLASH
const byte Strobes2Pin = 2;     //FLASH
const byte NavigationPin = 7;   //FLASH
const byte DeflectorPin = 6;    //FADE
const byte NacellesPin = 9;     //FADE
const byte ShuttlebayPin = 10;  //FADE
const byte FloodlightPin = 8;   //BLINK
const byte TorpedoPin = 4;      //BLINK
const byte ImpulsePin = 11;     //FADE
const byte CabinPin2 = 5;       //FADE
const byte CabinPin1 = 3;       //FADE

// Faders                       pin           min  max  millis    on?    stop?
LedFader Cabin1Fader      (CabinPin1,          0,   255,  3000,   false,  true);
LedFader Cabin2Fader      (CabinPin2,          0,    80,  3000,   false,  true);
LedFader ShuttlebayFader  (ShuttlebayPin,      0,    80,  3000,   false,  true);
LedFader FloodlightFader  (FloodlightPin,      0,   255,  3000,   false,  true);
LedFader NacellesFader    (NacellesPin,        0,   255,  3000,   false,  true);
LedFader DeflectorFader   (DeflectorPin,       0,   255,  3000,   false,  true);
LedFader ImpulseFader     (ImpulsePin,         0,   255,  3000,   false,  true);
LedFader TorpedoFader     (TorpedoPin,         0,   255,  3000,   false,  true);

// Flashers                pin          off-time  on-time       on?
LedFlasher strobes    (StrobesPin,        1900,       100,     false);
LedFlasher strobes2   (Strobes2Pin,        900,       100,     false);
LedFlasher navigation (NavigationPin,      500,      1500,     false);

// states for the state machine
typedef enum
{
  initialState,
  //startup mode  
  wantCabin1startup,
  wantCabin2startup,
  wantShuttleBaystartup,
  wantFloodlightstartup,
  wantNacellesstartup,
  wantImpulsestartup,
  wantDeflectorstartup,
  wantTorpedostartup,
  wantNavigationstartup,
  wantStrobestartup,
  wantStrobe2startup,
  } 
states;
// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;

void setup ()
{
  pinMode (CabinPin1, OUTPUT);
  pinMode (CabinPin2, OUTPUT);
  pinMode (ShuttlebayPin, OUTPUT);
  pinMode (FloodlightPin, OUTPUT);
  pinMode (NacellesPin, OUTPUT);
  pinMode (ImpulsePin, OUTPUT);
  pinMode (DeflectorPin, OUTPUT);
  pinMode (TorpedoPin, OUTPUT);
  pinMode (StrobesPin, OUTPUT);
  pinMode (TorpedoPin, OUTPUT);
  pinMode (StrobesPin, OUTPUT);
  pinMode (Strobes2Pin, OUTPUT);
  // set up faders, flashers  
  Cabin1Fader.begin ();
  Cabin2Fader.begin ();
  ShuttlebayFader.begin ();
  FloodlightFader.begin ();
  NacellesFader.begin ();
  ImpulseFader.begin ();
  DeflectorFader.begin ();
  TorpedoFader.begin ();
  strobes.begin ();
  strobes2.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 = wantCabin1startup;
    break;

  case wantCabin1startup:
    Cabin1Fader.on();
    state = wantCabin2startup;
    break;

  case wantCabin2startup:
    Cabin2Fader.on();
    state = wantShuttleBaystartup;
    break;

  case wantShuttleBaystartup:
    ShuttlebayFader.on();
    state = wantFloodlightstartup;
    break;

  case wantFloodlightstartup:
    digitalWrite(FloodlightPin, HIGH);
    state = wantNacellesstartup;
    break;

  case wantNacellesstartup:
    NacellesFader.on();
    state = wantDeflectorstartup;
    break;

  case wantImpulsestartup:
    ImpulseFader.on();
    state = wantDeflectorstartup;
    break;

  case wantDeflectorstartup:
    DeflectorFader.on();
    state = wantTorpedostartup;
    break;

  case wantTorpedostartup:
    digitalWrite(TorpedoPin, HIGH);
    state = wantNavigationstartup;
    break;

  case wantNavigationstartup:
    navigation.on();
    state = wantStrobestartup;              
    break;

  case wantStrobestartup:
    strobes.on();
    state = wantStrobes2on;
    break;

  case wantStrobe2startup:
    strobes2.on();
    break;

    //impulse mode         


  }  // end of switch on state
}  // end of doStateChange


void loop ()
{
  if (millis () - lastStateChange >= timeInThisState)
    doStateChange ();
  // update faders, flashers
  Cabin1Fader.update ();
  Cabin2Fader.update ();
  ShuttlebayFader.update ();
  FloodlightFader.update ();
  DeflectorFader.update ();
  NacellesFader.update ();
  ImpulseFader.update ();
  TorpedoFader.update ();
  navigation.update ();
  strobes.update ();
  strobes2.update ();
  // other stuff here like testing switches
}  // end of loop
i'd be greatful if someone can look over it/test it on their arduino uno and see if they can offer some help or resolve the issue.
 

WBahn

Joined Mar 31, 2012
32,840
Uhm... we are not mind readers. You give us code that you say doesn't work but then apparently expect us to figure out what it is supposed to do based on the code that isn't working.

What portion of the code have you narrowed the problem down to and what, exactly, it is supposed to do and what, exactly, does it appear to be doing?
 

Thread Starter

c.marsh

Joined May 16, 2009
100
the only 2 parts of the code that isnt working is the impulse state and strobe 2 state.

they are simply not turning on and fading/blinking where they are meant to. i.e no light from the leds connected to the pins.

the impulse is meant to fade on and the strobe2 is meant to blink. as it is they do nothing where as the entierity of the rest of the code works flawlessly
 

LDC3

Joined Apr 27, 2013
924
You may want to change this code:

Rich (BB code):
  case wantNacellesstartup:
    NacellesFader.on();
    state = wantDeflectorstartup;  // should be wantImpulsestartup
    break;

  case wantImpulsestartup:
    ImpulseFader.on();
    state = wantDeflectorstartup;
    break;
Since wantImpulsestartup is never called.
and
Rich (BB code):
  case wantStrobestartup:
    strobes.on();
    state = wantStrobes2on;  // should be wantStrobe2startup
    break;

  case wantStrobe2startup:
    strobes2.on();
    break;
 

Thread Starter

c.marsh

Joined May 16, 2009
100
good sir thank you!

thats what happens when you spend 6 hours working on code lol you start to not see the small mistakes.

i just needed a second pair of eyes.

thank you once again, tested and working!
 

WBahn

Joined Mar 31, 2012
32,840
the only 2 parts of the code that isnt working is the impulse state and strobe 2 state.

they are simply not turning on and fading/blinking where they are meant to. i.e no light from the leds connected to the pins.

the impulse is meant to fade on and the strobe2 is meant to blink. as it is they do nothing where as the entierity of the rest of the code works flawlessly
I have a feeling that more of the code isn't working than that. But this gives us something to go on.

It would be nice if you told us the exact names that are used so that we don't have to guess.

(To self: Why is it that people insist on asking for free help from strangers and then force them to read minds and guess at things? :confused:)

Let's look at the "strobe2" situation. Doing a search for "strobe2" yields two snippets:

In the 'states' enumeration definition:

typedef enum
{
initialState,
//startup mode
wantCabin1startup,
wantCabin2startup,
wantShuttleBaystartup,
wantFloodlightstartup,
wantNacellesstartup,
wantImpulsestartup,
wantDeflectorstartup,
wantTorpedostartup,
wantNavigationstartup,
wantStrobestartup,
wantStrobe2startup, // HERE
}
states;

and in the doStatesChange() function:

case wantStrobe2startup:
strobes2.on();
break;

Now, if I search for "strobes2" (i.e., plural instead of singular), I get the following hits:

In the globals:
const byte Strobes2Pin = 2; //FLASH
...
LedFlasher strobes2 (Strobes2Pin, 900, 100, false);

In setup():
pinMode (Strobes2Pin, OUTPUT);
...
strobes2.begin ();

in the doStatesChange() function:
case wantStrobestartup:
strobes.on();
state = wantStrobes2on;
break;

and in loop():
strobes2.update ();

You are setting yourself up for problems if you mix conventions like this. Decide if you are going to name things singular or plural and then be consistent.

The doStatesChange() function appears to be intended, consistent with the name, to move to a different state each time it is called. So what state does it move to after you go into the "wantStrobe2startup" state?

You need to look at what your code does. Not what you want it to do, but what you actually told it to do.

You doStatesChange() function is actually written in a way that makes it very easy to walk through the progression of states that you are actually telling it to make:

case initialState: ==> wantCabin1startup;
case wantCabin1startup: ==> wantCabin2startup;
case wantCabin2startup: ==> wantShuttleBaystartup;
case wantShuttleBaystartup: ==> wantFloodlightstartup;
case wantFloodlightstartup: ==> wantNacellesstartup;
case wantNacellesstartup: ==> wantDeflectorstartup;

// Skips the 'wantImpulsestartup' case
case wantImpulsestartup: ==> wantDeflectorstartup;

case wantDeflectorstartup: ==> wantTorpedostartup;
case wantTorpedostartup: ==> wantNavigationstartup;
case wantNavigationstartup: ==> wantStrobestartup;
case wantStrobestartup: ==> wantStrobes2on;

// Missing a 'wantStrobes2on' case

case wantStrobe2startup: ==> [none specified, stays put]

So there is never any state change that moves it into the Implulsestartup or the wantStrobe2startup states. Not a big mystery why they don't seem to work, but it has nothing to do with the code for those states, it's because you never enter those states!
 

Thread Starter

c.marsh

Joined May 16, 2009
100
yeah, that was kinda pointed out in the post before yours, i appreciate the explanation, and im sure it will help someone else, but i had already known how the state changes worked.

the problem was that i couldn't see were my error was, the error being having the state calling for the deflector instead of the impulse, it was because i had been working on it for so many hours already and the time i was doing it.

i was working on it from 11pm to 5am, in hind sight a rather stupid time to work so long on code as the mind simply doesnt see simple things when your tired at 5am.

i would have probably realised had it been a reasonable hour.

still, thanks for the help there, most appreciated and i appologise now if any of that sounds brash or rude, its honestly not meant to, but i never left anyone guessing anything, i said in the initial post which parts of the code wasn't working and if you read through the code you can see what it is meant to do and as you and another good forumer pointed out, why it wasn't working.
 

ErnieM

Joined Apr 24, 2011
8,415
(To self: Why is it that people insist on asking for free help from strangers and then force them to read minds and guess at things? :confused:)
My guess is they themselves cannot or will not look at their work in a logical and consistent manor to tease out what the problem is.

I've oft found that by the time I'm half way thru a post with a question I've identified the issue just by describing how my thing is supposed to work, what it does, where it goes wrong and BING, there's the answer.

I had a dog I would talk to about my work, and that too helped lead to the fix.
 

Thread Starter

c.marsh

Joined May 16, 2009
100
i tried talking to my dog, but she takes that as an invitation to try and get on my lap, german shepard, not a comfortable animal to have sitting on you lol
 

WBahn

Joined Mar 31, 2012
32,840
My guess is they themselves cannot or will not look at their work in a logical and consistent manor to tease out what the problem is.

I've oft found that by the time I'm half way thru a post with a question I've identified the issue just by describing how my thing is supposed to work, what it does, where it goes wrong and BING, there's the answer.

I had a dog I would talk to about my work, and that too helped lead to the fix.
Same here. I have been known to e-mail myself a question as though I were a stranger and it often really helps. I will also talk to an empty chair and, on occasion, get into an argument with it (and sometimes proceed to lose).

I used to talk to my dog, but I can't with the current one -- I can't quite get over the willing suspension of disbelief when faced with such strident evidence right in front of me that I am talking to ... an idiot.
 

WBahn

Joined Mar 31, 2012
32,840
i tried talking to my dog, but she takes that as an invitation to try and get on my lap, german shepard, not a comfortable animal to have sitting on you lol
Oh, I don't know. I grew up with a Golden Retriever / Husky mix that was nothing more than a 90lb lap dog. I felt abandoned if she wasn't in my lap. Then again, I was a teenager and I had a lap.
 

LDC3

Joined Apr 27, 2013
924
I used to talk to my dog, but I can't with the current one -- I can't quite get over the willing suspension of disbelief when faced with such strident evidence right in front of me that I am talking to ... an idiot.
At least it is not a mirror. :D
 

djsfantasi

Joined Apr 11, 2010
9,237
I cover the wall with paper and write down everything I know or assume about the problem. The solution comes to me at about the half full point.
 
Top