fustrating code

Discussion in 'Programmer's Corner' started by c.marsh, Jun 11, 2013.

  1. c.marsh

    Thread Starter Active Member

    May 16, 2009
    100
    0
    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

    Code ( (Unknown Language)):
    1. // The Voyager
    2.  
    3. #include <LedFader.h>
    4. #include <LedFlasher.h>
    5.  
    6. // pin assignments PMW 3,5,6,9,10,11
    7. const byte StrobesPin = 1;      //FLASH
    8. const byte Strobes2Pin = 2;     //FLASH
    9. const byte NavigationPin = 7;   //FLASH
    10. const byte DeflectorPin = 6;    //FADE
    11. const byte NacellesPin = 9;     //FADE
    12. const byte ShuttlebayPin = 10;  //FADE
    13. const byte FloodlightPin = 8;   //BLINK
    14. const byte TorpedoPin = 4;      //BLINK
    15. const byte ImpulsePin = 11;     //FADE
    16. const byte CabinPin2 = 5;       //FADE
    17. const byte CabinPin1 = 3;       //FADE
    18.  
    19. // Faders                       pin           min  max  millis    on?    stop?
    20. LedFader Cabin1Fader      (CabinPin1,          0,   255,  3000,   false,  true);
    21. LedFader Cabin2Fader      (CabinPin2,          0,    80,  3000,   false,  true);
    22. LedFader ShuttlebayFader  (ShuttlebayPin,      0,    80,  3000,   false,  true);
    23. LedFader FloodlightFader  (FloodlightPin,      0,   255,  3000,   false,  true);
    24. LedFader NacellesFader    (NacellesPin,        0,   255,  3000,   false,  true);
    25. LedFader DeflectorFader   (DeflectorPin,       0,   255,  3000,   false,  true);
    26. LedFader ImpulseFader     (ImpulsePin,         0,   255,  3000,   false,  true);
    27. LedFader TorpedoFader     (TorpedoPin,         0,   255,  3000,   false,  true);
    28.  
    29. // Flashers                pin          off-time  on-time       on?
    30. LedFlasher strobes    (StrobesPin,        1900,       100,     false);
    31. LedFlasher strobes2   (Strobes2Pin,        900,       100,     false);
    32. LedFlasher navigation (NavigationPin,      500,      1500,     false);
    33.  
    34. // states for the state machine
    35. typedef enum
    36. {
    37.   initialState,
    38.   //startup mode  
    39.   wantCabin1startup,
    40.   wantCabin2startup,
    41.   wantShuttleBaystartup,
    42.   wantFloodlightstartup,
    43.   wantNacellesstartup,
    44.   wantImpulsestartup,
    45.   wantDeflectorstartup,
    46.   wantTorpedostartup,
    47.   wantNavigationstartup,
    48.   wantStrobestartup,
    49.   wantStrobe2startup,
    50.   }
    51. states;
    52. // state machine variables
    53. states state = initialState;
    54. unsigned long lastStateChange = 0;
    55. unsigned long timeInThisState = 1000;
    56.  
    57. void setup ()
    58. {
    59.   pinMode (CabinPin1, OUTPUT);
    60.   pinMode (CabinPin2, OUTPUT);
    61.   pinMode (ShuttlebayPin, OUTPUT);
    62.   pinMode (FloodlightPin, OUTPUT);
    63.   pinMode (NacellesPin, OUTPUT);
    64.   pinMode (ImpulsePin, OUTPUT);
    65.   pinMode (DeflectorPin, OUTPUT);
    66.   pinMode (TorpedoPin, OUTPUT);
    67.   pinMode (StrobesPin, OUTPUT);
    68.   pinMode (TorpedoPin, OUTPUT);
    69.   pinMode (StrobesPin, OUTPUT);
    70.   pinMode (Strobes2Pin, OUTPUT);
    71.   // set up faders, flashers  
    72.   Cabin1Fader.begin ();
    73.   Cabin2Fader.begin ();
    74.   ShuttlebayFader.begin ();
    75.   FloodlightFader.begin ();
    76.   NacellesFader.begin ();
    77.   ImpulseFader.begin ();
    78.   DeflectorFader.begin ();
    79.   TorpedoFader.begin ();
    80.   strobes.begin ();
    81.   strobes2.begin ();
    82.   navigation.begin ();
    83. }  // end of setup
    84.  
    85. void doStateChange ()
    86. {
    87.   lastStateChange = millis ();    // when we last changed states
    88.   timeInThisState = 1000;         // default one second between states
    89.  
    90.   switch (state)
    91.   {
    92.   case initialState:
    93.     state = wantCabin1startup;
    94.     break;
    95.  
    96.   case wantCabin1startup:
    97.     Cabin1Fader.on();
    98.     state = wantCabin2startup;
    99.     break;
    100.  
    101.   case wantCabin2startup:
    102.     Cabin2Fader.on();
    103.     state = wantShuttleBaystartup;
    104.     break;
    105.  
    106.   case wantShuttleBaystartup:
    107.     ShuttlebayFader.on();
    108.     state = wantFloodlightstartup;
    109.     break;
    110.  
    111.   case wantFloodlightstartup:
    112.     digitalWrite(FloodlightPin, HIGH);
    113.     state = wantNacellesstartup;
    114.     break;
    115.  
    116.   case wantNacellesstartup:
    117.     NacellesFader.on();
    118.     state = wantDeflectorstartup;
    119.     break;
    120.  
    121.   case wantImpulsestartup:
    122.     ImpulseFader.on();
    123.     state = wantDeflectorstartup;
    124.     break;
    125.  
    126.   case wantDeflectorstartup:
    127.     DeflectorFader.on();
    128.     state = wantTorpedostartup;
    129.     break;
    130.  
    131.   case wantTorpedostartup:
    132.     digitalWrite(TorpedoPin, HIGH);
    133.     state = wantNavigationstartup;
    134.     break;
    135.  
    136.   case wantNavigationstartup:
    137.     navigation.on();
    138.     state = wantStrobestartup;              
    139.     break;
    140.  
    141.   case wantStrobestartup:
    142.     strobes.on();
    143.     state = wantStrobes2on;
    144.     break;
    145.  
    146.   case wantStrobe2startup:
    147.     strobes2.on();
    148.     break;
    149.  
    150.     //impulse mode        
    151.  
    152.  
    153.   }  // end of switch on state
    154. }  // end of doStateChange
    155.  
    156.  
    157. void loop ()
    158. {
    159.   if (millis () - lastStateChange >= timeInThisState)
    160.     doStateChange ();
    161.   // update faders, flashers
    162.   Cabin1Fader.update ();
    163.   Cabin2Fader.update ();
    164.   ShuttlebayFader.update ();
    165.   FloodlightFader.update ();
    166.   DeflectorFader.update ();
    167.   NacellesFader.update ();
    168.   ImpulseFader.update ();
    169.   TorpedoFader.update ();
    170.   navigation.update ();
    171.   strobes.update ();
    172.   strobes2.update ();
    173.   // other stuff here like testing switches
    174. }  // end of loop
    175.  
    176.  
    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.
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    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?
     
  3. c.marsh

    Thread Starter Active Member

    May 16, 2009
    100
    0
    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
     
  4. LDC3

    Active Member

    Apr 27, 2013
    920
    160
    You may want to change this code:

    Code ( (Unknown Language)):
    1.  
    2.   case wantNacellesstartup:
    3.     NacellesFader.on();
    4.     state = [COLOR="Red"]wantDeflectorstartup[/COLOR];  // should be [COLOR="Lime"]wantImpulsestartup[/COLOR]
    5.     break;
    6.  
    7.   case wantImpulsestartup:
    8.     ImpulseFader.on();
    9.     state = wantDeflectorstartup;
    10.     break;
    11.  
    12.  
    Since wantImpulsestartup is never called.
    and
    Code ( (Unknown Language)):
    1.  
    2.   case wantStrobestartup:
    3.     strobes.on();
    4.     state = [COLOR="Red"]wantStrobes2on[/COLOR];  // should be [COLOR="Lime"]wantStrobe2startup[/COLOR]
    5.     break;
    6.  
    7.   case wantStrobe2startup:
    8.     strobes2.on();
    9.     break;
    10.  
    11.  
     
    sq-aristo likes this.
  5. c.marsh

    Thread Starter Active Member

    May 16, 2009
    100
    0
    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!
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    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!
     
  7. c.marsh

    Thread Starter Active Member

    May 16, 2009
    100
    0
    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.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    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.
     
  9. c.marsh

    Thread Starter Active Member

    May 16, 2009
    100
    0
    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
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    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.
     
    ErnieM likes this.
  11. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    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.
     
  12. LDC3

    Active Member

    Apr 27, 2013
    920
    160
    At least it is not a mirror. :D
     
  13. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    There are times I wonder.... :D
     
  14. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,804
    833
    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.
     
Loading...