renegadetechworks.com - OpenSource RenegadeTechworks Mitsubishi 3000gt Dodge Stealth Electronically Controlled Suspension OEM Replacement And Retrofit ECS controllers https://renegadetechworks.com/index.php/opensource 2024-11-21T10:25:27+00:00 renegadetechworks.com support@renegadetechworks.com Joomla! - Open Source Content Management RenegadeTechWorks 3000gt ECS Model 01/02 Controller Open Source Down Loads 2014-08-29T22:28:07+00:00 2014-08-29T22:28:07+00:00 https://renegadetechworks.com/index.php/opensource/downloads TechWorks Admin support@renegadetechworks.com <p> </p> <pre>*************************************************************************************** Description of Software &amp; electrical design: <br /> Mitsubishi 3000gt/Stealth Custom Controller system<br /> for controlling stock OEM mitsubishi ECS Struts. Hardware Platform: ATAMega 328 MicroController <br /> Copyright (C) 2014 Marcus Diaz, RenegadeTechWorks, LLC *************************************************************************************** Licensing: Licensed under GNU GPL Version 2 These software and hardware designs dowloadable below are free software. <br /> you can redistribute it and/or modify it under the terms of the <br /> GNU General Public License as published by the Free Software Foundation<br /> under Version 2 of the License This program is distributed in the hope that it will be useful,<br /> but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. <br /> <br /> See this link <a href="https://renegadetechworks.com/images/OpenSource/gpl-2.0.txt">GNU General Public License</a> for more details. or write to the Free<br /> Software Foundation, Inc.,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />****************************************************************************************</pre> <h4>Do a "right click -&gt; save link as "  to download and save the files below.</h4> <h3 style="padding-left: 30px;">      </h3> <table style="height: 53px;" border="10" width="604"> <tbody> <tr> <td> <h4>The system platform is based around an amtel328 core design using 12  channels  of the Microcontroller for monitoring and driving the struts using a Texas Inst. 4 channel H-Bridge motor driver .  2 channels for the sport /tour lights, 1 channel for the Selector switch,  &amp; 2 channels for controlling the addressable 4 LED array, 1 channel for switch selector mode type.     Total channels used = 18.</h4> <h4><span style="text-decoration: underline;"><a title="Renegade TechWorks 3000gt ECS Model 01 Controller Schematic PDF Download" href="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01PCB1_2.pdf" target="_blank">Renegade TechWorks 3000gt ECS Model 01 Controller Schematic PDF</a></span></h4> </td> </tr> </tbody> </table> <p> </p> <p> </p> <table style="height: 53px;" border="10" width="604"> <tbody> <tr> <td> <h4>The software contained in the  .INO file below  was developed using the Arduino IDE - set for an Uno / Amtel328  compile target.  The techworks controller can be programmed by using the current Arduino IDE with the standard Upload command &amp;  USB FTDI upload cable.  The cable connects to the 6- Pin programming headers on the controller board.</h4> <h4>The coding style is flat C-coding. No C++ and tons of comments.</h4> <h4><span style="text-decoration: underline;"><a title="Renegade TechWorks 3000gt ECS M01/M02Controller Firmware" href="https://renegadetechworks.com/images/OpenSource/RenegadeTechWorks3000gtECSFirmware.ino" target="_blank">Renegade TechWorks 3000gt ECS M01/M02 Controller Firmware</a></span></h4> </td> </tr> </tbody> </table> <p> </p> <p> </p> <pre>*************************************************************************************** Description of Software &amp; electrical design: <br /> Mitsubishi 3000gt/Stealth Custom Controller system<br /> for controlling stock OEM mitsubishi ECS Struts. Hardware Platform: ATAMega 328 MicroController <br /> Copyright (C) 2014 Marcus Diaz, RenegadeTechWorks, LLC *************************************************************************************** Licensing: Licensed under GNU GPL Version 2 These software and hardware designs dowloadable below are free software. <br /> you can redistribute it and/or modify it under the terms of the <br /> GNU General Public License as published by the Free Software Foundation<br /> under Version 2 of the License This program is distributed in the hope that it will be useful,<br /> but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. <br /> <br /> See this link <a href="https://renegadetechworks.com/images/OpenSource/gpl-2.0.txt">GNU General Public License</a> for more details. or write to the Free<br /> Software Foundation, Inc.,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />****************************************************************************************</pre> <h4>Do a "right click -&gt; save link as "  to download and save the files below.</h4> <h3 style="padding-left: 30px;">      </h3> <table style="height: 53px;" border="10" width="604"> <tbody> <tr> <td> <h4>The system platform is based around an amtel328 core design using 12  channels  of the Microcontroller for monitoring and driving the struts using a Texas Inst. 4 channel H-Bridge motor driver .  2 channels for the sport /tour lights, 1 channel for the Selector switch,  &amp; 2 channels for controlling the addressable 4 LED array, 1 channel for switch selector mode type.     Total channels used = 18.</h4> <h4><span style="text-decoration: underline;"><a title="Renegade TechWorks 3000gt ECS Model 01 Controller Schematic PDF Download" href="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01PCB1_2.pdf" target="_blank">Renegade TechWorks 3000gt ECS Model 01 Controller Schematic PDF</a></span></h4> </td> </tr> </tbody> </table> <p> </p> <p> </p> <table style="height: 53px;" border="10" width="604"> <tbody> <tr> <td> <h4>The software contained in the  .INO file below  was developed using the Arduino IDE - set for an Uno / Amtel328  compile target.  The techworks controller can be programmed by using the current Arduino IDE with the standard Upload command &amp;  USB FTDI upload cable.  The cable connects to the 6- Pin programming headers on the controller board.</h4> <h4>The coding style is flat C-coding. No C++ and tons of comments.</h4> <h4><span style="text-decoration: underline;"><a title="Renegade TechWorks 3000gt ECS M01/M02Controller Firmware" href="https://renegadetechworks.com/images/OpenSource/RenegadeTechWorks3000gtECSFirmware.ino" target="_blank">Renegade TechWorks 3000gt ECS M01/M02 Controller Firmware</a></span></h4> </td> </tr> </tbody> </table> <p> </p> RenegadeTechWorks 3000gt ECS Model 03 Controller Open Source Down Loads (2) 2014-08-29T22:28:07+00:00 2014-08-29T22:28:07+00:00 https://renegadetechworks.com/index.php/opensource/techworks-model03-downloads TechWorks Admin support@renegadetechworks.com <p> </p> <pre>*************************************************************************************** Description of Software &amp; electrical design: <br /> Mitsubishi 3000gt/Stealth Custom Controller system<br /> for controlling stock OEM mitsubishi ECS Struts. Hardware Platform: ATAMega 328 MicroController <br /> Copyright (C) 2014 Marcus Diaz, RenegadeTechWorks, LLC *************************************************************************************** Licensing: Licensed under GNU GPL Version 2 These software and hardware designs dowloadable below are free software. <br /> you can redistribute it and/or modify it under the terms of the <br /> GNU General Public License as published by the Free Software Foundation<br /> under Version 2 of the License This program is distributed in the hope that it will be useful,<br /> but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. <br /> <br /> See this link <a href="https://renegadetechworks.com/images/OpenSource/gpl-2.0.txt">GNU General Public License</a> for more details. or write to the Free<br /> Software Foundation, Inc.,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />****************************************************************************************</pre> <h4>Do a "right click -&gt; save link as "  to download and save the files below.</h4> <h3 style="padding-left: 30px;">      </h3> <table style="height: 53px;" border="10" width="604"> <tbody> <tr> <td> <h4>The system platform is based around an amtel328 core design using 12  channels  of the Microcontroller for monitoring and driving the struts using a Texas Inst. 4 channel H-Bridge motor driver .  2 channels for the sport /tour lights, 1 channel for the Selector switch,  &amp; 1  channels for controlling the addressable 4 LED array,  2 channels for I2C controll of the Acceleromter  Total channels used = 18.</h4> <h4><span style="text-decoration: underline;"><a title="Renegade TechWorks 3000gt ECS Model 03 Controller Schematic PDF Download" href="https://renegadetechworks.com/images/OpenSource/Renegade_Techworks_ECS_M03_Schematic.pdf" target="_blank">Renegade TechWorks 3000gt ECS Model 03 Controller Schematic PDF</a></span></h4> </td> </tr> </tbody> </table> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <pre>*************************************************************************************** Description of Software &amp; electrical design: <br /> Mitsubishi 3000gt/Stealth Custom Controller system<br /> for controlling stock OEM mitsubishi ECS Struts. Hardware Platform: ATAMega 328 MicroController <br /> Copyright (C) 2014 Marcus Diaz, RenegadeTechWorks, LLC *************************************************************************************** Licensing: Licensed under GNU GPL Version 2 These software and hardware designs dowloadable below are free software. <br /> you can redistribute it and/or modify it under the terms of the <br /> GNU General Public License as published by the Free Software Foundation<br /> under Version 2 of the License This program is distributed in the hope that it will be useful,<br /> but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. <br /> <br /> See this link <a href="https://renegadetechworks.com/images/OpenSource/gpl-2.0.txt">GNU General Public License</a> for more details. or write to the Free<br /> Software Foundation, Inc.,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />****************************************************************************************</pre> <h4>Do a "right click -&gt; save link as "  to download and save the files below.</h4> <h3 style="padding-left: 30px;">      </h3> <table style="height: 53px;" border="10" width="604"> <tbody> <tr> <td> <h4>The system platform is based around an amtel328 core design using 12  channels  of the Microcontroller for monitoring and driving the struts using a Texas Inst. 4 channel H-Bridge motor driver .  2 channels for the sport /tour lights, 1 channel for the Selector switch,  &amp; 1  channels for controlling the addressable 4 LED array,  2 channels for I2C controll of the Acceleromter  Total channels used = 18.</h4> <h4><span style="text-decoration: underline;"><a title="Renegade TechWorks 3000gt ECS Model 03 Controller Schematic PDF Download" href="https://renegadetechworks.com/images/OpenSource/Renegade_Techworks_ECS_M03_Schematic.pdf" target="_blank">Renegade TechWorks 3000gt ECS Model 03 Controller Schematic PDF</a></span></h4> </td> </tr> </tbody> </table> <p> </p> <p> </p> <p> </p> <p> </p> TechWorks ECS Model 01/02 Electrical Schematics 2014-08-29T20:42:34+00:00 2014-08-29T20:42:34+00:00 https://renegadetechworks.com/index.php/opensource/techworks-ecm-m01-pcb1-2-schematics TechWorks Admin support@renegadetechworks.com <p><img title="RenegadeTechWorks 3000gt ECS Schematic Overview" src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicOverview.png" alt="RenegadeTechWorks 3000gt ECS Schematic Overview" width="1178" height="928" /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS Processor Schematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicProcessor.png" alt="RenegadeTechWorks 3000gt ECS Processor Schematic " /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS OEM F12 F13 PlugsSchematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicOemPlugs.png" alt="RenegadeTechWorks 3000gt ECS OEM F12 F13 PlugsSchematic " width="1184" height="784" /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS Retrofit Plug Schematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicRetrofitPlug.png" alt="RenegadeTechWorks 3000gt ECS Retrofit Plug Schematic " /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS Programming Header Schematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicProgammingHeader.png" alt="RenegadeTechWorks 3000gt ECS Programming Header Schematic " /></p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS Schematic Overview" src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicOverview.png" alt="RenegadeTechWorks 3000gt ECS Schematic Overview" width="1178" height="928" /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS Processor Schematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicProcessor.png" alt="RenegadeTechWorks 3000gt ECS Processor Schematic " /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS OEM F12 F13 PlugsSchematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicOemPlugs.png" alt="RenegadeTechWorks 3000gt ECS OEM F12 F13 PlugsSchematic " width="1184" height="784" /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS Retrofit Plug Schematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicRetrofitPlug.png" alt="RenegadeTechWorks 3000gt ECS Retrofit Plug Schematic " /></p> <p> </p> <p><img title="RenegadeTechWorks 3000gt ECS Programming Header Schematic " src="https://renegadetechworks.com/images/OpenSource/TechWorksECSM01SchematicProgammingHeader.png" alt="RenegadeTechWorks 3000gt ECS Programming Header Schematic " /></p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> TechWorks ECS Model 01/02 Firmware Source Code 2014-08-22T19:06:53+00:00 2014-08-22T19:06:53+00:00 https://renegadetechworks.com/index.php/opensource/techworks-ecs-software TechWorks Admin support@renegadetechworks.com <pre>//****************************************************************************<br />//<br />//     Description of Software:    <br />//<br />//     Mitsubishi 3000gt/Stealth Custom Controller Program<br />//     for controlling stock OEM mitsubishi ECS Struts.<br />//     Hardware Platform:   ATAMega 328 MicroController<br />//<br />//     Copyright (C) 2014  Marcus Diaz, RenegadeTechWorks, LLC<br />//<br />//****************************************************************************<br />// Licensing:<br />//              Licensed under GNU GPL Version 2<br />//<br />//    This program is free software; you can redistribute it and/or modify<br />//    it under the terms of the GNU General Public License as published by<br />//    the Free Software Foundation under Version 2 of the License<br />//<br />//    This program is distributed in the hope that it will be useful,<br />//    but WITHOUT ANY WARRANTY; without even the implied warranty of<br />//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br />//    GNU General Public License for more details.<br />//<br />//    You should have received a copy of the GNU General Public License along<br />//    with this program; if not, write to the Free Software Foundation, Inc.,<br />//    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />//****************************************************************************<br />// VERSION HISTORY<br />// --------------------------------------------------------------------<br />// SW Ver  :  v1.0<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      09/15/2012<br />// Comments:  Production version of code based on branch from original<br />//            prototype controller <br />//            Diag Mode working - this version disables Select SW. <br />//            check during Diag Mode Execution.'<br />//            This overcomes Voltage Drop.<br />//<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.1<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/7/2012<br />// Comments:  Production version of code based on branch from original<br />//            prototype controller <br />//            -v1.1 uses a Jumper accross Pins 6 &lt;--&gt; Pin3 on the<br />//             program header to indicate what type of selector switch<br />//             is being used as follows:<br />//                Jumper On : USE push Button ECS Switch<br />//                Jumper Off: Use Rotatry 4 Position Selector Switch<br />//            -v1.1 Has implemented the new re-written Diagonstic seq.<br />//                for using both LED's and OEM Tour/Sport Lights<br />//            -v1.1 also implements Diagnostic mode interrupt code<br />//            -v1.1 Implements a Dimming control option. To use at <br />//                 start up put the system in Diagnostic Mode using<br />//                 using either the stock ECS switch by pressing and<br />//                 holding or Rotary switch to position 4. <br />//*******************************************************************<br />// SW Ver  :  v1.1<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/12/2012<br />// Comments:  Base  SVN production version of the code.<br />//            <br />//          Production version of code based on branch from original<br />//            prototype controller <br />//            -v1.1 uses a Jumper accross Pins 6 &lt;--&gt; Pin3 on the<br />//             program header to indicate what type of selector switch<br />//             is being used as follows:<br />//                Jumper On : USE push Button ECS Switch<br />//                Jumper Off: Use Rotatry 4 Position Selector Switch<br />//            -v1.1 Has implemented the new re-written Diagonstic seq.<br />//                for using both LED's and OEM Tour/Sport Lights<br />//            -v1.1 also implements Diagnostic mode interrupt code<br />//            -v1.1 Implements a Dimming control option. To use at <br />//                 start up put the system in Diagnostic Mode using<br />//                 using either the stock ECS switch by pressing and<br />//                 holding or Rotary switch to position 4. <br />//<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.2<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/13/2012<br />// Comments:  <br />//            added 2000 msec delay in diagonstic routine during display<br />//            of sucessfull strut test when both Tour &amp; Sport lights<br />//            are turned on.            <br />//            <br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.3<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/23/2012<br />// Comments:  <br />//          - Corrected all code paths to correctly handle UNKNOWN Strut State<br />//          - Made Setup initialization routine more robust - all struts must aggree<br />//          - Also during setup Tour/Sport will go into error mode if there<br />//            was disagreement<br />//          - added displayStrut as derivative of original readstrut()<br />//          - doubled MAXTRIES to 10000 - helps with laggy struts <br />//          - fixed bug in readSelector() in case of PUSHBUTTON - was <br />//            missing " else return (strutState)" at end<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.5<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/31/2012<br />// Comments:  <br />//          - Baselining this as the final production version for the first production<br />//            run. <br />//          - Other than these comments there are no diffs between this and <br />//          - the previous verson<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.6<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/13/2014<br />// Comments:  <br />//          - Added saftey code to detect intermittent broken signal wire<br />//            and prevent motor driver burn out<br />//          - When broken wire detected - Both Tour Sport Lights will alternately flash<br />//          -added new diagnostic read status mode (only available from pushbutton)<br />//          -added new startup &amp; error sequencing display routines for Sport/Tour Lights<br />//          -LEDs now Have Yellow Error Condition = Signal Wire Failure<br />//          -LEDs Red Error Condition = Motor command sequence failure.<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.7<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/13/2014<br />// Comments:  <br />//          -Added EProm Read/Write Code during startup to retrieve/store LED <br />//           brigtness defaults<br />//          -Increased MaxTries by 20% from 10000 to 12000<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.7<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/17/2014<br />// Comments:  <br />//          -Minor display tweek for Diagnostic display of Sport / Tourlights<br />//           So that they just blink 3 times in groups of 3 regardless of which <br />//           strut is being reported for a Motor Command Failure<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.7a<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/17/2014<br />// Comments:  <br />//          -increased MaxTries to 15000 from 12000<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.8<br />// HWDesign:  ECM M01 PCB Rev1.1,1.2<br />// Date:      12/28/2014<br />// Comments:  <br />//          -Replaced LED CODE to use FAST SPI LED Library <br />//          -Now supports WS2801 &amp; WS2812b &amp; Others<br />//          -Added code to read Pin Jumper on Pins 2 to 3.<br />//          -If Pin 2 Jumpered to ground/Pin3 Code will use WS2801 LED <br />//           IF Pin 2 not Jumpered to Ground/ Pin 3 Code will use WS2812b LED <br />//<br />//          -In displayStrut - Added setLED(x,D_OFF)to Case Unknown to make<br />//           timed loop equal for all cases while Maxtries is incrementing<br />//          --NOTE 1.8a &amp; 1.8 have a timming issue with WS2801 LEDs.<br />//           all other code fine.<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.8a<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      1/1/2015<br />// Comments:  <br />//          -removed led.Show() call from setLed()<br />//          -added led.Show() in all places needed<br />//          -NOTE 1.8a &amp; 1.8 have a timming issue with WS2801 LEDs.<br />//           all other code fine.    <br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.9<br />// HWDesign:  ECM M01 PCB Rev1.1,1.2<br />// Date:      2/18/2015<br />// Comments:  <br />//          -removed all delays that were added to try and force sync up<br />//          -upgraded to fastLED lib 3.0 (fixed sync problem with WS2801's)<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.9a<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      2/18/2015<br />// Comments:  <br />//          -added 1 millesecond delay to Motor enable drive loop to fix<br />//           problem with motor turning of too soon on the edge case<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.10<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      8/02/2015<br />// Comments:  <br />//          -setStrutMode() added 33 msec delay just outside of Motor While loop to<br />//           force motor to run 33 msec longer.  Forces overrun of switch target<br />//          -setStrutMode() removed saftey checks for previous strut errors, code <br />//           will now retry a strut even if in previous cycle it had any kind of error.<br />//          -increased MaxTries to 7194(2.0 secs) from 2000<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.11<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      8/09/2015<br />// Comments:  <br />//          -displayStrut() <br />//             -added debounce algorithm to ignore S1 S2 transients<br />//              less than X microseconds apart.<br />//             -added timeout code to abort after Y milleseconds of trying<br />//<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.12<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      8/09/2015<br />// Comments:  <br />//          -Removed MotorEnable(motorx,LOW) commands all placess except Startup<br />//          -Set Motor Delay overun to 5msec instead of 33msec<br />//<br />//*****************************************************************************<br /><br />#include &lt;EEPROM.h&gt;<br />#include &lt;FastLED.h&gt;<br /><br />#define strutFR 1<br />#define strutFL 2<br />#define strutRR 3<br />#define strutRL 4<br /><br />#define HARD     1<br />#define MEDIUM     2<br />#define SOFT     3<br />#define DIAG    4<br />#define READDIAG 5<br />#define UNKNOWN 7<br />#define NOCHANGE 10<br />#define STARTUP  11<br /><br />#define D_RED       1<br />#define D_GREEN       2<br />#define D_BLUE       3<br />#define D_TURQUOIS 4<br />#define D_PURPLE   5<br />#define D_YELLOW   6<br />#define D_STARTUP  7<br />#define D_HARD     8<br />#define D_MEDIUM   9<br />#define D_SOFT     10<br />#define D_WHITE    11<br />#define D_OFF      12 <br /><br /><br />#define RED100   0xFF0000 // Bright Red<br />#define GREEN100 0x00FF00 // Bright Green<br />#define BLUE100  0x0000FF // Bright Blue<br />#define RED001   0x010000 // Faint red<br />#define RED050   0x800000 // 1/2 red (0x80 = 128 out of 256)<br />#define RED025   0x300000 // 25% RED<br />#define GREEN050 0x008000 // half Green<br />#define GREEN025 0x003000 // 25% GREEN<br />#define BLUE050  0x000080 // half Blue<br />#define BLUE025  0x000030 // 25% BLUE<br />#define WHITE015 0x151515 // 15% White<br />#define WHITE010 0x101010 // 10% White<br />#define WHITE005 0x050505 // 05% White<br />#define WHITE001 0x020202 // 05% White<br />#define WHITE100 0xFFFFFF // 100% White<br /><br />#define LEDOFF           0x000000    <br />#define RED            0xFF0000 <br />#define ORANGE         0xFF5500<br />#define ORANGEYELLOW   0xFFA000<br />#define YELLOW         0xFFFF00<br />#define YELLOWGREEN    0xA0FF00<br />#define GREENYELLOW    0x50FF00<br />#define GREEN          0x00FF00<br />#define GREENCYAN      0x00FF4B<br />#define CYAN           0x00FFFF<br />#define CYANBLUE       0x00A0FF<br />#define BLUECYAN       0x005AFF  <br />#define BLUE           0x0000FF<br />#define BLUEVIOLET     0x4800FF<br />#define VIOLETBLUE     0x7500FF<br />#define MAGENTA        0xFF00FF<br />#define PINKDEEP       0xFF1493<br />#define PINKHOT        0xFF69B4<br />#define PINK           0xF3967A<br /> <br />#define VIOLET         0xEE82EE<br /><br /><br />#define ERROR 1<br />#define NOERROR 0<br /><br />#define SIGNAL_WIRE_FAILURE   2<br />#define MOTOR_COMMAND_FAILURE 1<br />#define NO_FAILURES 0<br /><br />#define MAXTRIES 7194    // maxtries = 7194 = max of 2 secs of Motor on<br /><br /><br />#define MAXLIGHTLOOPS 20<br /><br />#define ROTARY 1<br />#define PUSHBUTTON 2<br />#define SAMEMODE 1<br />#define NEXTMODE 2<br /><br />#define LED_LEVEL1 1       // minimum brightness level<br />#define LED_LEVEL2 2<br />#define LED_LEVEL3 3<br />#define LED_LEVEL4 4       // Maximum brightness level<br /><br />#define EPROM_LEDMODE 1    // address in EPROM for LEDMODE<br /><br />//<br />//  Allocate Structure For LED Strip<br />//<br />#define NUM_LEDS 4<br />struct CRGB ledColors[NUM_LEDS];<br /><br />int ledMode = LED_LEVEL2;<br /><br />int selectorType = ROTARY;  // selectorType captures what kind of selector switch is hooked up to unit<br /><br />// Previous State of Selector Switch<br />int currentSelectorState = STARTUP;<br /><br />int strutState = HARD;<br />int strutStateFL;<br />int strutStateFR;<br />int strutStateRL;<br />int strutStateRR;<br /><br />long ledHardValue ;<br />long ledMediumValue;<br />long ledSoftValue;<br />long ledRedValue;<br /><br /><br />int LED1 = 0;    // Front Left Strut LED<br />int LED2 = 1;    // Front Right Strut LED<br />int LED3 = 2;    // Rear Left Strut LED<br />int LED4 = 3;    // Rear Right Strut LED <br /><br />int errorFL = NO_FAILURES;<br />int errorFR = NO_FAILURES;<br />int errorRL = NO_FAILURES;<br />int errorRR = NO_FAILURES;<br /><br />int tryCount = 0;<br /><br />int lightLoopCounter = MAXLIGHTLOOPS;<br />int lightLoopCounterValue;<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//  Map Board I/O Pins to World<br />//<br />// /////////////////////////////////////////////////////////////////////////////////////<br />// Front Left Strut Pins<br />int strutFLswitch1 = 3; <br />int strutFLswitch2 = 2;<br />int strutFLmotor   = 10;<br /><br />// Front Right Strut Pins<br />int strutFRswitch1 = 5; <br />int strutFRswitch2 = 4;<br />int strutFRmotor   = 11;<br /><br />// Rear Left Strut Pins<br />int strutRLswitch1 = 7; <br />int strutRLswitch2 = 6;<br />int strutRLmotor   = 14;<br /><br />// Rear Right Strut Pins<br />int strutRRswitch1 = 9; <br />int strutRRswitch2 = 8;<br />int strutRRmotor   = 15;<br /><br />//Mode Selector Switch Pin<br />int ModeSelectSwitch = 17;<br /><br />//Switch Type Selector Option Pin<br />int switchTypeSelect = 1;<br /><br />//LED Type Selector Option Pin<br />int LEDTypeSelect = 0;<br /><br />//MotorEnable Pin<br />int MotorEnable = 16;<br /><br />//<br />// LED Light Ports <br />//<br />#define DATA_PIN 18    // Data pin that led data will be written out over<br />#define CLOCK_PIN 19   // Clock pin only needed for SPI based chipsets when not using hardware SP<br /><br />// Sport Light Port<br />int SportLight = 12; <br /><br />// Tour Light Port<br />int TourLight = 13;<br /><br /><br /><br /> <br />///////////////////////////////////////////////////////////////////////////////////////<br />// setup initializes startup <br />///////////////////////////////////////////////////////////////////////////////////////<br />void setup(){<br /><br />        int selectorValue;<br />        int diagLoopNumber;<br />  <br /><br />      // initalize MotorEnable Pin for digital output and disable motors<br />    pinMode(MotorEnable, OUTPUT);<br />    digitalWrite(MotorEnable,LOW);<br /> <br />        // Initialize the Sport &amp; Tour Light OutPut Pins<br />        pinMode(SportLight, OUTPUT); <br />        pinMode(TourLight, OUTPUT);<br /> <br />          // intialiaze all the motor out pins<br />        initializeMotorIOPin(strutFLmotor);<br />        initializeMotorIOPin(strutFRmotor);<br />        initializeMotorIOPin(strutRRmotor);<br />        initializeMotorIOPin(strutRLmotor);<br />    <br />        // Initialize all the strut input pins <br />     initializeStrutIOPin(strutFRswitch1);<br />     initializeStrutIOPin(strutFRswitch2);<br />     initializeStrutIOPin(strutFLswitch1);<br />     initializeStrutIOPin(strutFLswitch2);<br />     initializeStrutIOPin(strutRRswitch1);<br />     initializeStrutIOPin(strutRRswitch2);<br />     initializeStrutIOPin(strutRLswitch1);<br />     initializeStrutIOPin(strutRLswitch2);<br /> <br /><br /><br />    // analyze   LED  Pin To figure out what type of leds are being used <br />        // HIGH = WS2801   LOW = WS2812<br />        pinMode(LEDTypeSelect, INPUT);<br />        digitalWrite(LEDTypeSelect,HIGH); <br /><br />        // Read the pin to see if it's HIGH or LOW      <br />        if(digitalRead(LEDTypeSelect) == HIGH){<br />           FastLED.addLeds&lt;WS2812B, DATA_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />    }else{<br />            FastLED.addLeds&lt;WS2801, DATA_PIN, CLOCK_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />    }<br /><br />        // ************************************************************<br />        //<br />        // LED Setup &amp; Initialization<br />        //<br />        // ************************************************************<br />        // Uncomment one of the following lines for your leds arrangement.<br />        // FastLED.addLeds&lt;WS2801, DATA_PIN, CLOCK_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />        // FastLED.addLeds&lt;TM1803, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;TM1804, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;TM1809, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2811, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2812, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2812B, DATA_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />        // FastLED.addLeds&lt;UCS1903, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2801, RGB&gt;(ledColors, NUM_LEDS);<br />        // FastLED.addLeds&lt;SM16716, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;LPD8806, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;SM16716, DATA_PIN, CLOCK_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;LPD8806, DATA_PIN, CLOCK_PIN, RGB&gt;(leds, NUM_LEDS);<br />       <br />        // Clear Out the LED arrays<br />        memset(ledColors, 0,  NUM_LEDS * sizeof(struct CRGB));   <br />        LEDS.show(); //Push the current color frame to the LEDs<br /><br /><br />    // analyze Selector TYPE Input Pin To figure out if Rotary of Pushbutton is connected <br />        // HIGH = Rotary   LOW = PUSHBUTTON<br />        pinMode(switchTypeSelect, INPUT);<br />        digitalWrite(switchTypeSelect,HIGH); <br /><br />        // Read the pin to see if it's HIGH or LOW      <br />        if(digitalRead(switchTypeSelect) == LOW){<br />             selectorType = PUSHBUTTON;<br />             pinMode(ModeSelectSwitch, INPUT);<br />             digitalWrite(ModeSelectSwitch, HIGH);<br />             lightLoopCounterValue = 900; // the lightloopcounter is timing dependent based on selector type<br />             diagLoopNumber = 450;<br />        }else{<br />             selectorType = ROTARY;<br />             pinMode(ModeSelectSwitch, INPUT);<br />             digitalWrite(ModeSelectSwitch, LOW);<br />             lightLoopCounterValue = 60;  // the lightloopcounter is timing dependent based on selector type<br />             diagLoopNumber = 40;<br />        } <br />        lightLoopCounter = lightLoopCounterValue;<br /><br />    //<br />    // Retrieve LED Brigtness level from EPROM - if not within range set it to 2nd level and write it back        //<br />        ledMode = eepromReadInt(EPROM_LEDMODE);    <br />    //<br />    // Make sure the value is with in range<br />    //<br />    if (ledMode != LED_LEVEL1 &amp;&amp; ledMode != LED_LEVEL2 &amp;&amp; ledMode != LED_LEVEL3 &amp;&amp; ledMode != LED_LEVEL4){<br />        // It's not within range - set to LEVEL two and write it back<br />        ledMode = LED_LEVEL1;<br />            eepromWriteInt(EPROM_LEDMODE,ledMode);<br />    } <br /><br /><br />    //<br />    // Blink the Sport / Tour Lights at startup as test to driver to show they are working.<br />    //<br />        blinkBothLights(1,3000,1,800);<br />    <br />  <br />    //<br />    //SET the LEDs to what the EPROM Value Says<br />    //<br />    switch(ledMode){<br />        case LED_LEVEL1:<br />                     //SET the LEDs to Minimum Brightness Mode    <br />                         ledHardValue = rgbEncode(2,0,2);<br />                         ledMediumValue = rgbEncode(0,2,2);<br />                         ledSoftValue = rgbEncode(0,0,2);<br />                         ledRedValue = rgbEncode(2,0,0);<br /><br />            break;<br />        case LED_LEVEL2:<br />                     //SET the LEDs to 2nd to Minimum Brightness Mode    <br />                         ledHardValue = rgbEncode(10,0,10);<br />                         ledMediumValue = rgbEncode(0,10,5);<br />                         ledSoftValue = rgbEncode(0,0,10);<br />                         ledRedValue = rgbEncode(10,0,0);<br />            break;<br />        case LED_LEVEL3:<br />                     //SET the LEDs to Medium Brightness Mode    <br />                         ledHardValue = rgbEncode(70,0,70);<br />                         ledMediumValue = rgbEncode(0,70,35);<br />                         ledSoftValue = rgbEncode(0,0,70);<br />                         ledRedValue = rgbEncode(70,0,0);<br />            break;<br />        case LED_LEVEL4:<br />                     //SET the LEDs to Maximum Brightness Mode    <br />                         ledHardValue = rgbEncode(120,0,120);<br />                         ledMediumValue = rgbEncode(0,120,60);<br />                         ledSoftValue = rgbEncode(0,0,150);<br />                         ledRedValue = rgbEncode(120,0,0);<br />            break;<br />    }<br /><br />        //  <br />        //See if user wants to set brightness level of LEDs<br />        //<br />        // Let them know it's time &amp; wait 3 secs<br />        setLED(LED1, D_WHITE);<br />        setLED(LED2, D_WHITE);<br />        setLED(LED3, D_WHITE);<br />        setLED(LED4, D_WHITE);<br />        <br />        LEDS.show();<br />    <br />        delay(2000);<br /><br /><br /><br />    //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br />        selectorValue = readSelectorSwitch();<br />        if(selectorValue == READDIAG || selectorValue == DIAG ){<br /><br />            // ok..they want to adjust - put the leds into their medium value colors so they know<br />            strutState=DIAG;<br />            <br />        //Default the LEDs to MEDIUM Mode    <br />            ledHardValue = rgbEncode(10,0,10);<br />            ledMediumValue = rgbEncode(0,10,5);<br />            ledSoftValue = rgbEncode(0,0,10);<br />            ledRedValue = rgbEncode(10,0,0);<br />            strutState = MEDIUM;<br />  <br />            setLED(LED1, D_HARD);<br />            setLED(LED2, D_MEDIUM);<br />            setLED(LED3, D_SOFT);<br />            setLED(LED4, D_RED);<br />            LEDS.show();<br />                    <br />            // Loop for a while to let the adjust and then bail<br />        for(int i = 1; i &lt;diagLoopNumber; i++){<br />                  delay(20);<br />                   switch(readSelectorSwitch()){<br />                      case HARD:<br />                     //SET the LEDs to Minimum Brightness Mode    <br />                     ledMode = LED_LEVEL1;<br />                         ledHardValue = rgbEncode(2,0,2);<br />                         ledMediumValue = rgbEncode(0,2,2);<br />                         ledSoftValue = rgbEncode(0,0,2);<br />                         ledRedValue = rgbEncode(2,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />                      break;<br /><br />                      case MEDIUM:<br />                     //SET the LEDs to 2nd to Minimum Brightness Mode    <br />                     ledMode = LED_LEVEL2;<br />                         ledHardValue = rgbEncode(10,0,10);<br />                         ledMediumValue = rgbEncode(0,10,5);<br />                         ledSoftValue = rgbEncode(0,0,10);<br />                         ledRedValue = rgbEncode(10,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />                      break;<br /><br />                      case SOFT:<br />                     //SET the LEDs to Medium Brightness Mode    <br />                     ledMode = LED_LEVEL3;<br />                         ledHardValue = rgbEncode(70,0,70);<br />                         ledMediumValue = rgbEncode(0,70,35);<br />                         ledSoftValue = rgbEncode(0,0,70);<br />                         ledRedValue = rgbEncode(70,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />                      break;<br />               <br />              case DIAG:<br />                     //SET the LEDs to Maximum Brightness Mode    <br />                     ledMode = LED_LEVEL4;<br />                         ledHardValue = rgbEncode(120,0,120);<br />                         ledMediumValue = rgbEncode(0,120,60);<br />                         ledSoftValue = rgbEncode(0,0,150);<br />                         ledRedValue = rgbEncode(120,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />              break;<br />                 }<br />        }<br />        //<br />        // IF the new mode is not whats already in EProm Update EProm<br />        //<br />            if(ledMode != eepromReadInt(EPROM_LEDMODE)) eepromWriteInt(EPROM_LEDMODE,ledMode);<br />        }<br />        <br />    // Let them know it's over<br />        setLED(LED1,D_WHITE);<br />        setLED(LED2,D_WHITE);<br />        setLED(LED3,D_WHITE);<br />        setLED(LED4,D_WHITE);<br />        LEDS.show();<br />        delay(2000);<br />        <br />        // Set LEDS &amp; Lights to current state of Struts <br />        strutStateFL = displayStrut(strutFLswitch1,strutFLswitch2,LED1,ERROR);<br />        strutStateFR = displayStrut(strutFRswitch1,strutFRswitch2,LED2,ERROR);<br />        strutStateRL = displayStrut(strutRLswitch1,strutRLswitch2,LED3,ERROR);<br />        strutStateRR = displayStrut(strutRRswitch1,strutRRswitch2,LED4,ERROR); <br />        <br />        <br />        // IF the state of all the struts match <br />    // set strutState to whatever the Front Left Strut is reading     <br />    if(strutStateFL == strutStateFR &amp;&amp; <br />       strutStateFL == strutStateRL &amp;&amp;     <br />       strutStateFL == strutStateRR &amp;&amp;     <br />       strutStateFL != UNKNOWN ){<br />        <br />          strutState=strutStateFL;<br />          currentSelectorState = strutStateFL;<br />          <br />                  // Set the TourSport Lights to indicate initial setting <br />              setTourSportLights();<br />        }else{<br />         // not sure what state all struts are in. Arbitarily try and set to HARD<br />         // Let user try and move to different state<br />         strutState=HARD;<br />         currentSelectorState = HARD;<br />                 setStrutMode( HARD, HARD, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                 setStrutMode( HARD, HARD, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                 setStrutMode( HARD, HARD, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                 setStrutMode( HARD, HARD, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br /><br />                 setTourSportLights();<br />    }<br />}<br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />//<br />//            main loop  <br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />void loop()<br />{<br />  <br />   //<br />   // Make Sure the Strut Motors are OFF<br />   //<br />   digitalWrite(strutFRmotor, LOW);<br />   digitalWrite(strutFLmotor, LOW);<br />   digitalWrite(strutRRmotor, LOW);<br />   digitalWrite(strutRLmotor, LOW);<br /><br />   //  SIGNAL_WIRE_FAILURE   <br />   //  MOTOR_COMMAND_FAILURE <br />   //  NO_FAILURES <br /> <br />   <br />   delay(20);<br />   //<br />   // Read the state of the Switch<br />   //   <br />     switch(readSelectorSwitch()){<br />    case(NOCHANGE):<br />                // Set LEDS &amp; Lights to current state of Struts <br />                if (errorFL == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutFLswitch1,strutFLswitch2,LED1,ERROR)) {<br />            errorFL = SIGNAL_WIRE_FAILURE;<br />            setLED(LED1,D_YELLOW);<br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />                if (errorFR == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutFRswitch1,strutFRswitch2,LED2,ERROR)){<br />            errorFR = SIGNAL_WIRE_FAILURE;<br />                   setLED(LED2,D_YELLOW);<br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />                if (errorRL == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutRLswitch1,strutRLswitch2,LED3,ERROR)){<br />            errorRL = SIGNAL_WIRE_FAILURE;<br />                   setLED(LED3,D_YELLOW);<br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />                if (errorRR == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutRRswitch1,strutRRswitch2,LED4,ERROR)){<br />            errorRR = SIGNAL_WIRE_FAILURE;<br />            setLED(LED4,D_YELLOW); <br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />        setTourSportLights();<br />                break;<br /><br />    case(HARD):<br /><br />                  // Front Right Strut <br />                setStrutMode( HARD, HARD, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                setStrutMode( HARD, HARD, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                setStrutMode( HARD, HARD, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                setStrutMode( HARD, HARD, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br /><br />        // Set the TourSport Lights to indicate if setting of all four was sucessfull or Error <br />        setTourSportLights();<br />        break;<br /><br />    case(MEDIUM):<br /><br />                setStrutMode( MEDIUM, MEDIUM, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                setStrutMode( MEDIUM, MEDIUM, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                setStrutMode( MEDIUM, MEDIUM, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                setStrutMode( MEDIUM, MEDIUM, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br /><br />        // Set the TourSport Lights to indicate if setting of all four was sucessfull or Error <br />        setTourSportLights();<br />        break;<br /><br />    case(SOFT):<br />                <br />                setStrutMode( SOFT, SOFT, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                setStrutMode( SOFT, SOFT, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                setStrutMode( SOFT, SOFT, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                setStrutMode( SOFT, SOFT, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);                 <br /><br />        // Set the TourSport Lights to indicate if setting of all four was sucessfull or Error <br />        setTourSportLights();<br />                break;<br /><br /><br /><br />    case(READDIAG):<br />                readDiagnostics();<br />                break;<br /><br />    case(DIAG):<br />                runDiagnostic();<br />                break;<br /><br />        case(UNKNOWN):<br />        setTourSportLights();<br />                break;<br /><br />       }<br /><br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink one of the lights<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkLights(int lightaddress, int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay){<br />    <br />    setLights(lightaddress,LOW);<br />    <br />    for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br />        <br />    for (int count = numberBlinks; count &gt; 0; count--){<br />       delay(blinkDelay);<br />       setLights(lightaddress,HIGH);<br />       delay(blinkDelay);<br />       setLights(lightaddress,LOW);<br />        }<br />    delay(groupDelay);<br />    }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink one of the lights<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkBothLights(int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay){<br />    <br />    // Turn off both Lights<br />    setLights(SportLight,LOW);<br />    setLights(TourLight,LOW);<br />    <br />    for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br />        <br />    for (int count = numberBlinks; count &gt; 0; count--){<br />       delay(blinkDelay);<br />           setLights(SportLight,HIGH);<br />           setLights(TourLight,HIGH);<br />       <br />       delay(blinkDelay);<br />           setLights(SportLight,LOW);<br />           setLights(TourLight,LOW);<br />        }<br />    delay(groupDelay);<br />    }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Set TourSport Lights to CorrectMode<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setTourSportLights(){<br /><br />                lightLoopCounter--;<br />                if(lightLoopCounter &lt;= 0){<br />                    lightLoopCounter = lightLoopCounterValue;<br />                    <br />  <br />                  // if there was a problem start flashing shit.. to the do sport/tourlight error thing<br />            if (errorFL == SIGNAL_WIRE_FAILURE || <br />                errorFR == SIGNAL_WIRE_FAILURE ||<br />                errorRL == SIGNAL_WIRE_FAILURE ||<br />                errorRR == SIGNAL_WIRE_FAILURE ){<br />               <br />                           for(int i=0;i&lt;4;i++){<br />                         setLights(SportLight,HIGH);<br />                             setLights(TourLight,LOW);<br />                 delay(250);<br />                         setLights(SportLight,LOW);<br />                             setLights(TourLight,HIGH);<br />                 delay(250);<br />                   }<br />                    }<br />                          <br />                    if(errorFL == MOTOR_COMMAND_FAILURE || <br />               errorFR == MOTOR_COMMAND_FAILURE ||<br />               errorRL == MOTOR_COMMAND_FAILURE ||<br />               errorRR == MOTOR_COMMAND_FAILURE ) {<br />                            blinkBothLights(1,300,3,1000);<br />                           // blinkLights(SportLight, 3,800,1,1); <br />                            delay(300); <br />                    }   <br /><br />         }<br /><br />                switch (strutState){<br />               case(HARD):<br />                         setLights(SportLight,HIGH);<br />                             setLights(TourLight,LOW);<br />                 break;<br /><br />               case(MEDIUM):<br />                         setLights(SportLight,HIGH);<br />                             setLights(TourLight,HIGH);<br />                 break;<br /><br />               case(SOFT):<br />                         setLights(SportLight,LOW);<br />                             setLights(TourLight,HIGH);<br />                 break;<br />                 <br />               case(UNKNOWN):<br />                         setLights(SportLight,LOW);<br />                             setLights(TourLight,LOW);<br />                 break;<br />        }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Motors<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeMotorIOPin(int pin){<br /><br />    // Set pin used for strut motor as output <br />    pinMode(pin, OUTPUT);<br /><br />        // Make sure Motor is off<br />    digitalWrite(pin,LOW);<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Switches<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeStrutIOPin(int pin){<br /><br />    // Set pin used for strut motor as output <br />    pinMode(pin, INPUT);<br /><br />        // Pull Pin HIGH         <br />    digitalWrite(pin,HIGH);<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set an LED  to a color by Address<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLED(int ledaddress, int color){<br />    //<br />        // rgbEncode(int red,int green,int blue)<br />    // red,green,blue =  0, 255<br />    //<br /><br />    switch(color){<br />        case D_HARD:<br />            setLedColors(ledaddress,ledHardValue);<br />                break;    <br />        case D_MEDIUM:<br />            setLedColors(ledaddress,ledMediumValue);<br />                break;    <br />        case D_SOFT:<br />            setLedColors(ledaddress,ledSoftValue);<br />                break;    <br />        case D_RED:<br />            setLedColors(ledaddress,ledRedValue);<br />                break;    <br />        case D_GREEN:<br />            setLedColors(ledaddress,rgbEncode(0,10,0));<br />                break;    <br />        case D_BLUE:<br />            setLedColors(ledaddress,rgbEncode(0,0,25));<br />                break;    <br />        case D_TURQUOIS:<br />            setLedColors(ledaddress,rgbEncode(0,40,64));<br />                break;    <br />        case D_PURPLE:<br />            setLedColors(ledaddress,rgbEncode(10,0,10));<br />                break;    <br />        case D_YELLOW:<br />            setLedColors(ledaddress,rgbEncode(10,10,0));<br />                break;<br />        case D_STARTUP:<br />            setLedColors(ledaddress,rgbEncode(2,2,2));<br />                break;    <br />        case D_WHITE:<br />            setLedColors(ledaddress,rgbEncode(15,15,15));<br />                break;    <br />        case D_OFF:<br />            setLedColors(ledaddress,rgbEncode(0,0,0));<br />                break;    <br /><br />             <br />        }<br />         // LEDS.show(); //PUSH the current color definitions of ALL the LEDs out<br />         // delayMicroseconds(600); //Wait for 200us to go into reset<br />     <br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to turn an LED off<br />//////////////////////////////////////////////////////////////////////////////////////<br />void offLED(int ledaddress){<br />        <br />    setLedColors(ledaddress,LEDOFF);<br />        LEDS.show(); //PUSH the current color definitions of ALL the LEDs out<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set either the Tour Or Sport light to an HIGH/ON or LOW/OFF state<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLights(int light, int state){<br />  digitalWrite(light,state);<br />        <br />}<br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />// Reference 5v = 1023<br />//<br />// Hard     0.0v     / xxx<br />//    Cutoff  0.415  / 85<br />// Medium   0.83v    / xxx<br />//    Cutoff   1.245   / 255<br />// Soft     1.66v    / xxx<br />//    Cutoff   2.075   / 424<br />// Diag     2.5      / xxx<br />//    Cutoff   2.915   / 596<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readSelectorSwitch(){<br />    int voltage;<br />    int voltage2;<br />    float voltdiff;<br /><br />    // Based on the selector switch type do the appropriate read<br />    if(selectorType == ROTARY){<br />        <br />       voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />       delay(500);                               // debounce for 500 msec<br />       voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />       voltdiff = abs(1.0-voltage/voltage2);     // Calculate the percentage difference <br /><br />       //<br />       // as long as voltage difference &gt; 10% keep reading<br />       //<br />       while(voltdiff &gt; .1 ){<br />          voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />          delay(500);                             // debounce for 500 msec<br />          voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />          voltdiff = abs(1.0-voltage/voltage2);   // Calculate the percentage difference <br />       }<br /><br />           //<br />       // based on the converted voltage value and the range return the value<br />           //<br />       if(voltage &lt; 85){                          <br />             // Selector is in HARD mode <br />             if (currentSelectorState == HARD) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP ) currentSelectorState =HARD;<br />           strutState = HARD;<br />               return(HARD);<br />         }<br />     <br />       }else if (voltage &gt;= 85 &amp;&amp; voltage &lt; 255){ <br />         // Selector is in MEDIUM mode <br />             if (currentSelectorState == MEDIUM) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP ) currentSelectorState = MEDIUM;<br />               strutState = MEDIUM;<br />           return(MEDIUM);<br />         }<br /><br />       }else if (voltage &gt;= 255 &amp;&amp; voltage &lt; 424){<br />             // Selector is in SOFT mode <br />             if (currentSelectorState == SOFT) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP ) currentSelectorState = SOFT;<br />               strutState = SOFT;<br />           return(SOFT);<br />         }<br /><br />       }else if(voltage &gt;= 424 &amp;&amp; voltage &lt;596){<br />             // Selector is in DIAG mode <br />             if (currentSelectorState == DIAG) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP) currentSelectorState = DIAG;<br />               strutState = DIAG;<br />           return(DIAG);<br />         }<br /><br />       }else{<br />              strutState=UNKNOWN;<br />          return (UNKNOWN);<br /><br />       }<br /><br />    }else if (selectorType == PUSHBUTTON){<br />            // if they are pressing switch i.e. LOW then decide what's next<br />            if(digitalRead(ModeSelectSwitch) == LOW){<br />                  delay(1000);<br />          //<br />          // If they are no longer pressing switch then shift the struts to next state<br />          //<br />                  if(digitalRead(ModeSelectSwitch) == HIGH){<br />                        switch(strutState){<br />                           case HARD:<br />                             currentSelectorState = MEDIUM;<br />                             strutState = MEDIUM;<br />                             return(MEDIUM);<br />                           break;<br /><br />                           case MEDIUM:<br />                             currentSelectorState = SOFT;<br />                     strutState = SOFT; <br />                             return(SOFT);<br />                           break;<br /><br />                           case SOFT:<br />                             currentSelectorState = HARD;<br />                     strutState = HARD; <br />                             return(HARD);<br />                           break;<br />               <br />               case READDIAG:<br />               case DIAG:<br />               case UNKNOWN:<br />                             currentSelectorState = DIAG;<br />                     strutState = HARD; <br />                             return(HARD);<br />                           break;<br />                   }<br />            }else{<br />             //<br />             // They are still holding button down wait 3 secs<br />                     delay(3000);<br /><br />             //If they are still holding the switch after 3secs - see if they want DIAGNOSTIC or READ DIAG mode<br />                     if(digitalRead(ModeSelectSwitch) == LOW){ <br />                         blinkBothLights(3,150,2,1000); // Blink both Lights to let them know they are at this mode<br />                         setLED(LED1,D_OFF);<br />                         setLED(LED2,D_OFF);<br />                         setLED(LED3,D_OFF);<br />                         setLED(LED4,D_OFF);<br />                         LEDS.show();<br />                 delay (3000);<br />        <br /> <br />                         if(digitalRead(ModeSelectSwitch) == LOW){ <br />                              //<br />                              // Blink both Lights to let them know they are at this mode<br />                  // They should let go now after seeing blinks if they want to run diag mode<br />                  // <br />                               blinkBothLights(3,150,2,1000); // Blink both Lights to let them know they are at this mode<br />                          <br />                   // Let them know it's over<br />                               setLED(LED1,D_WHITE);<br />                               setLED(LED2,D_WHITE);<br />                               setLED(LED3,D_WHITE);<br />                               setLED(LED4,D_WHITE);<br />                               LEDS.show();<br />                       delay (3000);        <br /><br />                     //If they let go before 3secs - then they want DIAGNOSTIC <br />                             if(digitalRead(ModeSelectSwitch) == HIGH){ <br />                     // Ok they let go they want to Run Diagnostic Mode<br />                                strutState = DIAG; <br />                                return(strutState);<br />                 } else return (strutState);<br />                 <br />             }else {<br />                // Ok they let go they want to Read the current Diagnostic Codes<br />                            strutState = READDIAG;<br />                            return(strutState);<br />             }<br /><br />             } else return (strutState);   //Nope they dont' want anything - just return current State.<br />               }<br />           <br />       }else if (currentSelectorState == DIAG &amp;&amp; strutState == HARD){<br />           // if here then this is 2nd pass thru just after comming out of DIAG or READDIAG MODE<br />               // Switch was not being pressed and last state was DIAG mode Now RETURN HARD to MAIN loop one more time<br />               currentSelectorState = HARD;<br />               return(HARD);<br />       } else return(NOCHANGE);  // Switch is not being pressed - return NOCHANGE state<br />    }<br />}<br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />// Reference 5v = 1023<br />//<br />// Hard     0.0v     / xxx<br />//    Cutoff  0.415  / 85<br />// Medium   0.83v    / xxx<br />//    Cutoff   1.245   / 255<br />// Soft     1.66v    / xxx<br />//    Cutoff   2.075   / 424<br />// Diag     2.5      / xxx<br />//    Cutoff   2.915   / 596<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readDiagSelectorSwitch(){<br />    int voltage;<br />    int voltage2;<br />    float voltdiff;<br /><br />    // Based on the selector switch type do the appropriate read<br />    if(selectorType == ROTARY){<br />        <br />       voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />       delay(500);                               // debounce for 500 msec<br />       voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />       voltdiff = abs(1.0-voltage/voltage2);     // Calculate the percentage difference <br /><br />       //<br />       // as long as voltage difference &gt; 10% keep reading<br />       //<br />       while(voltdiff &gt; .1 ){<br />          voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />          delay(500);                             // debounce for 500 msec<br />          voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />          voltdiff = abs(1.0-voltage/voltage2);   // Calculate the percentage difference <br />       }<br />           currentSelectorState = DIAG;<br /><br />           //<br />       // based on the converted voltage value and the range return the value<br />           //<br />       if(voltage &lt; 85) return (HARD) ;<br />       else if (voltage &gt;= 85 &amp;&amp; voltage &lt; 255)  return (MEDIUM) ;<br />       else if (voltage &gt;= 255 &amp;&amp; voltage &lt; 424) return (SOFT);<br />       else if(voltage &gt;= 424 &amp;&amp; voltage &lt;596) return (NOCHANGE);<br />       else return (UNKNOWN);<br /><br /><br />    }else if (selectorType == PUSHBUTTON){<br />            // if they are pressing switch i.e. LOW then decide what's next<br />            if(digitalRead(ModeSelectSwitch) == LOW){<br />                 blinkBothLights(3,150,2,1000); // Blink both Lights to let them know they are at this mode<br />         delay (2000);<br />          //<br />          // they are pressing switch then shift the struts out of DIAG mode to HARD <br />          //<br />                  currentSelectorState = DIAG;<br />          strutState = HARD; <br />                  return(HARD);<br />            }else return (NOCHANGE);<br />         }<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Read actual status of strut , set LED color apprropriately and return value<br />//<br />//<br />///////////////////////////////////////////////////////////////////////////////////////<br />int readstrut(int strutS1,int strutS2){<br />  int switch1;<br />  int switch2;<br /><br /><br />  switch2 = !digitalRead(strutS1);<br />  switch1 = !digitalRead(strutS2);<br />  <br />  if (switch1 == LOW &amp;&amp; switch2 == HIGH){<br />    // Strut is in HARD mode<br />    return(HARD);<br />    <br />  }else if (switch1 == HIGH &amp;&amp;  switch2 == HIGH){<br />    // Strut is in MEDIUM mode  <br />    return(MEDIUM);<br />    <br />  }else if (switch1 == HIGH &amp;&amp; switch2 == LOW){<br />    // Strut is in SOFT Mode<br />    return(SOFT);<br />  }else {<br />    return(UNKNOWN);<br />  }<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Read actual status of strut , set LED color apprropriately and return value<br />//<br />//<br />///////////////////////////////////////////////////////////////////////////////////////<br />int displayStrut(int strutS1,int strutS2,int strutLED,int displayMode){<br />  int switch1;<br />  int switch2;<br />  <br />  int switch1b;<br />  int switch2b;<br /><br />  int timeout = 0;<br />  int numberTries = 0;<br />/*<br />  unsigned long timestart;<br />  unsigned long timenow;<br />  timestart = millis();<br />  */<br /><br />  //<br />  // intial switch reads<br />  //<br />  switch2 = !digitalRead(strutS1);<br />  switch1 = !digitalRead(strutS2);<br />  delayMicroseconds(50); //Wait for 100us to debounce contacts<br />  switch2b = !digitalRead(strutS1);<br />  switch1b = !digitalRead(strutS2);<br /><br /><br />  //<br />  // See if the switches are reading the same<br />  //<br />  while ((switch2 != switch2b || switch1 != switch1b)&amp;&amp; numberTries &lt; 40){<br />      switch2 = !digitalRead(strutS1);<br />      switch1 = !digitalRead(strutS2);<br />      delayMicroseconds(50); //Wait for 50us to debounce contacts<br />      switch2b = !digitalRead(strutS1);<br />      switch1b = !digitalRead(strutS2);<br />      numberTries++;<br />      <br />  }<br />  <br /> <br />  if (switch1 == LOW &amp;&amp; switch2 == HIGH &amp;&amp; numberTries &lt;40 ){<br />    // Strut is in HARD mode<br />    setLED(strutLED, D_HARD);<br />    LEDS.show();<br />    return(HARD);<br />    <br />  }else if (switch1 == HIGH &amp;&amp;  switch2 == HIGH &amp;&amp; numberTries &lt;40 ){<br />    // If Strut is in MEDIUM mode  <br />    setLED(strutLED, D_MEDIUM);<br />    LEDS.show();<br />    return(MEDIUM);<br />    <br />  }else if (switch1 == HIGH &amp;&amp; switch2 == LOW &amp;&amp; numberTries &lt;40 ){<br />    // Strut is in SOFT Mode<br />    setLED(strutLED, D_SOFT);<br />    LEDS.show();<br />    return(SOFT);<br />  }else {<br />    if(displayMode == ERROR) setLED(strutLED, D_RED);<br />    else setLED(strutLED, D_OFF);<br />    LEDS.show();<br />    return(UNKNOWN);<br />  }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Set the desired strut to a particular mode        <br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />int setStrutMode( int selectorDesiredMode, int strutDesiredMode, int strutMotor, int strutSwitch1, int strutSwitch2,int displayLED, int *strutErrorStatus){<br />  int tryCount;<br />  tryCount = 0;<br /><br />   //  SIGNAL_WIRE_FAILURE   <br />   //  MOTOR_COMMAND_FAILURE <br />   //  NO_FAILURES <br /><br />                //<br />                // Reset the Error Flag since we are now allowing a retry for each strut.<br />                //<br />                *strutErrorStatus = NO_FAILURES;<br /><br />                //<br />                // Enable MotorChip<br />                //<br />                digitalWrite(MotorEnable,HIGH);<br /><br /><br />                //<br />        // IF any previous failures - display status &amp; kick out <br />        //<br />                /***** This code disabled as of firmware version 1.10 **************<br />                if (*strutErrorStatus == MOTOR_COMMAND_FAILURE ){<br />                        setLED(displayLED,D_RED);<br />                        LEDS.show();<br />            return(ERROR);<br />        }else if (*strutErrorStatus == SIGNAL_WIRE_FAILURE){<br />                        setLED(displayLED,D_YELLOW);<br />                        LEDS.show();<br />            return(ERROR);<br />        }<br />                ********************************************************************/<br /><br />                //<br />        // ok so no previous problems with this strut - get readay to set to new mode<br />                // <br />        <br />                <br />                //<br />           // Turn on motor as long as strut is not in desired mode but only try the loop for MAXTRIES number of times<br />                //<br />                 while( displayStrut(strutSwitch1, strutSwitch2,displayLED,NOERROR) != strutDesiredMode &amp;&amp;<br />               tryCount &lt; MAXTRIES ){<br />            <br />                digitalWrite(strutMotor, HIGH);<br />                  tryCount++;<br />            }<br />        <br />            // run the motors for 5 msecs more to force strut further into this zone.<br />            delay(5);<br />        <br />        // Turn the Motor Off<br />            digitalWrite(strutMotor, LOW);<br />                    <br />                 // if  MaxTries not exceeded strut is now in desired mode<br />          if(tryCount &lt; MAXTRIES ){<br /><br />                     // Strut is now in desired mode<br />                     *strutErrorStatus = NO_FAILURES;<br />                     return (NOERROR);<br /><br />                }else {<br />            //<br />            // Maxtries was exceeded - display RED for 2 secs turn off for 1 sec &amp; try again.<br />            //<br />                    setLED(displayLED,D_RED);<br />                    LEDS.show();<br />                    delay(1000);<br />                    setLED(displayLED,D_OFF);<br />                    LEDS.show();<br /><br />            //<br />            // Try One More Time<br />            // <br />            tryCount = 0;<br /><br /><br />                     while( displayStrut(strutSwitch1, strutSwitch2,displayLED,NOERROR) != strutDesiredMode &amp;&amp; <br />              tryCount &lt; MAXTRIES ) {<br />                  digitalWrite(strutMotor, HIGH);<br />                    tryCount++;<br />                }<br /><br />                // run the motors for 33msecs more to force strut further into this zone.<br />                    delay(33);<br /><br />            // Turn the Motor Off<br />                digitalWrite(strutMotor, LOW);<br />    <br />              if(tryCount &lt; MAXTRIES){<br />                         // Strut Managed to get into desired mode on 2nd try<br />                        *strutErrorStatus = NO_FAILURES;<br />                         return (NOERROR);<br />              }else{<br />               //<br />               // Maxtries was exceeded - Flag this strut as bad.<br />               //<br />                        setLED(displayLED,D_RED);<br />                        LEDS.show();<br />                        *strutErrorStatus = MOTOR_COMMAND_FAILURE;<br />                        return (ERROR);<br />              }<br />        }<br />         <br />}<br /><br />//*******************************************************************************************<br />//<br />// Diagnostic routine<br />//<br />//<br />//*******************************************************************************************<br />void runDiagnostic(){<br />  <br />             while(readDiagSelectorSwitch()== NOCHANGE){ <br />                   errorFR=0;<br />                   errorFL=0;<br />                   errorRR=0;<br />                   errorRL=0;<br />                   <br />                   // Turn Off All LEDS<br />                   offLED(LED1);<br />                   offLED(LED2);<br />                   offLED(LED3);<br />                   offLED(LED4);<br />                   <br />                   // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />                   delay(1000);<br />  <br />                   // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST FRONT LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           <br />           <br />                   //SET LED 1 to White to indicate testing Front Left Strut <br />                   setLED(LED1,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 1 time as group 4 times To indicate FL strut<br />                   blinkLights(SportLight, 1,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Front Left Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />                                      <br /><br />                      //Attempt to set  Front Left Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Front Left Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorFL == 0){<br />                       setLED(LED1,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED1,D_RED);<br />                      LEDS.show();<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br /><br />                   <br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST FRONT Right STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 2 to White to indicate testing Front Right Strut <br />                   setLED(LED2,D_WHITE);<br />                   LEDS.show();<br />             <br />           // Blink Sport Light 2 time as group 3 times To indicate FR strut  <br />                   blinkLights(SportLight, 2,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Front Right Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />                      //Attempt to set  Front Right Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Front Left Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br />              // Turn off Lights<br />              setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorFR == 0){<br />                       setLED(LED2,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED2,D_RED);<br />                      LEDS.show();<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST REAR LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 3 to White to indicate testing Front Left Strut <br />                   setLED(LED3,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 3 time as group 3 times To indicate RL strut <br />                   blinkLights(SportLight, 3,300,4,1000); <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Rear Left Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br />                      //Attempt to set  Rear Left Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Rear Left Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />              // Turn off Lights<br />              setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorRL == 0){<br />                       setLED(LED3,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED3,D_RED);<br />                      LEDS.show();<br /><br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST REAR RIGHT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 1 to White to indicate testing Rear Right Strut <br />                   setLED(LED4,D_WHITE);<br />                   LEDS.show();<br />            <br />           // Blink Sport Light 4 time as group 3 times To indicate RR strut <br />                   blinkLights(SportLight, 4,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Rear Right Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />                      //Attempt to set  Rear Right Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Rear Right Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />              // Turn off Lights<br />              setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorRR == 0){<br />                       setLED(LED4,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED4,D_RED);<br />                      LEDS.show();<br />           }<br /><br />           <br />           <br />                   delay(2000);<br />          }<br />          return; <br /><br />}<br />//*******************************************************************************************<br />//<br />//Read Diagnostic routine<br />//This routine displays the current error status of all the struts based on current statuses incurred<br />//this is mainly for Cars that have Sports Tour Lights Only<br />//<br />//*******************************************************************************************<br />void readDiagnostics(){<br />                   <br />                   //  Possible Error Codes for errorFL, errorFR, errorRL, errorRR  =<br />                   //    <br />                   //         SIGNAL_WIRE_FAILURE   <br />                   //         MOTOR_COMMAND_FAILURE <br />                   //         NO_FAILURES <br />                   //<br /><br />         while( readDiagSelectorSwitch()== NOCHANGE){  <br /><br /><br />                   // Turn Off All LEDS<br />                   offLED(LED1);<br />                   offLED(LED2);<br />                   offLED(LED3);<br />                   offLED(LED4);<br />                   <br />                   // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />                   delay(1000);<br />             <br />                   // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />  <br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT FRONT LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           <br />           <br />           // Blink Sport Light 1 time as group 4 times To indicate FL strut<br />                   blinkLights(SportLight, 1,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br /><br />           // Display Final test results for this Strut<br />           if (errorFL == NO_FAILURES){<br />                       setLED(LED1,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorFL == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED1,D_RED);<br />                      LEDS.show();<br /><br />           }else if (errorFL == SIGNAL_WIRE_FAILURE){ <br />                  setLED(LED1,D_YELLOW);<br />                      LEDS.show();<br />                   for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br /><br />                   <br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT FRONT Right STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 2 to White to indicate testing Front Right Strut <br />                   setLED(LED2,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 2 time as group 3 times To indicate FR strut  <br />                   blinkLights(SportLight, 2,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br /><br />           // Display Final test results for this Strut<br />           if (errorFR == NO_FAILURES){<br />                       setLED(LED2,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorFR == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED2,D_RED);<br />                      LEDS.show();<br />                      delay(2500);<br /><br />           }else if (errorFR == SIGNAL_WIRE_FAILURE){ <br />                    setLED(LED2,D_YELLOW);<br />                      LEDS.show();<br />              for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br /><br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT REAR LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 3 to White to indicate testing Front Left Strut <br />                   setLED(LED3,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 3 time as group 3 times To indicate RL strut <br />                   blinkLights(SportLight, 3,300,3,1000); <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br /><br />           // Display Final test results for this Strut<br />           if (errorRL == NO_FAILURES){<br />                       setLED(LED3,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorRL == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED3,D_RED);<br />                      LEDS.show();<br />                      delay(2500);<br /><br />           }else if (errorRL == SIGNAL_WIRE_FAILURE){<br />                   setLED(LED3,D_YELLOW); <br />                      LEDS.show();<br />               for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br /><br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT REAR RIGHT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 1 to White to indicate testing Rear Right Strut <br />                   setLED(LED4,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 4 time as group 3 times To indicate RR strut <br />                   blinkLights(SportLight, 4,300,3,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />           // Display Final test results for this Strut<br />           if (errorRR == NO_FAILURES){<br />                       setLED(LED4,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorRR == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED4,D_RED);<br />                      LEDS.show();<br />                      delay(2500);<br /><br />           }else if (errorRR == SIGNAL_WIRE_FAILURE){<br />                   setLED(LED4,D_YELLOW); <br />                      LEDS.show();<br />              for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br />            <br />                   delay(2000);<br />          }<br />          return;<br />}<br /><br />//**************************************************************************<br />// this routine converts three seperate RGB values in to a correctly encoded RGB integer<br />//**************************************************************************<br />long rgbEncode(int red,int green,int blue){<br /> long rgb;<br /><br /> //if (red &gt; 255) red=255;<br /> //if (green &gt; 255) green=255;<br /> //if (blue &gt; 255) blue=255;<br /><br /> rgb = red*65536 + green*256 + blue;<br /><br /> return (rgb);<br /> <br />} <br /><br /><br /><br />/****************************************************************************************<br /> *<br /> *<br /> * Routine to read int from EProm<br /> *<br /> *<br /> ****************************************************************************************/<br />int eepromReadInt(int address){<br />   int value = 0x0000;<br />   value = value | (EEPROM.read(address) &lt;&lt; 8);<br />   value = value | EEPROM.read(address+1);<br />   return value;<br />}<br /><br />/****************************************************************************************<br /> *<br /> *<br /> * Routine to Write int to EProm<br /> *<br /> *<br /> ****************************************************************************************/<br />void eepromWriteInt(int address, int value){<br />   EEPROM.write(address, (value &gt;&gt; 8) &amp; 0xFF );<br />   EEPROM.write(address+1, value &amp; 0xFF);<br />}<br /><br />//****************************************************<br />//<br />// Convert Desired HEX color into  rgb values and store in LED array<br />//<br />//****************************************************<br /><br />void setLedColors(int LED,long hexValue) {<br /><br />  ledColors[LED].r = ((hexValue &gt;&gt; 16) &amp; 0xFF) ;  // Extract the RR byte<br />  ledColors[LED].g = ((hexValue &gt;&gt; 8) &amp; 0xFF) ;   // Extract the GG byte<br />  ledColors[LED].b = ((hexValue) &amp; 0xFF) ;        // Extract the BB byte<br /><br />}<br /><br /></pre> <pre>//****************************************************************************<br />//<br />//     Description of Software:    <br />//<br />//     Mitsubishi 3000gt/Stealth Custom Controller Program<br />//     for controlling stock OEM mitsubishi ECS Struts.<br />//     Hardware Platform:   ATAMega 328 MicroController<br />//<br />//     Copyright (C) 2014  Marcus Diaz, RenegadeTechWorks, LLC<br />//<br />//****************************************************************************<br />// Licensing:<br />//              Licensed under GNU GPL Version 2<br />//<br />//    This program is free software; you can redistribute it and/or modify<br />//    it under the terms of the GNU General Public License as published by<br />//    the Free Software Foundation under Version 2 of the License<br />//<br />//    This program is distributed in the hope that it will be useful,<br />//    but WITHOUT ANY WARRANTY; without even the implied warranty of<br />//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br />//    GNU General Public License for more details.<br />//<br />//    You should have received a copy of the GNU General Public License along<br />//    with this program; if not, write to the Free Software Foundation, Inc.,<br />//    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />//****************************************************************************<br />// VERSION HISTORY<br />// --------------------------------------------------------------------<br />// SW Ver  :  v1.0<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      09/15/2012<br />// Comments:  Production version of code based on branch from original<br />//            prototype controller <br />//            Diag Mode working - this version disables Select SW. <br />//            check during Diag Mode Execution.'<br />//            This overcomes Voltage Drop.<br />//<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.1<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/7/2012<br />// Comments:  Production version of code based on branch from original<br />//            prototype controller <br />//            -v1.1 uses a Jumper accross Pins 6 &lt;--&gt; Pin3 on the<br />//             program header to indicate what type of selector switch<br />//             is being used as follows:<br />//                Jumper On : USE push Button ECS Switch<br />//                Jumper Off: Use Rotatry 4 Position Selector Switch<br />//            -v1.1 Has implemented the new re-written Diagonstic seq.<br />//                for using both LED's and OEM Tour/Sport Lights<br />//            -v1.1 also implements Diagnostic mode interrupt code<br />//            -v1.1 Implements a Dimming control option. To use at <br />//                 start up put the system in Diagnostic Mode using<br />//                 using either the stock ECS switch by pressing and<br />//                 holding or Rotary switch to position 4. <br />//*******************************************************************<br />// SW Ver  :  v1.1<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/12/2012<br />// Comments:  Base  SVN production version of the code.<br />//            <br />//          Production version of code based on branch from original<br />//            prototype controller <br />//            -v1.1 uses a Jumper accross Pins 6 &lt;--&gt; Pin3 on the<br />//             program header to indicate what type of selector switch<br />//             is being used as follows:<br />//                Jumper On : USE push Button ECS Switch<br />//                Jumper Off: Use Rotatry 4 Position Selector Switch<br />//            -v1.1 Has implemented the new re-written Diagonstic seq.<br />//                for using both LED's and OEM Tour/Sport Lights<br />//            -v1.1 also implements Diagnostic mode interrupt code<br />//            -v1.1 Implements a Dimming control option. To use at <br />//                 start up put the system in Diagnostic Mode using<br />//                 using either the stock ECS switch by pressing and<br />//                 holding or Rotary switch to position 4. <br />//<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.2<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/13/2012<br />// Comments:  <br />//            added 2000 msec delay in diagonstic routine during display<br />//            of sucessfull strut test when both Tour &amp; Sport lights<br />//            are turned on.            <br />//            <br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.3<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/23/2012<br />// Comments:  <br />//          - Corrected all code paths to correctly handle UNKNOWN Strut State<br />//          - Made Setup initialization routine more robust - all struts must aggree<br />//          - Also during setup Tour/Sport will go into error mode if there<br />//            was disagreement<br />//          - added displayStrut as derivative of original readstrut()<br />//          - doubled MAXTRIES to 10000 - helps with laggy struts <br />//          - fixed bug in readSelector() in case of PUSHBUTTON - was <br />//            missing " else return (strutState)" at end<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.5<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      10/31/2012<br />// Comments:  <br />//          - Baselining this as the final production version for the first production<br />//            run. <br />//          - Other than these comments there are no diffs between this and <br />//          - the previous verson<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.6<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/13/2014<br />// Comments:  <br />//          - Added saftey code to detect intermittent broken signal wire<br />//            and prevent motor driver burn out<br />//          - When broken wire detected - Both Tour Sport Lights will alternately flash<br />//          -added new diagnostic read status mode (only available from pushbutton)<br />//          -added new startup &amp; error sequencing display routines for Sport/Tour Lights<br />//          -LEDs now Have Yellow Error Condition = Signal Wire Failure<br />//          -LEDs Red Error Condition = Motor command sequence failure.<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.7<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/13/2014<br />// Comments:  <br />//          -Added EProm Read/Write Code during startup to retrieve/store LED <br />//           brigtness defaults<br />//          -Increased MaxTries by 20% from 10000 to 12000<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.7<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/17/2014<br />// Comments:  <br />//          -Minor display tweek for Diagnostic display of Sport / Tourlights<br />//           So that they just blink 3 times in groups of 3 regardless of which <br />//           strut is being reported for a Motor Command Failure<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.7a<br />// HWDesign:  ECM M01 PCB Rev1.1<br />// Date:      4/17/2014<br />// Comments:  <br />//          -increased MaxTries to 15000 from 12000<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.8<br />// HWDesign:  ECM M01 PCB Rev1.1,1.2<br />// Date:      12/28/2014<br />// Comments:  <br />//          -Replaced LED CODE to use FAST SPI LED Library <br />//          -Now supports WS2801 &amp; WS2812b &amp; Others<br />//          -Added code to read Pin Jumper on Pins 2 to 3.<br />//          -If Pin 2 Jumpered to ground/Pin3 Code will use WS2801 LED <br />//           IF Pin 2 not Jumpered to Ground/ Pin 3 Code will use WS2812b LED <br />//<br />//          -In displayStrut - Added setLED(x,D_OFF)to Case Unknown to make<br />//           timed loop equal for all cases while Maxtries is incrementing<br />//          --NOTE 1.8a &amp; 1.8 have a timming issue with WS2801 LEDs.<br />//           all other code fine.<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.8a<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      1/1/2015<br />// Comments:  <br />//          -removed led.Show() call from setLed()<br />//          -added led.Show() in all places needed<br />//          -NOTE 1.8a &amp; 1.8 have a timming issue with WS2801 LEDs.<br />//           all other code fine.    <br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.9<br />// HWDesign:  ECM M01 PCB Rev1.1,1.2<br />// Date:      2/18/2015<br />// Comments:  <br />//          -removed all delays that were added to try and force sync up<br />//          -upgraded to fastLED lib 3.0 (fixed sync problem with WS2801's)<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.9a<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      2/18/2015<br />// Comments:  <br />//          -added 1 millesecond delay to Motor enable drive loop to fix<br />//           problem with motor turning of too soon on the edge case<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.10<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      8/02/2015<br />// Comments:  <br />//          -setStrutMode() added 33 msec delay just outside of Motor While loop to<br />//           force motor to run 33 msec longer.  Forces overrun of switch target<br />//          -setStrutMode() removed saftey checks for previous strut errors, code <br />//           will now retry a strut even if in previous cycle it had any kind of error.<br />//          -increased MaxTries to 7194(2.0 secs) from 2000<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.11<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      8/09/2015<br />// Comments:  <br />//          -displayStrut() <br />//             -added debounce algorithm to ignore S1 S2 transients<br />//              less than X microseconds apart.<br />//             -added timeout code to abort after Y milleseconds of trying<br />//<br />//--------------------------------------------------------------------<br />// SW Ver  :  v1.12<br />// HWDesign:  ECM M01 PCB Rev 1.1,1.2<br />// Date:      8/09/2015<br />// Comments:  <br />//          -Removed MotorEnable(motorx,LOW) commands all placess except Startup<br />//          -Set Motor Delay overun to 5msec instead of 33msec<br />//<br />//*****************************************************************************<br /><br />#include &lt;EEPROM.h&gt;<br />#include &lt;FastLED.h&gt;<br /><br />#define strutFR 1<br />#define strutFL 2<br />#define strutRR 3<br />#define strutRL 4<br /><br />#define HARD     1<br />#define MEDIUM     2<br />#define SOFT     3<br />#define DIAG    4<br />#define READDIAG 5<br />#define UNKNOWN 7<br />#define NOCHANGE 10<br />#define STARTUP  11<br /><br />#define D_RED       1<br />#define D_GREEN       2<br />#define D_BLUE       3<br />#define D_TURQUOIS 4<br />#define D_PURPLE   5<br />#define D_YELLOW   6<br />#define D_STARTUP  7<br />#define D_HARD     8<br />#define D_MEDIUM   9<br />#define D_SOFT     10<br />#define D_WHITE    11<br />#define D_OFF      12 <br /><br /><br />#define RED100   0xFF0000 // Bright Red<br />#define GREEN100 0x00FF00 // Bright Green<br />#define BLUE100  0x0000FF // Bright Blue<br />#define RED001   0x010000 // Faint red<br />#define RED050   0x800000 // 1/2 red (0x80 = 128 out of 256)<br />#define RED025   0x300000 // 25% RED<br />#define GREEN050 0x008000 // half Green<br />#define GREEN025 0x003000 // 25% GREEN<br />#define BLUE050  0x000080 // half Blue<br />#define BLUE025  0x000030 // 25% BLUE<br />#define WHITE015 0x151515 // 15% White<br />#define WHITE010 0x101010 // 10% White<br />#define WHITE005 0x050505 // 05% White<br />#define WHITE001 0x020202 // 05% White<br />#define WHITE100 0xFFFFFF // 100% White<br /><br />#define LEDOFF           0x000000    <br />#define RED            0xFF0000 <br />#define ORANGE         0xFF5500<br />#define ORANGEYELLOW   0xFFA000<br />#define YELLOW         0xFFFF00<br />#define YELLOWGREEN    0xA0FF00<br />#define GREENYELLOW    0x50FF00<br />#define GREEN          0x00FF00<br />#define GREENCYAN      0x00FF4B<br />#define CYAN           0x00FFFF<br />#define CYANBLUE       0x00A0FF<br />#define BLUECYAN       0x005AFF  <br />#define BLUE           0x0000FF<br />#define BLUEVIOLET     0x4800FF<br />#define VIOLETBLUE     0x7500FF<br />#define MAGENTA        0xFF00FF<br />#define PINKDEEP       0xFF1493<br />#define PINKHOT        0xFF69B4<br />#define PINK           0xF3967A<br /> <br />#define VIOLET         0xEE82EE<br /><br /><br />#define ERROR 1<br />#define NOERROR 0<br /><br />#define SIGNAL_WIRE_FAILURE   2<br />#define MOTOR_COMMAND_FAILURE 1<br />#define NO_FAILURES 0<br /><br />#define MAXTRIES 7194    // maxtries = 7194 = max of 2 secs of Motor on<br /><br /><br />#define MAXLIGHTLOOPS 20<br /><br />#define ROTARY 1<br />#define PUSHBUTTON 2<br />#define SAMEMODE 1<br />#define NEXTMODE 2<br /><br />#define LED_LEVEL1 1       // minimum brightness level<br />#define LED_LEVEL2 2<br />#define LED_LEVEL3 3<br />#define LED_LEVEL4 4       // Maximum brightness level<br /><br />#define EPROM_LEDMODE 1    // address in EPROM for LEDMODE<br /><br />//<br />//  Allocate Structure For LED Strip<br />//<br />#define NUM_LEDS 4<br />struct CRGB ledColors[NUM_LEDS];<br /><br />int ledMode = LED_LEVEL2;<br /><br />int selectorType = ROTARY;  // selectorType captures what kind of selector switch is hooked up to unit<br /><br />// Previous State of Selector Switch<br />int currentSelectorState = STARTUP;<br /><br />int strutState = HARD;<br />int strutStateFL;<br />int strutStateFR;<br />int strutStateRL;<br />int strutStateRR;<br /><br />long ledHardValue ;<br />long ledMediumValue;<br />long ledSoftValue;<br />long ledRedValue;<br /><br /><br />int LED1 = 0;    // Front Left Strut LED<br />int LED2 = 1;    // Front Right Strut LED<br />int LED3 = 2;    // Rear Left Strut LED<br />int LED4 = 3;    // Rear Right Strut LED <br /><br />int errorFL = NO_FAILURES;<br />int errorFR = NO_FAILURES;<br />int errorRL = NO_FAILURES;<br />int errorRR = NO_FAILURES;<br /><br />int tryCount = 0;<br /><br />int lightLoopCounter = MAXLIGHTLOOPS;<br />int lightLoopCounterValue;<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//  Map Board I/O Pins to World<br />//<br />// /////////////////////////////////////////////////////////////////////////////////////<br />// Front Left Strut Pins<br />int strutFLswitch1 = 3; <br />int strutFLswitch2 = 2;<br />int strutFLmotor   = 10;<br /><br />// Front Right Strut Pins<br />int strutFRswitch1 = 5; <br />int strutFRswitch2 = 4;<br />int strutFRmotor   = 11;<br /><br />// Rear Left Strut Pins<br />int strutRLswitch1 = 7; <br />int strutRLswitch2 = 6;<br />int strutRLmotor   = 14;<br /><br />// Rear Right Strut Pins<br />int strutRRswitch1 = 9; <br />int strutRRswitch2 = 8;<br />int strutRRmotor   = 15;<br /><br />//Mode Selector Switch Pin<br />int ModeSelectSwitch = 17;<br /><br />//Switch Type Selector Option Pin<br />int switchTypeSelect = 1;<br /><br />//LED Type Selector Option Pin<br />int LEDTypeSelect = 0;<br /><br />//MotorEnable Pin<br />int MotorEnable = 16;<br /><br />//<br />// LED Light Ports <br />//<br />#define DATA_PIN 18    // Data pin that led data will be written out over<br />#define CLOCK_PIN 19   // Clock pin only needed for SPI based chipsets when not using hardware SP<br /><br />// Sport Light Port<br />int SportLight = 12; <br /><br />// Tour Light Port<br />int TourLight = 13;<br /><br /><br /><br /> <br />///////////////////////////////////////////////////////////////////////////////////////<br />// setup initializes startup <br />///////////////////////////////////////////////////////////////////////////////////////<br />void setup(){<br /><br />        int selectorValue;<br />        int diagLoopNumber;<br />  <br /><br />      // initalize MotorEnable Pin for digital output and disable motors<br />    pinMode(MotorEnable, OUTPUT);<br />    digitalWrite(MotorEnable,LOW);<br /> <br />        // Initialize the Sport &amp; Tour Light OutPut Pins<br />        pinMode(SportLight, OUTPUT); <br />        pinMode(TourLight, OUTPUT);<br /> <br />          // intialiaze all the motor out pins<br />        initializeMotorIOPin(strutFLmotor);<br />        initializeMotorIOPin(strutFRmotor);<br />        initializeMotorIOPin(strutRRmotor);<br />        initializeMotorIOPin(strutRLmotor);<br />    <br />        // Initialize all the strut input pins <br />     initializeStrutIOPin(strutFRswitch1);<br />     initializeStrutIOPin(strutFRswitch2);<br />     initializeStrutIOPin(strutFLswitch1);<br />     initializeStrutIOPin(strutFLswitch2);<br />     initializeStrutIOPin(strutRRswitch1);<br />     initializeStrutIOPin(strutRRswitch2);<br />     initializeStrutIOPin(strutRLswitch1);<br />     initializeStrutIOPin(strutRLswitch2);<br /> <br /><br /><br />    // analyze   LED  Pin To figure out what type of leds are being used <br />        // HIGH = WS2801   LOW = WS2812<br />        pinMode(LEDTypeSelect, INPUT);<br />        digitalWrite(LEDTypeSelect,HIGH); <br /><br />        // Read the pin to see if it's HIGH or LOW      <br />        if(digitalRead(LEDTypeSelect) == HIGH){<br />           FastLED.addLeds&lt;WS2812B, DATA_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />    }else{<br />            FastLED.addLeds&lt;WS2801, DATA_PIN, CLOCK_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />    }<br /><br />        // ************************************************************<br />        //<br />        // LED Setup &amp; Initialization<br />        //<br />        // ************************************************************<br />        // Uncomment one of the following lines for your leds arrangement.<br />        // FastLED.addLeds&lt;WS2801, DATA_PIN, CLOCK_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />        // FastLED.addLeds&lt;TM1803, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;TM1804, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;TM1809, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2811, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2812, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2812B, DATA_PIN, RGB&gt;(ledColors, NUM_LEDS);<br />        // FastLED.addLeds&lt;UCS1903, DATA_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;WS2801, RGB&gt;(ledColors, NUM_LEDS);<br />        // FastLED.addLeds&lt;SM16716, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;LPD8806, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;SM16716, DATA_PIN, CLOCK_PIN, RGB&gt;(leds, NUM_LEDS);<br />        // FastLED.addLeds&lt;LPD8806, DATA_PIN, CLOCK_PIN, RGB&gt;(leds, NUM_LEDS);<br />       <br />        // Clear Out the LED arrays<br />        memset(ledColors, 0,  NUM_LEDS * sizeof(struct CRGB));   <br />        LEDS.show(); //Push the current color frame to the LEDs<br /><br /><br />    // analyze Selector TYPE Input Pin To figure out if Rotary of Pushbutton is connected <br />        // HIGH = Rotary   LOW = PUSHBUTTON<br />        pinMode(switchTypeSelect, INPUT);<br />        digitalWrite(switchTypeSelect,HIGH); <br /><br />        // Read the pin to see if it's HIGH or LOW      <br />        if(digitalRead(switchTypeSelect) == LOW){<br />             selectorType = PUSHBUTTON;<br />             pinMode(ModeSelectSwitch, INPUT);<br />             digitalWrite(ModeSelectSwitch, HIGH);<br />             lightLoopCounterValue = 900; // the lightloopcounter is timing dependent based on selector type<br />             diagLoopNumber = 450;<br />        }else{<br />             selectorType = ROTARY;<br />             pinMode(ModeSelectSwitch, INPUT);<br />             digitalWrite(ModeSelectSwitch, LOW);<br />             lightLoopCounterValue = 60;  // the lightloopcounter is timing dependent based on selector type<br />             diagLoopNumber = 40;<br />        } <br />        lightLoopCounter = lightLoopCounterValue;<br /><br />    //<br />    // Retrieve LED Brigtness level from EPROM - if not within range set it to 2nd level and write it back        //<br />        ledMode = eepromReadInt(EPROM_LEDMODE);    <br />    //<br />    // Make sure the value is with in range<br />    //<br />    if (ledMode != LED_LEVEL1 &amp;&amp; ledMode != LED_LEVEL2 &amp;&amp; ledMode != LED_LEVEL3 &amp;&amp; ledMode != LED_LEVEL4){<br />        // It's not within range - set to LEVEL two and write it back<br />        ledMode = LED_LEVEL1;<br />            eepromWriteInt(EPROM_LEDMODE,ledMode);<br />    } <br /><br /><br />    //<br />    // Blink the Sport / Tour Lights at startup as test to driver to show they are working.<br />    //<br />        blinkBothLights(1,3000,1,800);<br />    <br />  <br />    //<br />    //SET the LEDs to what the EPROM Value Says<br />    //<br />    switch(ledMode){<br />        case LED_LEVEL1:<br />                     //SET the LEDs to Minimum Brightness Mode    <br />                         ledHardValue = rgbEncode(2,0,2);<br />                         ledMediumValue = rgbEncode(0,2,2);<br />                         ledSoftValue = rgbEncode(0,0,2);<br />                         ledRedValue = rgbEncode(2,0,0);<br /><br />            break;<br />        case LED_LEVEL2:<br />                     //SET the LEDs to 2nd to Minimum Brightness Mode    <br />                         ledHardValue = rgbEncode(10,0,10);<br />                         ledMediumValue = rgbEncode(0,10,5);<br />                         ledSoftValue = rgbEncode(0,0,10);<br />                         ledRedValue = rgbEncode(10,0,0);<br />            break;<br />        case LED_LEVEL3:<br />                     //SET the LEDs to Medium Brightness Mode    <br />                         ledHardValue = rgbEncode(70,0,70);<br />                         ledMediumValue = rgbEncode(0,70,35);<br />                         ledSoftValue = rgbEncode(0,0,70);<br />                         ledRedValue = rgbEncode(70,0,0);<br />            break;<br />        case LED_LEVEL4:<br />                     //SET the LEDs to Maximum Brightness Mode    <br />                         ledHardValue = rgbEncode(120,0,120);<br />                         ledMediumValue = rgbEncode(0,120,60);<br />                         ledSoftValue = rgbEncode(0,0,150);<br />                         ledRedValue = rgbEncode(120,0,0);<br />            break;<br />    }<br /><br />        //  <br />        //See if user wants to set brightness level of LEDs<br />        //<br />        // Let them know it's time &amp; wait 3 secs<br />        setLED(LED1, D_WHITE);<br />        setLED(LED2, D_WHITE);<br />        setLED(LED3, D_WHITE);<br />        setLED(LED4, D_WHITE);<br />        <br />        LEDS.show();<br />    <br />        delay(2000);<br /><br /><br /><br />    //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br />        selectorValue = readSelectorSwitch();<br />        if(selectorValue == READDIAG || selectorValue == DIAG ){<br /><br />            // ok..they want to adjust - put the leds into their medium value colors so they know<br />            strutState=DIAG;<br />            <br />        //Default the LEDs to MEDIUM Mode    <br />            ledHardValue = rgbEncode(10,0,10);<br />            ledMediumValue = rgbEncode(0,10,5);<br />            ledSoftValue = rgbEncode(0,0,10);<br />            ledRedValue = rgbEncode(10,0,0);<br />            strutState = MEDIUM;<br />  <br />            setLED(LED1, D_HARD);<br />            setLED(LED2, D_MEDIUM);<br />            setLED(LED3, D_SOFT);<br />            setLED(LED4, D_RED);<br />            LEDS.show();<br />                    <br />            // Loop for a while to let the adjust and then bail<br />        for(int i = 1; i &lt;diagLoopNumber; i++){<br />                  delay(20);<br />                   switch(readSelectorSwitch()){<br />                      case HARD:<br />                     //SET the LEDs to Minimum Brightness Mode    <br />                     ledMode = LED_LEVEL1;<br />                         ledHardValue = rgbEncode(2,0,2);<br />                         ledMediumValue = rgbEncode(0,2,2);<br />                         ledSoftValue = rgbEncode(0,0,2);<br />                         ledRedValue = rgbEncode(2,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />                      break;<br /><br />                      case MEDIUM:<br />                     //SET the LEDs to 2nd to Minimum Brightness Mode    <br />                     ledMode = LED_LEVEL2;<br />                         ledHardValue = rgbEncode(10,0,10);<br />                         ledMediumValue = rgbEncode(0,10,5);<br />                         ledSoftValue = rgbEncode(0,0,10);<br />                         ledRedValue = rgbEncode(10,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />                      break;<br /><br />                      case SOFT:<br />                     //SET the LEDs to Medium Brightness Mode    <br />                     ledMode = LED_LEVEL3;<br />                         ledHardValue = rgbEncode(70,0,70);<br />                         ledMediumValue = rgbEncode(0,70,35);<br />                         ledSoftValue = rgbEncode(0,0,70);<br />                         ledRedValue = rgbEncode(70,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />                      break;<br />               <br />              case DIAG:<br />                     //SET the LEDs to Maximum Brightness Mode    <br />                     ledMode = LED_LEVEL4;<br />                         ledHardValue = rgbEncode(120,0,120);<br />                         ledMediumValue = rgbEncode(0,120,60);<br />                         ledSoftValue = rgbEncode(0,0,150);<br />                         ledRedValue = rgbEncode(120,0,0);<br />  <br />                         setLED(LED1, D_HARD);<br />                         setLED(LED2, D_MEDIUM);<br />                         setLED(LED3, D_SOFT);<br />                         setLED(LED4, D_RED);<br />                         LEDS.show();<br />              break;<br />                 }<br />        }<br />        //<br />        // IF the new mode is not whats already in EProm Update EProm<br />        //<br />            if(ledMode != eepromReadInt(EPROM_LEDMODE)) eepromWriteInt(EPROM_LEDMODE,ledMode);<br />        }<br />        <br />    // Let them know it's over<br />        setLED(LED1,D_WHITE);<br />        setLED(LED2,D_WHITE);<br />        setLED(LED3,D_WHITE);<br />        setLED(LED4,D_WHITE);<br />        LEDS.show();<br />        delay(2000);<br />        <br />        // Set LEDS &amp; Lights to current state of Struts <br />        strutStateFL = displayStrut(strutFLswitch1,strutFLswitch2,LED1,ERROR);<br />        strutStateFR = displayStrut(strutFRswitch1,strutFRswitch2,LED2,ERROR);<br />        strutStateRL = displayStrut(strutRLswitch1,strutRLswitch2,LED3,ERROR);<br />        strutStateRR = displayStrut(strutRRswitch1,strutRRswitch2,LED4,ERROR); <br />        <br />        <br />        // IF the state of all the struts match <br />    // set strutState to whatever the Front Left Strut is reading     <br />    if(strutStateFL == strutStateFR &amp;&amp; <br />       strutStateFL == strutStateRL &amp;&amp;     <br />       strutStateFL == strutStateRR &amp;&amp;     <br />       strutStateFL != UNKNOWN ){<br />        <br />          strutState=strutStateFL;<br />          currentSelectorState = strutStateFL;<br />          <br />                  // Set the TourSport Lights to indicate initial setting <br />              setTourSportLights();<br />        }else{<br />         // not sure what state all struts are in. Arbitarily try and set to HARD<br />         // Let user try and move to different state<br />         strutState=HARD;<br />         currentSelectorState = HARD;<br />                 setStrutMode( HARD, HARD, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                 setStrutMode( HARD, HARD, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                 setStrutMode( HARD, HARD, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                 setStrutMode( HARD, HARD, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br /><br />                 setTourSportLights();<br />    }<br />}<br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />//<br />//            main loop  <br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />void loop()<br />{<br />  <br />   //<br />   // Make Sure the Strut Motors are OFF<br />   //<br />   digitalWrite(strutFRmotor, LOW);<br />   digitalWrite(strutFLmotor, LOW);<br />   digitalWrite(strutRRmotor, LOW);<br />   digitalWrite(strutRLmotor, LOW);<br /><br />   //  SIGNAL_WIRE_FAILURE   <br />   //  MOTOR_COMMAND_FAILURE <br />   //  NO_FAILURES <br /> <br />   <br />   delay(20);<br />   //<br />   // Read the state of the Switch<br />   //   <br />     switch(readSelectorSwitch()){<br />    case(NOCHANGE):<br />                // Set LEDS &amp; Lights to current state of Struts <br />                if (errorFL == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutFLswitch1,strutFLswitch2,LED1,ERROR)) {<br />            errorFL = SIGNAL_WIRE_FAILURE;<br />            setLED(LED1,D_YELLOW);<br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />                if (errorFR == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutFRswitch1,strutFRswitch2,LED2,ERROR)){<br />            errorFR = SIGNAL_WIRE_FAILURE;<br />                   setLED(LED2,D_YELLOW);<br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />                if (errorRL == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutRLswitch1,strutRLswitch2,LED3,ERROR)){<br />            errorRL = SIGNAL_WIRE_FAILURE;<br />                   setLED(LED3,D_YELLOW);<br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />                if (errorRR == NO_FAILURES &amp;&amp; currentSelectorState != displayStrut(strutRRswitch1,strutRRswitch2,LED4,ERROR)){<br />            errorRR = SIGNAL_WIRE_FAILURE;<br />            setLED(LED4,D_YELLOW); <br />                        LEDS.show();<br />                        // delayMicroseconds(800); //Wait for 800us to go into reset<br />        }<br /><br />        setTourSportLights();<br />                break;<br /><br />    case(HARD):<br /><br />                  // Front Right Strut <br />                setStrutMode( HARD, HARD, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                setStrutMode( HARD, HARD, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                setStrutMode( HARD, HARD, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                setStrutMode( HARD, HARD, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br /><br />        // Set the TourSport Lights to indicate if setting of all four was sucessfull or Error <br />        setTourSportLights();<br />        break;<br /><br />    case(MEDIUM):<br /><br />                setStrutMode( MEDIUM, MEDIUM, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                setStrutMode( MEDIUM, MEDIUM, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                setStrutMode( MEDIUM, MEDIUM, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                setStrutMode( MEDIUM, MEDIUM, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br /><br />        // Set the TourSport Lights to indicate if setting of all four was sucessfull or Error <br />        setTourSportLights();<br />        break;<br /><br />    case(SOFT):<br />                <br />                setStrutMode( SOFT, SOFT, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                setStrutMode( SOFT, SOFT, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                setStrutMode( SOFT, SOFT, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                setStrutMode( SOFT, SOFT, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);                 <br /><br />        // Set the TourSport Lights to indicate if setting of all four was sucessfull or Error <br />        setTourSportLights();<br />                break;<br /><br /><br /><br />    case(READDIAG):<br />                readDiagnostics();<br />                break;<br /><br />    case(DIAG):<br />                runDiagnostic();<br />                break;<br /><br />        case(UNKNOWN):<br />        setTourSportLights();<br />                break;<br /><br />       }<br /><br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink one of the lights<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkLights(int lightaddress, int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay){<br />    <br />    setLights(lightaddress,LOW);<br />    <br />    for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br />        <br />    for (int count = numberBlinks; count &gt; 0; count--){<br />       delay(blinkDelay);<br />       setLights(lightaddress,HIGH);<br />       delay(blinkDelay);<br />       setLights(lightaddress,LOW);<br />        }<br />    delay(groupDelay);<br />    }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink one of the lights<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkBothLights(int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay){<br />    <br />    // Turn off both Lights<br />    setLights(SportLight,LOW);<br />    setLights(TourLight,LOW);<br />    <br />    for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br />        <br />    for (int count = numberBlinks; count &gt; 0; count--){<br />       delay(blinkDelay);<br />           setLights(SportLight,HIGH);<br />           setLights(TourLight,HIGH);<br />       <br />       delay(blinkDelay);<br />           setLights(SportLight,LOW);<br />           setLights(TourLight,LOW);<br />        }<br />    delay(groupDelay);<br />    }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Set TourSport Lights to CorrectMode<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setTourSportLights(){<br /><br />                lightLoopCounter--;<br />                if(lightLoopCounter &lt;= 0){<br />                    lightLoopCounter = lightLoopCounterValue;<br />                    <br />  <br />                  // if there was a problem start flashing shit.. to the do sport/tourlight error thing<br />            if (errorFL == SIGNAL_WIRE_FAILURE || <br />                errorFR == SIGNAL_WIRE_FAILURE ||<br />                errorRL == SIGNAL_WIRE_FAILURE ||<br />                errorRR == SIGNAL_WIRE_FAILURE ){<br />               <br />                           for(int i=0;i&lt;4;i++){<br />                         setLights(SportLight,HIGH);<br />                             setLights(TourLight,LOW);<br />                 delay(250);<br />                         setLights(SportLight,LOW);<br />                             setLights(TourLight,HIGH);<br />                 delay(250);<br />                   }<br />                    }<br />                          <br />                    if(errorFL == MOTOR_COMMAND_FAILURE || <br />               errorFR == MOTOR_COMMAND_FAILURE ||<br />               errorRL == MOTOR_COMMAND_FAILURE ||<br />               errorRR == MOTOR_COMMAND_FAILURE ) {<br />                            blinkBothLights(1,300,3,1000);<br />                           // blinkLights(SportLight, 3,800,1,1); <br />                            delay(300); <br />                    }   <br /><br />         }<br /><br />                switch (strutState){<br />               case(HARD):<br />                         setLights(SportLight,HIGH);<br />                             setLights(TourLight,LOW);<br />                 break;<br /><br />               case(MEDIUM):<br />                         setLights(SportLight,HIGH);<br />                             setLights(TourLight,HIGH);<br />                 break;<br /><br />               case(SOFT):<br />                         setLights(SportLight,LOW);<br />                             setLights(TourLight,HIGH);<br />                 break;<br />                 <br />               case(UNKNOWN):<br />                         setLights(SportLight,LOW);<br />                             setLights(TourLight,LOW);<br />                 break;<br />        }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Motors<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeMotorIOPin(int pin){<br /><br />    // Set pin used for strut motor as output <br />    pinMode(pin, OUTPUT);<br /><br />        // Make sure Motor is off<br />    digitalWrite(pin,LOW);<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Switches<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeStrutIOPin(int pin){<br /><br />    // Set pin used for strut motor as output <br />    pinMode(pin, INPUT);<br /><br />        // Pull Pin HIGH         <br />    digitalWrite(pin,HIGH);<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set an LED  to a color by Address<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLED(int ledaddress, int color){<br />    //<br />        // rgbEncode(int red,int green,int blue)<br />    // red,green,blue =  0, 255<br />    //<br /><br />    switch(color){<br />        case D_HARD:<br />            setLedColors(ledaddress,ledHardValue);<br />                break;    <br />        case D_MEDIUM:<br />            setLedColors(ledaddress,ledMediumValue);<br />                break;    <br />        case D_SOFT:<br />            setLedColors(ledaddress,ledSoftValue);<br />                break;    <br />        case D_RED:<br />            setLedColors(ledaddress,ledRedValue);<br />                break;    <br />        case D_GREEN:<br />            setLedColors(ledaddress,rgbEncode(0,10,0));<br />                break;    <br />        case D_BLUE:<br />            setLedColors(ledaddress,rgbEncode(0,0,25));<br />                break;    <br />        case D_TURQUOIS:<br />            setLedColors(ledaddress,rgbEncode(0,40,64));<br />                break;    <br />        case D_PURPLE:<br />            setLedColors(ledaddress,rgbEncode(10,0,10));<br />                break;    <br />        case D_YELLOW:<br />            setLedColors(ledaddress,rgbEncode(10,10,0));<br />                break;<br />        case D_STARTUP:<br />            setLedColors(ledaddress,rgbEncode(2,2,2));<br />                break;    <br />        case D_WHITE:<br />            setLedColors(ledaddress,rgbEncode(15,15,15));<br />                break;    <br />        case D_OFF:<br />            setLedColors(ledaddress,rgbEncode(0,0,0));<br />                break;    <br /><br />             <br />        }<br />         // LEDS.show(); //PUSH the current color definitions of ALL the LEDs out<br />         // delayMicroseconds(600); //Wait for 200us to go into reset<br />     <br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to turn an LED off<br />//////////////////////////////////////////////////////////////////////////////////////<br />void offLED(int ledaddress){<br />        <br />    setLedColors(ledaddress,LEDOFF);<br />        LEDS.show(); //PUSH the current color definitions of ALL the LEDs out<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set either the Tour Or Sport light to an HIGH/ON or LOW/OFF state<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLights(int light, int state){<br />  digitalWrite(light,state);<br />        <br />}<br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />// Reference 5v = 1023<br />//<br />// Hard     0.0v     / xxx<br />//    Cutoff  0.415  / 85<br />// Medium   0.83v    / xxx<br />//    Cutoff   1.245   / 255<br />// Soft     1.66v    / xxx<br />//    Cutoff   2.075   / 424<br />// Diag     2.5      / xxx<br />//    Cutoff   2.915   / 596<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readSelectorSwitch(){<br />    int voltage;<br />    int voltage2;<br />    float voltdiff;<br /><br />    // Based on the selector switch type do the appropriate read<br />    if(selectorType == ROTARY){<br />        <br />       voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />       delay(500);                               // debounce for 500 msec<br />       voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />       voltdiff = abs(1.0-voltage/voltage2);     // Calculate the percentage difference <br /><br />       //<br />       // as long as voltage difference &gt; 10% keep reading<br />       //<br />       while(voltdiff &gt; .1 ){<br />          voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />          delay(500);                             // debounce for 500 msec<br />          voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />          voltdiff = abs(1.0-voltage/voltage2);   // Calculate the percentage difference <br />       }<br /><br />           //<br />       // based on the converted voltage value and the range return the value<br />           //<br />       if(voltage &lt; 85){                          <br />             // Selector is in HARD mode <br />             if (currentSelectorState == HARD) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP ) currentSelectorState =HARD;<br />           strutState = HARD;<br />               return(HARD);<br />         }<br />     <br />       }else if (voltage &gt;= 85 &amp;&amp; voltage &lt; 255){ <br />         // Selector is in MEDIUM mode <br />             if (currentSelectorState == MEDIUM) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP ) currentSelectorState = MEDIUM;<br />               strutState = MEDIUM;<br />           return(MEDIUM);<br />         }<br /><br />       }else if (voltage &gt;= 255 &amp;&amp; voltage &lt; 424){<br />             // Selector is in SOFT mode <br />             if (currentSelectorState == SOFT) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP ) currentSelectorState = SOFT;<br />               strutState = SOFT;<br />           return(SOFT);<br />         }<br /><br />       }else if(voltage &gt;= 424 &amp;&amp; voltage &lt;596){<br />             // Selector is in DIAG mode <br />             if (currentSelectorState == DIAG) return (NOCHANGE) ;<br />         else {<br />               if ( currentSelectorState != STARTUP) currentSelectorState = DIAG;<br />               strutState = DIAG;<br />           return(DIAG);<br />         }<br /><br />       }else{<br />              strutState=UNKNOWN;<br />          return (UNKNOWN);<br /><br />       }<br /><br />    }else if (selectorType == PUSHBUTTON){<br />            // if they are pressing switch i.e. LOW then decide what's next<br />            if(digitalRead(ModeSelectSwitch) == LOW){<br />                  delay(1000);<br />          //<br />          // If they are no longer pressing switch then shift the struts to next state<br />          //<br />                  if(digitalRead(ModeSelectSwitch) == HIGH){<br />                        switch(strutState){<br />                           case HARD:<br />                             currentSelectorState = MEDIUM;<br />                             strutState = MEDIUM;<br />                             return(MEDIUM);<br />                           break;<br /><br />                           case MEDIUM:<br />                             currentSelectorState = SOFT;<br />                     strutState = SOFT; <br />                             return(SOFT);<br />                           break;<br /><br />                           case SOFT:<br />                             currentSelectorState = HARD;<br />                     strutState = HARD; <br />                             return(HARD);<br />                           break;<br />               <br />               case READDIAG:<br />               case DIAG:<br />               case UNKNOWN:<br />                             currentSelectorState = DIAG;<br />                     strutState = HARD; <br />                             return(HARD);<br />                           break;<br />                   }<br />            }else{<br />             //<br />             // They are still holding button down wait 3 secs<br />                     delay(3000);<br /><br />             //If they are still holding the switch after 3secs - see if they want DIAGNOSTIC or READ DIAG mode<br />                     if(digitalRead(ModeSelectSwitch) == LOW){ <br />                         blinkBothLights(3,150,2,1000); // Blink both Lights to let them know they are at this mode<br />                         setLED(LED1,D_OFF);<br />                         setLED(LED2,D_OFF);<br />                         setLED(LED3,D_OFF);<br />                         setLED(LED4,D_OFF);<br />                         LEDS.show();<br />                 delay (3000);<br />        <br /> <br />                         if(digitalRead(ModeSelectSwitch) == LOW){ <br />                              //<br />                              // Blink both Lights to let them know they are at this mode<br />                  // They should let go now after seeing blinks if they want to run diag mode<br />                  // <br />                               blinkBothLights(3,150,2,1000); // Blink both Lights to let them know they are at this mode<br />                          <br />                   // Let them know it's over<br />                               setLED(LED1,D_WHITE);<br />                               setLED(LED2,D_WHITE);<br />                               setLED(LED3,D_WHITE);<br />                               setLED(LED4,D_WHITE);<br />                               LEDS.show();<br />                       delay (3000);        <br /><br />                     //If they let go before 3secs - then they want DIAGNOSTIC <br />                             if(digitalRead(ModeSelectSwitch) == HIGH){ <br />                     // Ok they let go they want to Run Diagnostic Mode<br />                                strutState = DIAG; <br />                                return(strutState);<br />                 } else return (strutState);<br />                 <br />             }else {<br />                // Ok they let go they want to Read the current Diagnostic Codes<br />                            strutState = READDIAG;<br />                            return(strutState);<br />             }<br /><br />             } else return (strutState);   //Nope they dont' want anything - just return current State.<br />               }<br />           <br />       }else if (currentSelectorState == DIAG &amp;&amp; strutState == HARD){<br />           // if here then this is 2nd pass thru just after comming out of DIAG or READDIAG MODE<br />               // Switch was not being pressed and last state was DIAG mode Now RETURN HARD to MAIN loop one more time<br />               currentSelectorState = HARD;<br />               return(HARD);<br />       } else return(NOCHANGE);  // Switch is not being pressed - return NOCHANGE state<br />    }<br />}<br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />// Reference 5v = 1023<br />//<br />// Hard     0.0v     / xxx<br />//    Cutoff  0.415  / 85<br />// Medium   0.83v    / xxx<br />//    Cutoff   1.245   / 255<br />// Soft     1.66v    / xxx<br />//    Cutoff   2.075   / 424<br />// Diag     2.5      / xxx<br />//    Cutoff   2.915   / 596<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readDiagSelectorSwitch(){<br />    int voltage;<br />    int voltage2;<br />    float voltdiff;<br /><br />    // Based on the selector switch type do the appropriate read<br />    if(selectorType == ROTARY){<br />        <br />       voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />       delay(500);                               // debounce for 500 msec<br />       voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />       voltdiff = abs(1.0-voltage/voltage2);     // Calculate the percentage difference <br /><br />       //<br />       // as long as voltage difference &gt; 10% keep reading<br />       //<br />       while(voltdiff &gt; .1 ){<br />          voltage = analogRead(ModeSelectSwitch)+1; // read the voltage    <br />          delay(500);                             // debounce for 500 msec<br />          voltage2 = analogRead(ModeSelectSwitch)+1;// read the voltage again<br />          voltdiff = abs(1.0-voltage/voltage2);   // Calculate the percentage difference <br />       }<br />           currentSelectorState = DIAG;<br /><br />           //<br />       // based on the converted voltage value and the range return the value<br />           //<br />       if(voltage &lt; 85) return (HARD) ;<br />       else if (voltage &gt;= 85 &amp;&amp; voltage &lt; 255)  return (MEDIUM) ;<br />       else if (voltage &gt;= 255 &amp;&amp; voltage &lt; 424) return (SOFT);<br />       else if(voltage &gt;= 424 &amp;&amp; voltage &lt;596) return (NOCHANGE);<br />       else return (UNKNOWN);<br /><br /><br />    }else if (selectorType == PUSHBUTTON){<br />            // if they are pressing switch i.e. LOW then decide what's next<br />            if(digitalRead(ModeSelectSwitch) == LOW){<br />                 blinkBothLights(3,150,2,1000); // Blink both Lights to let them know they are at this mode<br />         delay (2000);<br />          //<br />          // they are pressing switch then shift the struts out of DIAG mode to HARD <br />          //<br />                  currentSelectorState = DIAG;<br />          strutState = HARD; <br />                  return(HARD);<br />            }else return (NOCHANGE);<br />         }<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Read actual status of strut , set LED color apprropriately and return value<br />//<br />//<br />///////////////////////////////////////////////////////////////////////////////////////<br />int readstrut(int strutS1,int strutS2){<br />  int switch1;<br />  int switch2;<br /><br /><br />  switch2 = !digitalRead(strutS1);<br />  switch1 = !digitalRead(strutS2);<br />  <br />  if (switch1 == LOW &amp;&amp; switch2 == HIGH){<br />    // Strut is in HARD mode<br />    return(HARD);<br />    <br />  }else if (switch1 == HIGH &amp;&amp;  switch2 == HIGH){<br />    // Strut is in MEDIUM mode  <br />    return(MEDIUM);<br />    <br />  }else if (switch1 == HIGH &amp;&amp; switch2 == LOW){<br />    // Strut is in SOFT Mode<br />    return(SOFT);<br />  }else {<br />    return(UNKNOWN);<br />  }<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Read actual status of strut , set LED color apprropriately and return value<br />//<br />//<br />///////////////////////////////////////////////////////////////////////////////////////<br />int displayStrut(int strutS1,int strutS2,int strutLED,int displayMode){<br />  int switch1;<br />  int switch2;<br />  <br />  int switch1b;<br />  int switch2b;<br /><br />  int timeout = 0;<br />  int numberTries = 0;<br />/*<br />  unsigned long timestart;<br />  unsigned long timenow;<br />  timestart = millis();<br />  */<br /><br />  //<br />  // intial switch reads<br />  //<br />  switch2 = !digitalRead(strutS1);<br />  switch1 = !digitalRead(strutS2);<br />  delayMicroseconds(50); //Wait for 100us to debounce contacts<br />  switch2b = !digitalRead(strutS1);<br />  switch1b = !digitalRead(strutS2);<br /><br /><br />  //<br />  // See if the switches are reading the same<br />  //<br />  while ((switch2 != switch2b || switch1 != switch1b)&amp;&amp; numberTries &lt; 40){<br />      switch2 = !digitalRead(strutS1);<br />      switch1 = !digitalRead(strutS2);<br />      delayMicroseconds(50); //Wait for 50us to debounce contacts<br />      switch2b = !digitalRead(strutS1);<br />      switch1b = !digitalRead(strutS2);<br />      numberTries++;<br />      <br />  }<br />  <br /> <br />  if (switch1 == LOW &amp;&amp; switch2 == HIGH &amp;&amp; numberTries &lt;40 ){<br />    // Strut is in HARD mode<br />    setLED(strutLED, D_HARD);<br />    LEDS.show();<br />    return(HARD);<br />    <br />  }else if (switch1 == HIGH &amp;&amp;  switch2 == HIGH &amp;&amp; numberTries &lt;40 ){<br />    // If Strut is in MEDIUM mode  <br />    setLED(strutLED, D_MEDIUM);<br />    LEDS.show();<br />    return(MEDIUM);<br />    <br />  }else if (switch1 == HIGH &amp;&amp; switch2 == LOW &amp;&amp; numberTries &lt;40 ){<br />    // Strut is in SOFT Mode<br />    setLED(strutLED, D_SOFT);<br />    LEDS.show();<br />    return(SOFT);<br />  }else {<br />    if(displayMode == ERROR) setLED(strutLED, D_RED);<br />    else setLED(strutLED, D_OFF);<br />    LEDS.show();<br />    return(UNKNOWN);<br />  }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Set the desired strut to a particular mode        <br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />int setStrutMode( int selectorDesiredMode, int strutDesiredMode, int strutMotor, int strutSwitch1, int strutSwitch2,int displayLED, int *strutErrorStatus){<br />  int tryCount;<br />  tryCount = 0;<br /><br />   //  SIGNAL_WIRE_FAILURE   <br />   //  MOTOR_COMMAND_FAILURE <br />   //  NO_FAILURES <br /><br />                //<br />                // Reset the Error Flag since we are now allowing a retry for each strut.<br />                //<br />                *strutErrorStatus = NO_FAILURES;<br /><br />                //<br />                // Enable MotorChip<br />                //<br />                digitalWrite(MotorEnable,HIGH);<br /><br /><br />                //<br />        // IF any previous failures - display status &amp; kick out <br />        //<br />                /***** This code disabled as of firmware version 1.10 **************<br />                if (*strutErrorStatus == MOTOR_COMMAND_FAILURE ){<br />                        setLED(displayLED,D_RED);<br />                        LEDS.show();<br />            return(ERROR);<br />        }else if (*strutErrorStatus == SIGNAL_WIRE_FAILURE){<br />                        setLED(displayLED,D_YELLOW);<br />                        LEDS.show();<br />            return(ERROR);<br />        }<br />                ********************************************************************/<br /><br />                //<br />        // ok so no previous problems with this strut - get readay to set to new mode<br />                // <br />        <br />                <br />                //<br />           // Turn on motor as long as strut is not in desired mode but only try the loop for MAXTRIES number of times<br />                //<br />                 while( displayStrut(strutSwitch1, strutSwitch2,displayLED,NOERROR) != strutDesiredMode &amp;&amp;<br />               tryCount &lt; MAXTRIES ){<br />            <br />                digitalWrite(strutMotor, HIGH);<br />                  tryCount++;<br />            }<br />        <br />            // run the motors for 5 msecs more to force strut further into this zone.<br />            delay(5);<br />        <br />        // Turn the Motor Off<br />            digitalWrite(strutMotor, LOW);<br />                    <br />                 // if  MaxTries not exceeded strut is now in desired mode<br />          if(tryCount &lt; MAXTRIES ){<br /><br />                     // Strut is now in desired mode<br />                     *strutErrorStatus = NO_FAILURES;<br />                     return (NOERROR);<br /><br />                }else {<br />            //<br />            // Maxtries was exceeded - display RED for 2 secs turn off for 1 sec &amp; try again.<br />            //<br />                    setLED(displayLED,D_RED);<br />                    LEDS.show();<br />                    delay(1000);<br />                    setLED(displayLED,D_OFF);<br />                    LEDS.show();<br /><br />            //<br />            // Try One More Time<br />            // <br />            tryCount = 0;<br /><br /><br />                     while( displayStrut(strutSwitch1, strutSwitch2,displayLED,NOERROR) != strutDesiredMode &amp;&amp; <br />              tryCount &lt; MAXTRIES ) {<br />                  digitalWrite(strutMotor, HIGH);<br />                    tryCount++;<br />                }<br /><br />                // run the motors for 33msecs more to force strut further into this zone.<br />                    delay(33);<br /><br />            // Turn the Motor Off<br />                digitalWrite(strutMotor, LOW);<br />    <br />              if(tryCount &lt; MAXTRIES){<br />                         // Strut Managed to get into desired mode on 2nd try<br />                        *strutErrorStatus = NO_FAILURES;<br />                         return (NOERROR);<br />              }else{<br />               //<br />               // Maxtries was exceeded - Flag this strut as bad.<br />               //<br />                        setLED(displayLED,D_RED);<br />                        LEDS.show();<br />                        *strutErrorStatus = MOTOR_COMMAND_FAILURE;<br />                        return (ERROR);<br />              }<br />        }<br />         <br />}<br /><br />//*******************************************************************************************<br />//<br />// Diagnostic routine<br />//<br />//<br />//*******************************************************************************************<br />void runDiagnostic(){<br />  <br />             while(readDiagSelectorSwitch()== NOCHANGE){ <br />                   errorFR=0;<br />                   errorFL=0;<br />                   errorRR=0;<br />                   errorRL=0;<br />                   <br />                   // Turn Off All LEDS<br />                   offLED(LED1);<br />                   offLED(LED2);<br />                   offLED(LED3);<br />                   offLED(LED4);<br />                   <br />                   // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />                   delay(1000);<br />  <br />                   // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST FRONT LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           <br />           <br />                   //SET LED 1 to White to indicate testing Front Left Strut <br />                   setLED(LED1,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 1 time as group 4 times To indicate FL strut<br />                   blinkLights(SportLight, 1,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Front Left Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />                                      <br /><br />                      //Attempt to set  Front Left Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Front Left Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutFLmotor, strutFLswitch1, strutFLswitch2,LED1, &amp;errorFL);<br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorFL == 0){<br />                       setLED(LED1,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED1,D_RED);<br />                      LEDS.show();<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br /><br />                   <br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST FRONT Right STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 2 to White to indicate testing Front Right Strut <br />                   setLED(LED2,D_WHITE);<br />                   LEDS.show();<br />             <br />           // Blink Sport Light 2 time as group 3 times To indicate FR strut  <br />                   blinkLights(SportLight, 2,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Front Right Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />                      //Attempt to set  Front Right Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Front Left Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutFRmotor, strutFRswitch1, strutFRswitch2,LED2, &amp;errorFR);<br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br />              // Turn off Lights<br />              setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorFR == 0){<br />                       setLED(LED2,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED2,D_RED);<br />                      LEDS.show();<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST REAR LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 3 to White to indicate testing Front Left Strut <br />                   setLED(LED3,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 3 time as group 3 times To indicate RL strut <br />                   blinkLights(SportLight, 3,300,4,1000); <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Rear Left Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br />                      //Attempt to set  Rear Left Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Rear Left Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutRLmotor, strutRLswitch1, strutRLswitch2,LED3, &amp;errorRL);<br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />              // Turn off Lights<br />              setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorRL == 0){<br />                       setLED(LED3,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED3,D_RED);<br />                      LEDS.show();<br /><br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    TEST REAR RIGHT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 1 to White to indicate testing Rear Right Strut <br />                   setLED(LED4,D_WHITE);<br />                   LEDS.show();<br />            <br />           // Blink Sport Light 4 time as group 3 times To indicate RR strut <br />                   blinkLights(SportLight, 4,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                   // Do the test for this strut as many times as desired<br />               for(int i =1; i&lt;= 1; i++){<br /><br />                      //Attempt to set  Rear Right Strut HARD MODE<br />                      setStrutMode( DIAG, HARD, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br />                      delay(1000);<br />                   <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />                      //Attempt to set  Rear Right Strut Medium MODE<br />                      setStrutMode( DIAG, MEDIUM, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br />                      delay(1000);<br />              <br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />                                      <br />                      //Attempt to set  Rear Right Strut Soft MODE<br />                      setStrutMode( DIAG, SOFT, strutRRmotor, strutRRswitch1, strutRRswitch2,LED4, &amp;errorRR);        <br />                      delay(1000);<br /><br />              // See if abort desired<br />              if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />              <br /><br />              // Turn off Lights<br />              setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // Display Final test results for this Strut<br />           if (errorRR == 0){<br />                       setLED(LED4,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else{<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED4,D_RED);<br />                      LEDS.show();<br />           }<br /><br />           <br />           <br />                   delay(2000);<br />          }<br />          return; <br /><br />}<br />//*******************************************************************************************<br />//<br />//Read Diagnostic routine<br />//This routine displays the current error status of all the struts based on current statuses incurred<br />//this is mainly for Cars that have Sports Tour Lights Only<br />//<br />//*******************************************************************************************<br />void readDiagnostics(){<br />                   <br />                   //  Possible Error Codes for errorFL, errorFR, errorRL, errorRR  =<br />                   //    <br />                   //         SIGNAL_WIRE_FAILURE   <br />                   //         MOTOR_COMMAND_FAILURE <br />                   //         NO_FAILURES <br />                   //<br /><br />         while( readDiagSelectorSwitch()== NOCHANGE){  <br /><br /><br />                   // Turn Off All LEDS<br />                   offLED(LED1);<br />                   offLED(LED2);<br />                   offLED(LED3);<br />                   offLED(LED4);<br />                   <br />                   // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />                   delay(1000);<br />             <br />                   // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />  <br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT FRONT LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           <br />           <br />           // Blink Sport Light 1 time as group 4 times To indicate FL strut<br />                   blinkLights(SportLight, 1,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br /><br />           // Display Final test results for this Strut<br />           if (errorFL == NO_FAILURES){<br />                       setLED(LED1,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorFL == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED1,D_RED);<br />                      LEDS.show();<br /><br />           }else if (errorFL == SIGNAL_WIRE_FAILURE){ <br />                  setLED(LED1,D_YELLOW);<br />                      LEDS.show();<br />                   for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br /><br />                   <br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT FRONT Right STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 2 to White to indicate testing Front Right Strut <br />                   setLED(LED2,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 2 time as group 3 times To indicate FR strut  <br />                   blinkLights(SportLight, 2,300,4,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br /><br />           // Display Final test results for this Strut<br />           if (errorFR == NO_FAILURES){<br />                       setLED(LED2,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorFR == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED2,D_RED);<br />                      LEDS.show();<br />                      delay(2500);<br /><br />           }else if (errorFR == SIGNAL_WIRE_FAILURE){ <br />                    setLED(LED2,D_YELLOW);<br />                      LEDS.show();<br />              for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br /><br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT REAR LEFT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 3 to White to indicate testing Front Left Strut <br />                   setLED(LED3,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 3 time as group 3 times To indicate RL strut <br />                   blinkLights(SportLight, 3,300,3,1000); <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br /><br />           // Display Final test results for this Strut<br />           if (errorRL == NO_FAILURES){<br />                       setLED(LED3,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorRL == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED3,D_RED);<br />                      LEDS.show();<br />                      delay(2500);<br /><br />           }else if (errorRL == SIGNAL_WIRE_FAILURE){<br />                   setLED(LED3,D_YELLOW); <br />                      LEDS.show();<br />               for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br /><br /><br /><br />           // See if abort desired<br />           if(readDiagSelectorSwitch()!= NOCHANGE) return; <br />           <br />                   delay(2000);<br />  <br /><br />                   //////////////////////////////////////////////////////////////////////////////////////<br />                   //                    REPORT REAR RIGHT STRUT<br />                   ///////////////////////////////////////////////////////////////////////////////////////<br />           // Turn off Lights<br />               setLights(SportLight,LOW);<br />                   setLights(TourLight,LOW);<br />           <br />                   //SET LED 1 to White to indicate testing Rear Right Strut <br />                   setLED(LED4,D_WHITE);<br />                   LEDS.show();<br />           <br />           // Blink Sport Light 4 time as group 3 times To indicate RR strut <br />                   blinkLights(SportLight, 4,300,3,1000);  <br /><br />           // See if abort desired<br />                   if(readDiagSelectorSwitch()!= NOCHANGE) return; <br /><br />           // Display Final test results for this Strut<br />           if (errorRR == NO_FAILURES){<br />                       setLED(LED4,D_GREEN);<br />                       LEDS.show();<br />               setLights(TourLight,HIGH);<br />               setLights(SportLight,HIGH);<br />                       delay(2500);<br />           }else if (errorRR == MOTOR_COMMAND_FAILURE){<br />                      blinkBothLights(3,300,3,1000);<br />                      setLED(LED4,D_RED);<br />                      LEDS.show();<br />                      delay(2500);<br /><br />           }else if (errorRR == SIGNAL_WIRE_FAILURE){<br />                   setLED(LED4,D_YELLOW); <br />                      LEDS.show();<br />              for(int i=0;i&lt;8;i++){<br />                     setLights(SportLight,HIGH);<br />                         setLights(TourLight,LOW);<br />             delay(250);<br />                     setLights(SportLight,LOW);<br />                         setLights(TourLight,HIGH);<br />             delay(250);<br />              }<br />              // Turn off Lights<br />                  setLights(SportLight,LOW);<br />                      setLights(TourLight,LOW);<br />           }<br />            <br />                   delay(2000);<br />          }<br />          return;<br />}<br /><br />//**************************************************************************<br />// this routine converts three seperate RGB values in to a correctly encoded RGB integer<br />//**************************************************************************<br />long rgbEncode(int red,int green,int blue){<br /> long rgb;<br /><br /> //if (red &gt; 255) red=255;<br /> //if (green &gt; 255) green=255;<br /> //if (blue &gt; 255) blue=255;<br /><br /> rgb = red*65536 + green*256 + blue;<br /><br /> return (rgb);<br /> <br />} <br /><br /><br /><br />/****************************************************************************************<br /> *<br /> *<br /> * Routine to read int from EProm<br /> *<br /> *<br /> ****************************************************************************************/<br />int eepromReadInt(int address){<br />   int value = 0x0000;<br />   value = value | (EEPROM.read(address) &lt;&lt; 8);<br />   value = value | EEPROM.read(address+1);<br />   return value;<br />}<br /><br />/****************************************************************************************<br /> *<br /> *<br /> * Routine to Write int to EProm<br /> *<br /> *<br /> ****************************************************************************************/<br />void eepromWriteInt(int address, int value){<br />   EEPROM.write(address, (value &gt;&gt; 8) &amp; 0xFF );<br />   EEPROM.write(address+1, value &amp; 0xFF);<br />}<br /><br />//****************************************************<br />//<br />// Convert Desired HEX color into  rgb values and store in LED array<br />//<br />//****************************************************<br /><br />void setLedColors(int LED,long hexValue) {<br /><br />  ledColors[LED].r = ((hexValue &gt;&gt; 16) &amp; 0xFF) ;  // Extract the RR byte<br />  ledColors[LED].g = ((hexValue &gt;&gt; 8) &amp; 0xFF) ;   // Extract the GG byte<br />  ledColors[LED].b = ((hexValue) &amp; 0xFF) ;        // Extract the BB byte<br /><br />}<br /><br /></pre> TechWorks ECS Model 03 Firmware Source Code 2014-08-22T19:06:53+00:00 2014-08-22T19:06:53+00:00 https://renegadetechworks.com/index.php/opensource/techworks-ecs-software-2 TechWorks Admin support@renegadetechworks.com <pre>//****************************************************************************<br />//<br />// Description of Software: <br />//<br />// Mitsubishi 3000gt/Stealth Controller Program<br />// for controlling stock OEM mitsubishi ECS Struts.<br />// Mitsubish 3000gt model years 1991 - 1999.<br />// Note:models years 1995-1999 are retrofit of this system+Original Struts.<br />// Hardware Platform: ATAMega 328 MicroController<br />//<br />// Copyright (C) 2010,2011,2012,2013,2014,2015,2016<br />// Marcus Diaz, RenegadeTechWorks, LLC<br />//<br />//****************************************************************************<br />// Licensing:<br />// Licensed under GNU GPL Version 2<br />//<br />// This program is free software; you can redistribute it and/or modify<br />// it under the terms of the GNU General Public License as published by<br />// the Free Software Foundation under Version 2 of the License<br />//<br />// This program is distributed in the hope that it will be useful,<br />// but WITHOUT ANY WARRANTY; without even the implied warranty of<br />// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />// GNU General Public License for more details.<br />//<br />// You should have received a copy of the GNU General Public License along<br />// with this program; if not, write to the Free Software Foundation, Inc.,<br />// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />//****************************************************************************<br />// VERSION HISTORY<br />//<br />//--------------------------------------------------------------------<br />// SW Ver : v3.00 <br />// HWDesign: ECM M03 PCB Rev <br />// Date: 03/08/2016 <br />// Comments: <br />// This code represents a branch and re-write for the Model 03 of <br />// of theTechWorks ECS controller.<br />// The new Gen3/ Model03(M03) Controller uses a modified MO1 design<br />// New Features are<br />// -- freed up I/O ports to support I2C buss devices<br />// -- includes add on daughther board support for an DLH303 6-axis Accellerometer<br />// -- Code has been rewritten in more Ojbect Oriented style.<br />// -- Auto Mode added to use accelerometer data to auto set struts<br />// -- Manual Mode remains<br />// <br />//<br />//--------------------------------------------------------------------<br />// SW Ver : v3.01 <br />// HWDesign: ECM M03 PCB Rev <br />// Date: 07/05/2016 <br />// Comments: <br />// - Added to startup sequence to retrieve from eprom the <br />// default startup mode<br />// - Added Sport/Tour light blinking patterns to startup sequence that<br />// shows what default startup mode is <br />// - Added Set Led Brightness Mode<br />// - Added Save current mode as default startup mode<br />// - Modes are:<br />// 1 push - set next stiffnes setting<br />// 2 pushes - toggle auto/manual modes<br />// 3 pushes - set led brightness<br />// 4 pushes - save curreent mode as default startup mode<br />// 5 pushes - read diagnostics mode<br />// 6 pushes - run diagnostics mode<br />//--------------------------------------------------------------------<br />// SW Ver : v3.01 <br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 07/09/2016 <br />// Comments: <br />// - fixed logic in sportTourLoopLights routine to correctly display <br />// command mode &amp; signal wire failures<br />// - changed manual strut mode to do struts one at a time<br />//<br />///--------------------------------------------------------------------<br />// SW Ver : v3.01 <br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 07/16/2016 <br />// Comments: <br />// - changed manual strut mode back to parrallel mode <br />// - change H/W design to use 1.5 amp 9v VReg for old strut<br />///--------------------------------------------------------------------<br />// SW Ver : v3.2<br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 07/25/2016 <br />// Comments: <br />// - added modes to set threshold values<br />// - added modes to Calibrate accelerometer<br />// - added code to use accelerometer calibration values<br />// - added code in setStrutMode to turn motors back on for old struts<br />///--------------------------------------------------------------------<br />// SW Ver : v3.3 Official Production Release Used in Shipping M03 Controllers<br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 10/17/2016 <br />// Comments: <br />// - added logging Mode<br />// - added firmware version &amp; startup values to print at startup<br />// - added seperate maxtries params for Auto vs Manual strut modes<br />// - added LED = OFF when strut state cannot be determined during transition<br />// - added LED = GREENYELLOW if strut goes into mode but cannot stay there<br />// - reworked the logic &amp; assignments in readStrutState() - to match reality<br />// - changed sport tour lights for automode to show max target across the four struts<br />///***********************************************************************************<br />#include &lt;EEPROM.h&gt;<br />#include &lt;FastLED.h&gt;<br />#include &lt;PinChangeInt.h&gt;<br />#include &lt;Wire.h&gt;<br /><br />//<br />//********* Firmware Version ****************<br />//<br />#define FIRMWARE_VERSION "Firmware Version M03 v3.3"<br /><br /><br />//<br />//********* LSM303DLHC Accelerometer/ Compass I2C Addresses ****************<br />//<br />#define LSM303_ADDRESS_ACC (0x32 &gt;&gt; 1) // 0011001x<br />#define LSM303_ADDRESS_MAG (0x3C &gt;&gt; 1) // 0011110x<br /><br />#define MANUAL 1<br />#define AUTO 2<br />#define DIAG 3<br />#define READDIAG 4<br />#define SETSTARTUP 5<br />#define SETLED 6<br />#define SETTHRESHOLD 7<br />#define CALIBRATEACCEL 8<br /><br />#define NOMINAL 0<br />#define CHANGING 1<br />#define FAILED 2<br />#define OLDSTRUTS 3<br />#define PRENOMINAL 4<br /><br />#define STRUT_FL 0<br />#define STRUT_FR 1<br />#define STRUT_RL 2<br />#define STRUT_RR 3<br /><br />#define HARD 1<br />#define MEDIUM 2<br />#define SOFT 3<br />#define UNKNOWN 7<br />#define NOCHANGE 10<br />#define STARTUP 11<br /><br />#define D_RED 1<br />#define D_GREEN 2<br />#define D_BLUE 3<br />#define D_TURQUOIS 4<br />#define D_PURPLE 5<br />#define D_YELLOW 6<br />#define D_GREENYELLOW 7<br />#define D_STARTUP 8<br />#define D_HARD 9<br />#define D_MEDIUM 10<br />#define D_SOFT 11<br />#define D_WHITE 12<br />#define D_OFF 13 <br />#define D_UNKNOWN 14 <br /><br /><br />#define RED100 0xFF0000 // Bright Red<br />#define GREEN100 0x00FF00 // Bright Green<br />#define BLUE100 0x0000FF // Bright Blue<br />#define RED001 0x010000 // Faint red<br />#define RED050 0x800000 // 1/2 red (0x80 = 128 out of 256)<br />#define RED025 0x300000 // 25% RED<br />#define GREEN050 0x008000 // half Green<br />#define GREEN025 0x003000 // 25% GREEN<br />#define BLUE050 0x000080 // half Blue<br />#define BLUE025 0x000030 // 25% BLUE<br />#define WHITE015 0x151515 // 15% White<br />#define WHITE010 0x101010 // 10% White<br />#define WHITE005 0x050505 // 05% White<br />#define WHITE001 0x020202 // 05% White<br />#define WHITE100 0xFFFFFF // 100% White<br /><br />#define LEDOFF 0x000000 <br />#define RED 0xFF0000 <br />#define ORANGE 0xFF5500<br />#define ORANGEYELLOW 0xFFA000<br />#define YELLOW 0xFFFF00<br />#define YELLOWGREEN 0xA0FF00<br />#define GREENYELLOW 0x50FF00<br />#define GREEN 0x00FF00<br />#define GREENCYAN 0x00FF4B<br />#define CYAN 0x00FFFF<br />#define CYANBLUE 0x00A0FF<br />#define BLUECYAN 0x005AFF <br />#define BLUE 0x0000FF<br />#define BLUEVIOLET 0x4800FF<br />#define VIOLETBLUE 0x7500FF<br />#define MAGENTA 0xFF00FF<br />#define PINKDEEP 0xFF1493<br />#define PINKHOT 0xFF69B4<br />#define PINK 0xF3967A<br /> <br />#define VIOLET 0xEE82EE<br /><br /><br />#define ERROR 1<br />#define NOERROR 0<br /><br />#define CLEAR 1<br />#define DONT_CLEAR 0<br /><br />#define SIGNAL_WIRE_FAILURE 2<br />#define MOTOR_COMMAND_FAILURE 1<br />#define NO_FAILURES 0<br /><br />#define AUTO_MAXTRIES 475 // maxtries = 475 = approximately 2.75 secs of Motor on<br /> // AUTO_MAXTRIES time equivalent should be less than STRUT_HOLD_INTERVAL<br />#define MANUAL_MAXTRIES 10000 // maxtries = 10000 = approximately 4 secs of Motor on<br />#define MAXFAILURES 100 // maxiumum number of faliures allowed for a strut during a power on cycle<br /><br /><br />#define MAXLIGHTLOOPS 20<br /><br />#define ROTARY 1<br />#define PUSHBUTTON 2<br />#define SAMEMODE 1<br />#define NEXTMODE 2<br /><br />#define LED_LEVEL1 1 // minimum brightness level<br />#define LED_LEVEL2 2<br />#define LED_LEVEL3 3<br />#define LED_LEVEL4 4 // Maximum brightness level<br /><br />// ******************************************************************<br />//<br />// EPROM ADDRESSES<br />//<br />// ******************************************************************<br />#define EPROM_LEDMODE 1 // address in EPROM for default start up LED Brightness Level<br />#define EPROM_CONTROLLER_MODE 3 // address in EPROM for default start up Controller Mode (Auto or Manual)<br />#define EPROM_STRUT_MODE 5 // address in EPROM for default start up Strut Stiffness Mode<br />#define EPROM_MEDIUM_THRESHOLD 7 // address in EPROM for default start up Strut Stiffness Mode<br />#define EPROM_HARD_THRESHOLD 9 // address in EPROM for default start up Strut Stiffness Mode<br />#define EPROM_ACC_X_NEG_1G 11 // address in EPROM for Accelerometer Negative X axis 1G Calibration Value <br />#define EPROM_ACC_Y_NEG_1G 13 // address in EPROM for Accelerometer Negative Y axis 1G Calibration Value <br />#define EPROM_ACC_Z_NEG_1G 15 // address in EPROM for Accelerometer Negative Z axis 1G Calibration Value <br />#define EPROM_ACC_X_POS_1G 17 // address in EPROM for Accelerometer Positive X axis 1G Calibration Value <br />#define EPROM_ACC_Y_POS_1G 19 // address in EPROM for Accelerometer Positive Y axis 1G Calibration Value <br />#define EPROM_ACC_Z_POS_1G 21 // address in EPROM for Accelerometer Positive Z axis 1G Calibration Value <br /><br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />// Map Board I/O Pins to World<br />//<br />// /////////////////////////////////////////////////////////////////////////////////////<br />// Front Left Strut Pins<br />#define STRUT_FL_SWITCH1 3 <br />#define STRUT_FL_SWITCH2 2<br />#define STRUT_FL_MOTOR 10<br /><br />// Front Right Strut Pins<br />#define STRUT_FR_SWITCH1 5 <br />#define STRUT_FR_SWITCH2 4<br />#define STRUT_FR_MOTOR 11<br /><br />// Rear Left Strut Pins<br />#define STRUT_RL_SWITCH1 7 <br />#define STRUT_RL_SWITCH2 6<br />#define STRUT_RL_MOTOR 14<br /><br />// Rear Right Strut Pins<br />#define STRUT_RR_SWITCH1 9 <br />#define STRUT_RR_SWITCH2 8<br />#define STRUT_RR_MOTOR 15<br /><br />//<br />// LED Light Ports <br />//<br />#define DATA_PIN 16 // Data pin that led data will be written out over<br /><br />//<br />// Switch State <br />//<br />#define NO_PUSH_TO_PROCESS 0<br />#define NEW_PUSH_OCCURED 1<br />#define CONTINUE_PROCESSING 2<br /><br /><br /><br />#define ACC_DATA_POINTS 10 // maximum number of Acceleration DataPoints<br />#define STRUT_HOLD_INTERVAL 3000 // numbers of milliseconds to hold a strut higher state;<br /><br />#define X_AXIS 0 // Acceleromter X Axis in Array<br />#define Y_AXIS 1 // Acceleromter Y Axis in Array <br />#define Z_AXIS 2 // Acceleromter Z Axis in Array <br /><br />//<br />// Logging modes<br />//<br />#define LOG_TIME 1<br />#define LOG_STRUTS_NAME 2<br />#define LOG_ACCELEROMETER 3<br /><br />//***************************************************************************************<br />//<br />// Create and Intialize (some ) Global Variables<br />//<br />//***************************************************************************************<br /><br />// <br />// Map Physical Accelerometer axis &amp; sign to normalized variables<br />//<br /><br />int ForwardAxis = Y_AXIS; // default to Y axis<br />int ForwardSign = 1; // default to Y+ +Acc = Acceleration<br /><br />int LeftRightAxis = Z_AXIS; // default to Z axis<br />int LeftRightSign = 1; // default to Z+ +Acc = Left Turn<br /><br />int UpDownAxis = X_AXIS; // default to X axis<br />int UpDownSign = 1; // default to X+ +Acc = Up <br />int accelDataPoints = 10; // default number of acceleration data points to average<br /><br />float ForwardAccelerationArray[ACC_DATA_POINTS] ;<br />float LeftRightAccelerationArray[ACC_DATA_POINTS] ;<br />float UpDownAccelerationArray[ACC_DATA_POINTS] ;<br /><br />float ForwardAcceleration;<br />float LeftRightAcceleration;<br />float UpDownAcceleration;<br /><br />//<br />// X Y Z Axis Calibration Variables<br />//<br />int XAxisNegative1gCalibration = 0;<br />int YAxisNegative1gCalibration = 0;<br />int ZAxisNegative1gCalibration = 0;<br />int XAxisPositive1gCalibration = 0;<br />int YAxisPositive1gCalibration = 0;<br />int ZAxisPositive1gCalibration = 0;<br /><br />//<br />// variables to contain integer level value that corresponds to actual decimal value of threshold<br />// 1 = 0.1g 2= 0.2g 3= 0.3g 4= 0.4g 5 =0.5g 6= 0.6g 7=0.7g<br />int mediumThresholdLevel; <br />int hardThresholdLevel; <br />//<br />// Default Threshold variables for Auto Mode Strut transitions in G's of force<br />//<br />float mediumThreshold = 0.2;<br />float hardThreshold = 0.4;<br /><br />//Mode Selector Switch Pin<br />#define SELECTOR A3 // A3=Analog pin A3=physical pin 26 on AT328) is part of the PinChangeInt.h library defines<br />int selectorSwitchPin = 17;<br /><br />//Switch Type Selector Option Pin<br />int switchTypeSelect = 1;<br /><br />//LED Type Selector Option Pin<br />int LEDTypeSelect = 0;<br /><br />// Sport Light Port<br />int SportLight = 12; <br /><br />// Tour Light Port<br />int TourLight = 13;<br /><br />//Print Mode 1=log/print 0=no logging<br />int logMode = 0;<br />int logSequence = 0;<br /> <br />long ledHardValue ;<br />long ledMediumValue;<br />long ledSoftValue;<br />long ledRedValue;<br /><br />#define FL_LED 0 // Front Left Strut LED<br />#define FR_LED 1 // Front Right Strut LED<br />#define RL_LED 2 // Rear Left Strut LED<br />#define RR_LED 3 // Rear Right Strut LED <br /><br />int lightLoopCounter = MAXLIGHTLOOPS;<br />int lightLoopCounterValue;<br /><br />int accelerometerStrutTargets[4];<br /><br />int previousMode;<br />//<br />// Allocate Structure For LED Strip<br />//<br />#define NUM_LEDS 8<br />struct CRGB ledColors[NUM_LEDS];<br />long ledCurrentValue[4];<br /><br /><br />unsigned long lightCycleStartTime =0;<br />unsigned long signalWireFailureCycleStartTime;<br />unsigned long commandFailureCycleStartTime;<br /><br />// **********************************<br />// Selector Switch variables &amp; Intterupt function<br />// **********************************<br /><br />/*<br />// Notice that anything that gets modified inside an interrupt, that I wish to access<br />// outside the interrupt, is marked "volatile". That tells the compiler not to optimize<br />// them.<br />*/<br />volatile uint8_t pushCount=0;<br />volatile uint8_t pushEvent= NO_PUSH_TO_PROCESS;<br />volatile unsigned long pushTime=0;<br /><br />int firstTime=1;<br /><br />//<br />// Interupt functgion for Selector Switch<br />//<br />void selectorInterruptfunc() {<br /> pushCount = pushCount +1;<br /> pushTime=millis();<br /> pushEvent = NEW_PUSH_OCCURED;<br />}<br /> <br /><br /><br />// **********************************<br />// Strut Object Class Definition<br />// **********************************<br />class Strut{<br /> public:<br /> Strut(){<br /><br /><br /> }<br /><br /> ~Strut(){}<br /><br /> //************************************<br /> int strutPosition() { <br /> return vstrutPosition; <br /> }<br /> int setStrutPosition(int position) { <br /> vstrutPosition = position; <br /> }<br /> //************************************<br /> int desiredState() { <br /> return vdesiredState; <br /> }<br /> int setDesiredState(int target) { <br /> vdesiredState = target; <br /> } <br /> //************************************<br /> unsigned long lastChangeTime() { <br /> return vlastChangeTime; <br /> }<br /> void setLastChangeTime() { <br /> vlastChangeTime= millis(); <br /> } <br /> //************************************<br /> int actualState(int display) { <br /> vstate = readStrutState(vstrutPosition,display);<br /> return vstate; <br /> }<br /> //***********************************<br /> int transitionStatus() { <br /> return vtransitionStatus; <br /> }<br /> int setTransitionStatus(int status) { <br /> vtransitionStatus = status; <br /> }<br /> //************************************<br /> int switch1() { <br /> return vs1Pin; <br /> }<br /><br /> int setSwitch1(int pin) { <br /> vs1Pin = pin; <br /> }<br /> //************************************<br /> int switch2() { <br /> return vs2Pin; <br /> }<br /> int setSwitch2(int pin) { <br /> vs2Pin = pin; <br /> }<br /> //************************************<br /> int motorPin() { <br /> return vmotorPin; <br /> }<br /> int setMotor(int pin) { <br /> vmotorPin = pin; <br /> }<br /> int motorOn() { <br /> digitalWrite(vmotorPin, HIGH);<br /> }<br /> int motorOff() { <br /> digitalWrite(vmotorPin, LOW);<br /> }<br /> //************************************<br /> int tryCount() { <br /> return vtryCount; <br /> }<br /> void clearTryCount() { <br /> vtryCount =0; <br /> }<br /> void incrementTryCount() { <br /> vtryCount++; <br /> }<br /> //************************************<br /> int failures() { <br /> return vtotalFailures; <br /> }<br /> void clearFailures() { <br /> vtotalFailures = 0; <br /> }<br /> void incrementFailures() { <br /> vtotalFailures++; <br /> }<br /><br /> //************************************<br /> int errorStatus() { <br /> return verrorStatus; <br /> }<br /> void setErrorStatus(int status) { <br /> verrorStatus = status;<br /> }<br /><br /><br /><br /> private:<br /> int vstrutPosition; // Strut Postion: STRUT_FL _FR _RL _RR <br /> int vstate; // Actual State of the Struts: HARD, MEDIUM, SOFT<br /> int vdesiredState; // Desired Target State based on last command loop execution: H, M, S<br /> <br /> int vtransitionStatus; // Indicate if the Strut in transition from last command mode to a new desired target or<br /> // Or is stable and in quiet state<br /> // Modes Are:<br /> // NOMINAL = Strut Actual Mode matches the DesiredCommand Target. <br /> // Motors should be off<br /> // CHANGING = Strut In a commanded transitional state changing<br /> // from Medium to Hard for example. Motors Should be On<br /> // Neutral = Everything has been shut down and Motors are off. Command Target is not guaranteed<br /> // Strut is whatever state it is in.<br /> <br /> int verrorStatus = NO_FAILURES; // Indicates current Error Status of Strut Possible Error Status modes are<br /> // NO_FAILURES Means no failure has previously Been detected Since Last Command given<br /> // SIGNAL_WIRE_FAILURE Means a signal wire failure has been previously detected <br /> // MOTOR_COMMAND_FAILURE Means a Motor Command failure has been previously detected <br /> <br /> int vtryCount = 0; // Indicates how many times previously a Strut has has passed thru the command loop<br /> // while trying to achive the desired targetState<br /> <br /> int vtotalFailures = 0; // Indicates how many total times previously a Strut has exceeded maxtries/tryCount<br /> // while trying to achive the desired targetState<br /><br /> int vs1Pin;<br /> int vs2Pin;<br /> int vmotorPin;<br /> unsigned long vlastChangeTime;<br />};<br /><br />// *********************************************************<br />// Controller Object Class Definition<br />// *********************************************************<br />class Controller{<br /> public:<br /> Controller(){<br /><br /><br /> }<br /><br /> ~Controller(){}<br /><br /> int readSwitch(){<br /> return(readControllerSwitch());<br /> }<br /> <br /> int mode() { <br /> return vcontrollerMode; <br /> }<br /> int setMode(int mode) { <br /> vcontrollerMode = mode; <br /> }<br /> int strutMode() { <br /> return vcontrollerStrutMode; <br /> }<br /> int setControllerStrutMode(int mode) { <br /> vcontrollerStrutMode = mode; <br /> }<br /><br /> int selectorType() { <br /> return(vselectorType);<br /> }<br /> int setSelectorType(int type) { <br /> vselectorType = type;<br /> }<br /><br /> //<br /> // Maybe put read switch as function of Controller??<br /> //<br /> private:<br /> int vcontrollerMode; // Auto, Manual, Diag<br /> int vcontrollerStrutMode; // Hard, Med, Soft<br /> int vselectorType = ROTARY; // selectorType captures what kind of selector switch is hooked up to unit<br />};<br /><br /><br /><br />//<br />// ** instantiate &amp; Initialize Strut Array Object **<br />//<br />Strut myStruts[4];<br /><br /><br />//<br />// ** instantiate &amp; Initialize Controller Array Object **<br />//<br />Controller myController;<br /><br /><br />int ledMode = LED_LEVEL2;<br /><br /><br />// Previous State of Selector Switch<br />int currentSelectorState = STARTUP;<br /><br /><br /><br /><br /> <br />///////////////////////////////////////////////////////////////////////////////////////<br />// setup initializes startup <br />///////////////////////////////////////////////////////////////////////////////////////<br />void setup(){<br /><br /> int strutStateFL; <br /> int strutStateFR; <br /> int strutStateRL; <br /> int strutStateRR; <br /><br /> int selectorValue;<br /> int diagLoopNumber;<br /> int epromValue;<br /><br /> <br /> // Initialize the Sport &amp; Tour Light OutPut Pins<br /> pinMode(SportLight, OUTPUT); <br /> pinMode(TourLight, OUTPUT);<br /> <br /> //<br /> // intialize all the motor &amp; Switch state pins<br /> //<br /> myStruts[0].setSwitch1(STRUT_FL_SWITCH1);<br /> myStruts[0].setSwitch2(STRUT_FL_SWITCH2);<br /> myStruts[0].setMotor(STRUT_FL_MOTOR);<br /> myStruts[0].setStrutPosition(STRUT_FL);<br /> myStruts[1].setSwitch1(STRUT_FR_SWITCH1);<br /> myStruts[1].setSwitch2(STRUT_FR_SWITCH2);<br /> myStruts[1].setMotor(STRUT_FR_MOTOR);<br /> myStruts[1].setStrutPosition(STRUT_FR);<br /> myStruts[2].setSwitch1(STRUT_RL_SWITCH1);<br /> myStruts[2].setSwitch2(STRUT_RL_SWITCH2);<br /> myStruts[2].setMotor(STRUT_RL_MOTOR);<br /> myStruts[2].setStrutPosition(STRUT_RL);<br /> myStruts[3].setSwitch1(STRUT_RR_SWITCH1);<br /> myStruts[3].setSwitch2(STRUT_RR_SWITCH2);<br /> myStruts[3].setMotor(STRUT_RR_MOTOR);<br /> myStruts[3].setStrutPosition(STRUT_RR);<br /><br /> for (int i=0; i &lt; 4; i++){<br /> initializeMotorIOPin(myStruts[i].motorPin());<br /> initializeStrutIOPin(myStruts[i].switch1());<br /> initializeStrutIOPin(myStruts[i].switch2());<br /> }<br /><br /> //<br /> // initialize current led value array<br /> //<br /> for (int i=0; i &lt; 4; i++){<br /> ledCurrentValue[i] = 0x00;<br /> }<br /> <br /> //<br /> //<br /> // Initialize Accelerometer <br /> //<br /> Wire.begin(); // Start up I2C, required for LSM303 communication<br /> initAccelerometer();<br /><br /><br /> // ************************************************************<br /> //<br /> // LED Setup &amp; Initialization<br /> //<br /> // ************************************************************<br /> <br /><br /> FastLED.addLeds&lt;WS2812B, DATA_PIN, RGB&gt;(ledColors, NUM_LEDS);<br /><br /> // Clear Out the LED arrays<br /> memset(ledColors, 0, NUM_LEDS * sizeof(struct CRGB)); <br /> LEDS.show(); //Push the current color frame to the LEDs<br /><br /> //<br /> // initialize current led value array<br /> //<br /> for (int i=0; i &lt; 4; i++){<br /> ledCurrentValue[i] = 0x00;<br /> }<br /><br /> // analyze Selector TYPE Input Pin To figure out if Rotary of Pushbutton is connected <br /> // HIGH = Rotary LOW = PUSHBUTTON<br /> pinMode(switchTypeSelect, INPUT);<br /> digitalWrite(switchTypeSelect,HIGH); <br /><br /> // Read the pin to see if it's HIGH or LOW <br /> if(digitalRead(switchTypeSelect) == HIGH){<br /> myController.setSelectorType(PUSHBUTTON);<br /><br /> /* original non-interrupt switch config<br /> pinMode(selectorSwitchPin, INPUT);<br /> */<br /> <br /> //<br /> // new code to use interrupt for pushbutton switch<br /> //<br /> pinMode(SELECTOR, INPUT_PULLUP);<br /> attachPinChangeInterrupt(SELECTOR, selectorInterruptfunc , RISING); // Rising state change will trigger the interrupt.<br /> // attachPinChangeInterrupt(SELECTOR, selectorInterruptfunc , CHANGE); // Any state change will trigger the interrupt.<br /> // attachPinChangeInterrupt(SELECTOR, selectorInterruptfunc , FALLING); // Falling state change will trigger the interrupt.<br />/*<br /> digitalWrite(selectorSwitchPin, HIGH);<br />*/<br /><br /> lightLoopCounterValue = 900; // the lightloopcounter is timing dependent based on selector type<br /> diagLoopNumber = 450;<br /> }else{<br /> myController.setSelectorType(ROTARY);<br /> pinMode(selectorSwitchPin, INPUT);<br /> digitalWrite(selectorSwitchPin, LOW);<br /> lightLoopCounterValue = 60; // the lightloopcounter is timing dependent based on selector type<br /> diagLoopNumber = 40;<br /> } <br /> lightLoopCounter = lightLoopCounterValue;<br /><br /><br /> //<br /> // Retrieve LED Brigtness level from EPROM - if not within range set it to 2nd level and write it back<br /> //<br /> ledMode = eepromReadInt(EPROM_LEDMODE); <br /><br /> //<br /> // Make sure the value is with in range<br /> //<br /> if (ledMode != LED_LEVEL1 &amp;&amp; ledMode != LED_LEVEL2 &amp;&amp; ledMode != LED_LEVEL3 &amp;&amp; ledMode != LED_LEVEL4){<br /> // It's not within range - set to LEVEL two and write it back<br /> ledMode = LED_LEVEL2;<br /> eepromWriteInt(EPROM_LEDMODE,ledMode);<br /> } <br /><br /> //<br /> // Set the LED brightness values based on EProm value<br /> //<br /> setLedValues();<br /><br /> <br /> // EPROM_MEDIUM_THRESHOLD <br /> // EPROM_HARD_THRESHOLD <br /> <br /> //<br /> // Retrieve medium threshold level from EPROM - if not within range set it to 2nd level and write it back <br /> //<br /> mediumThresholdLevel = eepromReadInt(EPROM_MEDIUM_THRESHOLD ); <br /> //<br /> // Make sure the value is with in range<br /> //<br /> if (mediumThresholdLevel &lt; 1 || mediumThresholdLevel &gt; 7 ){<br /> // It's not within range - set to LEVEL two and write it back<br /> mediumThresholdLevel = 2 ; <br /> eepromWriteInt(EPROM_MEDIUM_THRESHOLD, mediumThresholdLevel );<br /> } <br /> //<br /> // Set g threshold level based on EEProm<br /> // <br /> mediumThreshold = float( mediumThresholdLevel/10.0) ; <br /> <br /> //<br /> // Retrieve hard threshold level from EPROM - if not within range set it to 2nd level and write it back <br /> //<br /> hardThresholdLevel = eepromReadInt(EPROM_HARD_THRESHOLD ); <br /> //<br /> // Make sure the value is with in range<br /> //<br /> if (hardThresholdLevel &lt; 1 || hardThresholdLevel &gt; 9 ){<br /> // It's not within range - set to LEVEL two and write it back<br /> hardThresholdLevel = 4 ; <br /> eepromWriteInt(EPROM_HARD_THRESHOLD, hardThresholdLevel );<br /> } <br /> //<br /> // Set g threshold level based on EEProm<br /> // <br /> hardThreshold = float( hardThresholdLevel/10.0) ; <br /> <br /><br /> <br /> //<br /> // Retrieve Strut Mode from EPROM <br /> epromValue= eepromReadInt(EPROM_STRUT_MODE);<br /><br /> /*<br /> Serial.print(F(" Eprom Strut Mode ="));<br /> Serial.println(epromValue);<br /> */<br /><br /> //<br /> // if not valid eprom value set it to Soft; <br /> //<br /> //<br /> if (epromValue != HARD &amp;&amp; epromValue != MEDIUM &amp;&amp; epromValue != SOFT ){<br /> // It's not within range - set to Soft as default<br /> epromValue = SOFT;<br /> eepromWriteInt(EPROM_STRUT_MODE,epromValue);<br /> } <br /> myController.setControllerStrutMode(epromValue);<br /><br /> <br /><br /><br /> //<br /> // Retrieve X Axis Neg 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> XAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_X_NEG_1G); <br /> if (XAxisNegative1gCalibration &gt;32000 || XAxisNegative1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> XAxisNegative1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_X_NEG_1G , XAxisNegative1gCalibration);<br /> } <br /><br /> //<br /> // Retrieve Y Axis Neg 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> YAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Y_NEG_1G); <br /> if (YAxisNegative1gCalibration &gt;32000 || YAxisNegative1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> YAxisNegative1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Y_NEG_1G , YAxisNegative1gCalibration);<br /> } <br /><br /> //<br /> // Retrieve Z Axis Neg 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> ZAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Z_NEG_1G); <br /> if (ZAxisNegative1gCalibration &gt;32000 || ZAxisNegative1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> ZAxisNegative1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Z_NEG_1G , ZAxisNegative1gCalibration);<br /> } <br /><br /> //<br /> // Retrieve X Axis Positive 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> XAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_X_POS_1G); <br /> if (XAxisPositive1gCalibration &gt; 32000 || XAxisPositive1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> XAxisPositive1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_X_POS_1G , XAxisPositive1gCalibration );<br /> } <br /><br /> //<br /> // Retrieve Y Axis Positive 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> YAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Y_POS_1G); <br /> if (YAxisPositive1gCalibration &gt; 32000 || YAxisPositive1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> YAxisPositive1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Y_POS_1G , YAxisPositive1gCalibration );<br /> } <br /><br /> //<br /> // Retrieve Z Axis Positive 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> ZAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Z_POS_1G); <br /> if (ZAxisPositive1gCalibration &gt; 32000 || ZAxisPositive1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> ZAxisPositive1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Z_POS_1G , ZAxisPositive1gCalibration );<br /> } <br /><br /><br /><br /> //<br /> // Turn on the Sport / Tour Lights at startup for 1 sec as test to driver to show they are working.<br /> //<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> delay(1000); <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> delay(300); <br /><br /> <br /> //<br /> // Retrieve Controller Mode from EPROM <br /> //<br /> epromValue= eepromReadInt(EPROM_CONTROLLER_MODE);<br /> /*<br /> Serial.print(F("Eprom Controller Mode ="));<br /> Serial.print(epromValue);<br /> */<br /> //<br /> // if controller mode not Auto or Manual set to manual<br /> //<br /> if (epromValue != AUTO &amp;&amp; epromValue != MANUAL ){<br /> // It's not within range - set to Manual as default<br /> epromValue = MANUAL;<br /> eepromWriteInt(EPROM_CONTROLLER_MODE,epromValue);<br /> } <br /> myController.setMode(epromValue);<br /><br /> //<br /> // Based on Controller default startup mode flash lights accordingly<br /> //<br /> if(epromValue == AUTO){<br /><br /> //<br /> // Blink both lights alternating(3 times) to indicate auto mode<br /> //<br /> for(int i=0;i&lt;2;i++){<br /> setLights(SportLight,HIGH);<br /> setLED(STRUT_FR,D_WHITE);<br /><br /> setLights(TourLight,LOW);<br /> setLED(STRUT_FL,D_OFF);<br /> <br /> delay(250);<br /> setLights(SportLight,LOW);<br /> setLED(STRUT_FR,D_OFF);<br /><br /> setLights(TourLight,HIGH);<br /> setLED(STRUT_FL,D_WHITE);<br /> delay(250);<br /> }<br /> }else if (epromValue == MANUAL){<br /> //<br /> // Blink both lights at together (3 times) to indicate manual mode<br /> //<br /> blinkBothLights(3,250,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> }<br /><br /><br /> lightCycleStartTime =millis();<br /> pushCount=0;<br /><br /> //<br /> // ********* Set Up for Logging &amp; Print some Initial Values<br /> //<br /> Serial.begin(115200); <br /> while (!Serial) {<br /> ; // wait for serial port to connect. Needed for Leonardo only<br /> };<br /> Serial.println(F("*************************************************************************"));<br /> Serial.println(F("Renegade Techworks Mitsubishi 3000gt/Stealth ECS Controller"));<br /> Serial.println(FIRMWARE_VERSION);<br /> Serial.print(F("Strut G-Force Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Strut G-Force Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> readPrintEpromValues();<br /><br />}<br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />//<br />// main loop <br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br /><br />void loop()<br />{<br /> <br /> //<br /> // Check for updates to the state of the Controller Mode <br /> // <br /> //<br /> myController.readSwitch();<br /><br /><br /> if(logMode) doLogMode(LOG_TIME,0,0);<br /><br /> switch(myController.mode()){<br /><br /> case(MANUAL):<br /> if(logMode) doLogMode(LOG_ACCELEROMETER,0,0);<br /> setManualStrutTargets(); <br /> setStruts(); <br /> // setManualStruts(); <br /> break;<br /><br /> case(AUTO):<br /> readAccelerometerValues(); <br /> setAccelerometerTargets();<br /> setAutoStrutTargets();<br /> setStruts(); <br /> break;<br /><br /> case(SETLED):<br /> setLed();<br /> break;<br /><br /> case(SETSTARTUP):<br /> setStartup(); <br /> break;<br /> <br /> case(DIAG):<br /> runDiagnostics();<br /> break;<br /><br /> case(READDIAG):<br /> readDiagnostics();<br /> break;<br /> <br /> case(SETTHRESHOLD):<br /> setAutoThreshold();<br /> break;<br /><br /> case(CALIBRATEACCEL):<br /> calibrateAccelerometer();<br /> break;<br /><br /> }<br /><br /> //<br /> // display current state of Sport-Tour lights<br /> // <br /> setLoopTourSportLights();<br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setAutoThreshold()<br />//<br />//<br />//************************************************************************************<br />void setAutoThreshold(){<br /> <br /> // Turn off Lights while setting LEDs<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> int autoThresholdSet = 0;<br /><br /> mediumThresholdLevel = 2;<br /> hardThresholdLevel = 4;<br /> <br /> /*<br /> * Stay in this loop until button is pressed multple times to indicate save<br /> */ <br /> while(1){ <br /> <br /> //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br /> switch(readPushButtonSwitch()){<br /><br /> case NOCHANGE:<br /> break;<br /><br /> case 1:<br /> //<br /> // Ok they want to change to next Threshold Values <br /> //<br /><br /> if(autoThresholdSet == 5) autoThresholdSet =1;<br /> else autoThresholdSet++;<br /><br /> //<br /> // Blink both lights at together x times to indicate which threshold set is selected<br /> //<br /> blinkBothLights(autoThresholdSet,500,1,0,1);<br /> switch(autoThresholdSet){<br /> case 1:<br /> mediumThresholdLevel = 1;<br /> hardThresholdLevel = 3;<br /> break;<br /><br /> case 2:<br /> mediumThresholdLevel = 2;<br /> hardThresholdLevel = 4;<br /> break;<br /> <br /> case 3:<br /> mediumThresholdLevel = 3;<br /> hardThresholdLevel = 5;<br /> break;<br /><br /> case 4:<br /> mediumThresholdLevel = 4;<br /> hardThresholdLevel = 6;<br /> break;<br /> <br /> case 5:<br /> mediumThresholdLevel = 5;<br /> hardThresholdLevel = 7;<br /> break;<br /> <br /> }<br /><br /> break;<br /><br /> case 2:<br /> case 3:<br /> case 4:<br /> //<br /> // Ok they want to save the auto Threshhold values and exit<br /> //<br /> eepromWriteInt(EPROM_MEDIUM_THRESHOLD,mediumThresholdLevel);<br /> eepromWriteInt(EPROM_HARD_THRESHOLD,hardThresholdLevel);<br /><br /> mediumThreshold = float( mediumThresholdLevel/10.0) ; <br /> hardThreshold = float( hardThresholdLevel/10.0) ; <br /> <br /> //<br /> // Blink both lights at together (7 times) to indicate saving Threshold mode<br /> //<br /> blinkBothLights(7,500,1,0,1);<br /> delay(500);<br /> //<br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /><br /> break;<br /> }<br /> }<br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setLed()<br />//<br />//<br />//************************************************************************************<br />void setLed(){<br /> <br /> // Turn off Lights while setting LEDs<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> /*<br /> * Display LedValues<br /> */<br /> setLedValues();<br /><br /> <br /> /*<br /> * Stay in this loop until button is pressed multple times to indicate save<br /> */ <br /> while(1){ <br /> <br /> //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br /> switch(readPushButtonSwitch()){<br /><br /> case NOCHANGE:<br /> break;<br /><br /> case 1:<br /> //<br /> // Ok they want to change to next LED Value <br /> //<br /><br /> if(ledMode == 4) ledMode =1;<br /> else ledMode++;<br /> <br /> setLedValues();<br /> break;<br /><br /> case 2:<br /> case 3:<br /> case 4:<br /> //<br /> // Ok they want to save the current LED Brightness value and exit<br /> //<br /> eepromWriteInt(EPROM_LEDMODE,ledMode);<br /> <br /> //<br /> // Blink both lights at together (3 times) to indicate LED mode<br /> //<br /> blinkBothLights(3,500,1,0,1);<br /><br /> //<br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /><br /> break;<br /> }<br /> }<br />}<br /><br /><br />//*************************************************************************************<br />//<br />//<br />// setLedValues<br />//<br />//<br />//************************************************************************************<br />void setLedValues(){<br /> <br /> switch(ledMode){<br /> case LED_LEVEL1:<br /> //SET the LEDs to Minimum Brightness Mode <br /> ledHardValue = rgbEncode(2,0,2);<br /> ledMediumValue = rgbEncode(0,2,2);<br /> ledSoftValue = rgbEncode(0,0,2);<br /> ledRedValue = rgbEncode(2,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /><br /> case LED_LEVEL2:<br /> //SET the LEDs to 2nd to Minimum Brightness Mode <br /> ledHardValue = rgbEncode(10,0,10);<br /> ledMediumValue = rgbEncode(0,10,5);<br /> ledSoftValue = rgbEncode(0,0,10);<br /> ledRedValue = rgbEncode(10,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /><br /> case LED_LEVEL3:<br /> //SET the LEDs to Medium Brightness Mode <br /> ledHardValue = rgbEncode(70,0,70);<br /> ledMediumValue = rgbEncode(0,70,35);<br /> ledSoftValue = rgbEncode(0,0,70);<br /> ledRedValue = rgbEncode(70,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /> <br /> case LED_LEVEL4:<br /> //SET the LEDs to Maximum Brightness Mode <br /> ledHardValue = rgbEncode(120,0,120);<br /> ledMediumValue = rgbEncode(0,120,60);<br /> ledSoftValue = rgbEncode(0,0,150);<br /> ledRedValue = rgbEncode(120,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /> }<br /><br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setSTARTUP()<br />//<br />//<br />//************************************************************************************<br />void setStartup(){<br /> int smode;<br /> //<br /> // Save current controller mode <br /> //<br /> eepromWriteInt(EPROM_CONTROLLER_MODE,previousMode);<br /> <br /> /*<br /> Serial.print(F(" Saving Controller Mode ="));<br /> Serial.print(previousMode);<br /> */ <br /><br /> //<br /> // Save current current controller strut mode<br /> //<br /> smode = myController.strutMode();<br /> eepromWriteInt(EPROM_STRUT_MODE,smode);<br /><br /> /*<br /> Serial.print(F(" Saving Strut Mode ="));<br /> Serial.println(smode);<br /> */ <br /><br /> //<br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode); <br /><br /> <br />}<br />//*************************************************************************************<br />//<br />//<br />// setStruts()<br />//<br />//<br />//************************************************************************************ <br />void setStruts(){<br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> if(logMode) doLogMode(LOG_STRUTS_NAME , strut,0);<br /> setStrutMode(strut);<br /> }<br /> <br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setManualStruts()<br />//<br />//<br />//************************************************************************************ <br />void setManualStruts(){<br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> setStrutDirectMode( strut, myStruts[strut].desiredState(), DONT_CLEAR);<br /> }<br /> <br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setManualStrutsTargets()<br />//<br />//<br />//************************************************************************************<br />void setManualStrutTargets(){<br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /><br /> <br /> //<br /> // See if we are changing states<br /> //<br /> if (myStruts[strut].desiredState() != myController.strutMode()){<br /><br /> //<br /> // Yes the human wants a new state - put it in motion<br /> //<br /> myStruts[strut].setDesiredState(myController.strutMode());<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> myStruts[strut].setLastChangeTime();<br /> myStruts[strut].clearTryCount();<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> }else {<br /> //<br /> // No Change - do nothing<br /> //<br /> }<br /> }<br /> <br />}<br />//*************************************************************************************<br />//<br />//<br />// setAutoStrutsTargets()<br />//<br />//<br />//************************************************************************************<br />void setAutoStrutTargets(){<br /><br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> //<br /> // See if we are changing states<br /> //<br /> if (myStruts[strut].desiredState() != accelerometerStrutTargets[strut]){<br /><br /> //<br /> // Yes the accelerometer readings dictate a new state - put it in motion<br /> //<br /> myStruts[strut].setDesiredState(accelerometerStrutTargets[strut]);<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> myStruts[strut].clearTryCount();<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> }else {<br /> //<br /> // No Change - do nothing<br /> //<br /> }<br /> }<br /> <br />}<br /><br /><br /><br />//*******************************************************************************************<br />//<br />//<br />// Rountine to only allow a strut downward change to Medium or Soft<br />// until after the hold interval has passed <br />//<br />//<br />//*******************************************************************************************<br />void setAutoStrutState(int strut, int mode){<br /> <br />unsigned long currentTime = millis();<br /><br /> if ( mode == SOFT &amp;&amp; myController.strutMode() == MEDIUM) mode = MEDIUM;<br /><br /> if (mode == HARD){<br /> <br /> accelerometerStrutTargets[strut] = mode;<br /> myStruts[strut].setLastChangeTime();<br /> <br /> } else if (mode == MEDIUM){<br /> if( accelerometerStrutTargets[strut] == HARD &amp;&amp; currentTime &lt; (myStruts[strut].lastChangeTime() + STRUT_HOLD_INTERVAL)) return;<br /><br /> accelerometerStrutTargets[strut] = MEDIUM;<br /> myStruts[strut].setLastChangeTime();<br /><br /> <br /> } else if (mode == SOFT){<br /><br /> if( (accelerometerStrutTargets[strut] == HARD ||accelerometerStrutTargets[strut] == MEDIUM) <br /> &amp;&amp; currentTime &lt; (myStruts[strut].lastChangeTime() + STRUT_HOLD_INTERVAL)) return;<br /><br /> accelerometerStrutTargets[strut] = SOFT;<br /> myStruts[strut].setLastChangeTime();<br /> }<br />}<br /><br /><br />//*******************************************************************************************<br />//<br />//<br />// Rountine to read current Accelerometer values and set Strut Targets <br />//<br />//<br />//*******************************************************************************************<br />void setAccelerometerTargets(){<br /><br /> <br /> /*<br /> ForwardAcceleration = 0.0;<br /> LeftRightAcceleration = 0.0;<br /> UpDownAcceleration = 0.0;<br /> STRUT_FL <br /> STRUT_FR <br /> STRUT_RL <br /> STRUT_RR<br /> */<br /><br /> /*<br /> Serial.print(F("Forward Accleration ="));<br /> Serial.print(ForwardAcceleration );<br /> Serial.print(F(" Left Right Accleration ="));<br /> Serial.println(LeftRightAcceleration );<br /> */<br /><br /> //<br /> // Check to see if we doing NO Acceleration or Braking ?<br /> //<br /> if (ForwardAcceleration &gt; (-1)*mediumThreshold &amp;&amp; ForwardAcceleration &lt; mediumThreshold ){ <br /> //<br /> // No Foward or Braking Acceleration <br /> // <br /><br /> /*<br /> Serial.println(F("No Forward or Braking Acceleration"));<br /> */ <br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set all struts to soft<br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // set rear struts to SOFT &amp; right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // set rear to SOFT &amp; left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // set rear struts to SOFT &amp; left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /> }<br /> <br /><br /> //<br /> //Check to see if we are doing MEDIUM range Forward Acceleration ?<br /> // <br /> } else if(ForwardAcceleration &gt;= mediumThreshold &amp;&amp; ForwardAcceleration &lt; hardThreshold ){<br /> //<br /> // Doing MEDIUM Forward Acceleration between mediumThresholdg's and hardThreshold <br /> // In this range the Rear Struts will be set to Medium Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /><br /> /*<br /> Serial.println(F("MEDIUM Forward Acceleration"));<br /> */<br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just rear struts to medium<br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set RIGHT side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // set RIGHT side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // set LEFT side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // set LEFT side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /> }<br /> <br /> //<br /> //Check to see if we are doing Hard range Forward Acceleration ?<br /> // <br /> } else if(ForwardAcceleration &gt;= hardThreshold ){ <br /> //<br /> // Doing HARD Forward Acceleration greater than hardThresholdgs <br /> // In this range the Rear Struts will be set to HARD Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /><br /> /*<br /> Serial.println(F("HARD Forward Acceleration"));<br /> */<br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just rear struts to HARD<br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // set rear struts to medium &amp; right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // set rear &amp; left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // set rear struts to medium &amp; left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> }<br /> <br /> //<br /> //Check to see if we are doing MEDIUM range Braking<br /> // <br /> } else if(ForwardAcceleration &gt; (-1)*hardThreshold &amp;&amp; ForwardAcceleration &lt; (-1)*mediumThreshold){ <br /> //<br /> // Doing MEDIUM range Braking between 2.0 &amp; 4.0 g's <br /> // In this range the Front Struts will be set to MEDIUM Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /><br /> /*<br /> * Serial.println(F("MEDIUM Braking"));<br /> */<br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just Front struts to HARD<br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // Set right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // Set left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /> }<br /> <br /> //<br /> //Check to see if we are doing HARD range Braking<br /> // <br /> } else if(ForwardAcceleration &lt;= (-1)*hardThreshold ){ <br /> //<br /> // Doing HARD range Braking greater than 4.0 g's <br /> // In this range the Front Struts will be set to Hard Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /> <br /> /*<br /> Serial.println(F("Hard Braking"));<br /> */<br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just Front struts to HARD<br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // Set right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // Set left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /> }<br /> <br /> } <br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to make one pass thru a Strut to Set the desired strut to a particular mode <br />// this routine must be called in a loop<br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />void setStrutMode( int strut ){<br /><br /> int maxTries; <br /> int currentState;<br /> unsigned long currentTime;<br /><br /> currentTime = millis();<br /><br /> if (myController.mode() == MANUAL) maxTries = MANUAL_MAXTRIES;<br /> else if (myController.mode() == AUTO) maxTries = AUTO_MAXTRIES; <br /><br /> //<br /> // If this strut has exceeded maximum allowed failures bail <br /> // <br /> if (myStruts[strut].failures() &gt; MAXFAILURES){<br /> // Make sure Motor is Off<br /> myStruts[strut].motorOff();<br /> if(logMode){<br /> Serial.print("MAXFAILS,"); <br /> Serial.print("FAILED,"); <br /> Serial.println("FAILED"); <br /> }<br /> return;<br /> }<br /><br /> //<br /> // Jump to strut state<br /> // <br /> switch (myStruts[strut].transitionStatus()){<br /> <br /><br /> case OLDSTRUTS:<br /> //<br /> // This case deals with old struts where the motors &amp; mechansim<br /> // can cause the strut to have problems getting into mode<br /> // This state will purposely run this strut motor <br /> // for an additional 20msec to try an force it into mode <br /> // turn off the motors and then see if the strut is in mode<br /> // and then set state accordingly<br /> <br /><br /> <br /> if( currentTime &lt; myStruts[strut].lastChangeTime() + 20) break;<br /> <br /> //<br /> // OK the motors for this strut have been forced on for 20 msecs<br /> // Turn them off or make sure their still off. <br /> myStruts[strut].motorOff();<br /> <br /> //<br /> // Wait for an additional 10 msecs( 20+10) for strut &amp; switches to stablize<br /> //<br /> if( currentTime &lt; myStruts[strut].lastChangeTime() + 30) break;<br /> <br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if( currentState == myStruts[strut].desiredState() ){<br /><br /> // Ok Struts have held desired mode for 5 msecs<br /> // Switch to Nominal<br /> <br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(NOMINAL);<br /> myStruts[strut].setLastChangeTime();<br /><br /> }else{<br /> //<br /> // Ok we were not able to get the struts into the correct <br /> // mode using old struts mode<br /> // flag them as bad<br /> //<br /> myStruts[strut].motorOff();<br /> myStruts[strut].setLastChangeTime();<br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].setErrorStatus(MOTOR_COMMAND_FAILURE); <br /> myStruts[strut].incrementFailures();<br /> setLED(strut,D_GREENYELLOW);<br /><br /> }<br /> break;<br /> <br /> case PRENOMINAL:<br /><br /> //<br /> // This state is used just after a strut has indicated it<br /> // has achieved the desired mode<br /> // it will double check the state of after 5 msec<br /> // and declare it Nominal if it stays in the desired mode<br /> //<br /><br /> //<br /> // wait 10 msec before checking status<br /> //<br /> if(currentTime &lt; myStruts[strut].lastChangeTime() + 10) break;<br /><br /> currentState = myStruts[strut].actualState(ERROR); <br /><br /> if( currentState == myStruts[strut].desiredState() ){<br /><br /> // Ok Struts have held desired mode<br /> // Switch to Nominal<br /> <br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(NOMINAL);<br /> myStruts[strut].setLastChangeTime();<br /><br /> }else{<br /> //<br /> // Ok it looks like we might be dealing with old struts<br /> // turn the motors back on and set to Old Struts mode<br /> //<br /> myStruts[strut].motorOn();<br /> myStruts[strut].incrementTryCount();<br /> myStruts[strut].setLastChangeTime();<br /> myStruts[strut].setTransitionStatus(OLDSTRUTS);<br /><br /> }<br /> break;<br /><br /> case FAILED:<br /> if (myStruts[strut].errorStatus() == SIGNAL_WIRE_FAILURE){<br /> setLED(strut,D_YELLOW);<br /> }else if (myStruts[strut].errorStatus() == MOTOR_COMMAND_FAILURE){<br /> setLED(strut,D_RED);<br /> }<br /> break;<br /><br /> case NOMINAL:<br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if (currentState != myStruts[strut].desiredState()) {<br /> setLED(strut,D_YELLOW);<br /> myStruts[strut].setErrorStatus(SIGNAL_WIRE_FAILURE); <br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /> }<br /><br /> break;<br /><br /> case CHANGING:<br /> <br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if( myStruts[strut].tryCount() &lt; maxTries ){<br /><br /><br /> if( currentState == myStruts[strut].desiredState() ){<br /> //<br /> // Ok.. we just got the strut into the desired state<br /> //<br /> //<br /> <br /> // make sure the motors are off<br /> myStruts[strut].motorOff();<br /> <br /> // Strut looks like it is now in desired mode<br /> // change state to PRENOMINAL - that state will figure out if strut <br /> // really stayed in desired state<br /> //<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(PRENOMINAL);<br /> myStruts[strut].setLastChangeTime();<br /><br /> }else {<br /> //<br /> // Ok still not in correct state keep motors on<br /> myStruts[strut].motorOn();<br /> myStruts[strut].incrementTryCount();<br /> } <br /><br /> }else {<br /> //<br /> // Maxtries was exceeded - display RED <br /> //<br /><br /> // Turn the Motor Off<br /> myStruts[strut].motorOff();<br /><br /> setLED(strut,D_RED);<br /> myStruts[strut].setErrorStatus(MOTOR_COMMAND_FAILURE);<br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /><br /> }<br /> break;<br /> }<br /><br /> if(logMode){<br /> if (myStruts[strut].errorStatus() == SIGNAL_WIRE_FAILURE){<br /> Serial.print("WIRE_FAIL,"); <br /> }else if (myStruts[strut].errorStatus() == MOTOR_COMMAND_FAILURE){<br /> Serial.print("MOTOR_FAIL,"); <br /> }else if (myStruts[strut].errorStatus() == NO_FAILURES){<br /> Serial.print("STRUT_OK,"); <br /> }<br /><br /> if (myStruts[strut].desiredState() == HARD){<br /> Serial.print("T=HARD,"); <br /> }else if (myStruts[strut].desiredState() == MEDIUM){<br /> Serial.print("T=MEDIUM,"); <br /> }else if (myStruts[strut].desiredState() == SOFT){<br /> Serial.print("T=SOFT,"); <br /> }<br /> if(strut == 3){<br /> if (currentState == HARD){<br /> Serial.println("HARD"); <br /> }else if (currentState == MEDIUM){<br /> Serial.println("MEDIUM"); <br /> }else if (currentState == SOFT){<br /> Serial.println("SOFT"); <br /> }else Serial.println("Unknown"); <br /> }else{<br /> if (currentState == HARD){<br /> Serial.print("HARD, "); <br /> }else if (currentState == MEDIUM){<br /> Serial.print("MEDIUM, "); <br /> }else if (currentState == SOFT){<br /> Serial.print("SOFT, "); <br /> }else Serial.print("Unknown, "); <br /> }<br /> }<br /><br /><br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Read actual status of strut , set LED color apprropriately and return value<br />//<br />//<br />///////////////////////////////////////////////////////////////////////////////////////<br />int readStrutState( int strut, int displayMode){<br /><br /> int strutS1;<br /> int strutS2;<br /> <br /> int switch1;<br /> int switch2;<br /> int switch1b;<br /> int switch2b;<br /><br /> int timeout = 0;<br /> int numberTries = 0;<br />/*<br /> unsigned long timestart;<br /> unsigned long timenow;<br /> timestart = millis();<br /> */<br /><br /> strutS1 = myStruts[strut].switch1();<br /> strutS2 = myStruts[strut].switch2();<br /><br /> //<br /> // intial switch reads<br /> //<br /> switch1 = digitalRead(strutS1);<br /> switch2 = digitalRead(strutS2);<br /> delayMicroseconds(50); //Wait for 100us to debounce contacts<br /> switch1b = digitalRead(strutS1);<br /> switch2b = digitalRead(strutS2);<br /><br /><br /> //<br /> // See if the switches are reading the same<br /> //<br /> while ((switch2 != switch2b || switch1 != switch1b)&amp;&amp; numberTries &lt; 40){<br /> switch1 = digitalRead(strutS1);<br /> switch2 = digitalRead(strutS2);<br /> delayMicroseconds(50); //Wait for 50us to debounce contacts<br /> switch1b = digitalRead(strutS1);<br /> switch2b = digitalRead(strutS2);<br /> numberTries++;<br /> <br /> }<br /> <br /> <br /> if (switch2 == HIGH &amp;&amp; switch1 == LOW &amp;&amp; numberTries &lt;40 ){<br /> // Strut is in HARD mode<br /> setLED(strut, D_HARD);<br /> return(HARD);<br /> <br /> }else if (switch2 == LOW &amp;&amp; switch1 == LOW &amp;&amp; numberTries &lt;40 ){<br /> // If Strut is in MEDIUM mode <br /> setLED(strut, D_MEDIUM);<br /> return(MEDIUM);<br /> <br /> }else if (switch2 == LOW &amp;&amp; switch1 == HIGH &amp;&amp; numberTries &lt;40 ){<br /> // Strut is in SOFT Mode<br /> setLED(strut, D_SOFT);<br /> return(SOFT);<br /> }else {<br /> // setLED(strut, D_UNKNOWN);<br /> setLED(strut, D_OFF);<br /> return(UNKNOWN);<br /> }<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to set a Strut to a desired mode <br />// this routine has it's own internal loop<br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />void setStrutDirectMode( int strut, int Mode,int Clear ){<br /> <br /> int currentState;<br /> int maxTries = 20000;<br /><br /><br /> /*<br /> SIGNAL_WIRE_FAILURE <br /> MOTOR_COMMAND_FAILURE <br /> NO_FAILURES <br /> if (myStruts[strut].desiredState() != myController.strutMode()){*<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setDesiredState(myController.strutMode());<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> STRUT_FL 0<br /> STRUT_FR 1<br /> STRUT_RL 2<br /> STRUT_RR 3<br /> */<br /><br /> if(Clear == CLEAR){<br /> myStruts[strut].clearFailures();<br /> myStruts[strut].clearTryCount();<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> <br /> } <br /><br /> myStruts[strut].setDesiredState(Mode);<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> <br /> while (myStruts[strut].transitionStatus() == CHANGING ){<br /> <br /> if (myStruts[strut].failures() &gt; MAXFAILURES){<br /> // Make sure Motor is Off<br /> myStruts[strut].motorOff();<br /> return;<br /> }<br /><br /> switch (myStruts[strut].transitionStatus()){<br /><br /> case FAILED:<br /> setLED(strut,D_RED);<br /> return;<br /> break;<br /><br /> case NOMINAL:<br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if (currentState != myStruts[strut].desiredState()) {<br /> setLED(strut,D_YELLOW);<br /> myStruts[strut].setErrorStatus(SIGNAL_WIRE_FAILURE); <br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /> }<br /> return;<br /><br /> case CHANGING:<br /> <br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if( myStruts[strut].tryCount() &lt; maxTries ){<br /><br /><br /> if( currentState == myStruts[strut].desiredState() ){<br /> //<br /> // Ok.. we just got the strut into the desired state<br /><br /> // delay for X milliseconds to make sure into mode.<br /> // delay(30); <br /> <br /> // Turn the Motor Off<br /> myStruts[strut].motorOff();<br /> <br /> // Strut is now in desired mode<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(NOMINAL);<br /><br /> //<br /> // Pause for 1 second and do a final check to see if strut stayed in the desired mode<br /> //<br /> delay(1000);<br /><br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if (currentState != myStruts[strut].desiredState()) {<br /> setLED(strut,D_YELLOW);<br /> myStruts[strut].setErrorStatus(SIGNAL_WIRE_FAILURE); <br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /> }<br /><br /> }else {<br /> //<br /> // Ok still not in correct state keep motors on<br /> myStruts[strut].motorOn();<br /> myStruts[strut].incrementTryCount();<br /> } <br /><br /> }else {<br /> //<br /> // Maxtries was exceeded - display RED <br /> //<br /><br /> // Turn the Motor Off<br /> myStruts[strut].motorOff();<br /><br /> setLED(strut,D_RED);<br /> myStruts[strut].setErrorStatus(MOTOR_COMMAND_FAILURE);<br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /><br /> }<br /> break;<br /> }<br /> }<br />}<br /><br /><br /><br /><br />//*********************************************************************************************<br />//<br />//<br />// Logging Routine<br />//<br />//<br />//*********************************************************************************************<br /><br />void doLogMode(int mode,int val1,int val2){<br />unsigned long currentTime = millis();<br /> /*<br /> * Time, Controller Mode, Controller Target , Acc FWD Avg, Acc LeftRight Avg , Acc UP Avg,<br /> * Strut Name, FL Strut STatus, FL Target, FL Strut Actual <br /> * Strut Name, FR Strut STatus, FR Target, FR Strut Actual <br /> * Strut Name, RL Strut STatus, RL Target, RL Strut Actual <br /> * Strut Name, RR Strut STatus, RR Target, RR Strut Actual <br /> *<br /> */<br /><br /> switch (mode){<br /><br /> case LOG_TIME:<br /> Serial.print(currentTime);<br /> Serial.print(F(","));<br /> <br /> if(myController.mode() == MANUAL){<br /> Serial.print(F("MANUAL,"));<br /><br /> }else if(myController.mode() == AUTO){<br /> Serial.print(F("AUTO,"));<br /> }else{<br /> Serial.print(F("MODE?,"));<br /> }<br /> if (myController.strutMode() == HARD){<br /> Serial.print(F("HARD,"));<br /> }else if (myController.strutMode() == MEDIUM){<br /> Serial.print(F("MEDIUM,"));<br /> }else if (myController.strutMode() == SOFT){<br /> Serial.print(F("SOFT,"));<br /> }<br /><br /> break;<br /> <br /> case LOG_STRUTS_NAME:<br /> if (val1 == 0) Serial.print("FL,"); <br /> else if (val1 == 1) Serial.print("FR,"); <br /> else if (val1 == 2) Serial.print("RL,"); <br /> else if (val1 == 3) Serial.print("RR,"); <br /> break;<br /><br /> case LOG_ACCELEROMETER:<br /> readAccelerometerValues(); <br /><br /><br /> break;<br /> }<br />}<br /><br /><br /><br />//**************************************************************************<br />// this routine converts three seperate RGB values in to a correctly encoded RGB integer<br />//**************************************************************************<br />long rgbEncode(int red,int green,int blue){<br /> long rgb;<br /><br /> //if (red &gt; 255) red=255;<br /> //if (green &gt; 255) green=255;<br /> //if (blue &gt; 255) blue=255;<br /><br /> rgb = red*65536 + green*256 + blue;<br /><br /> return (rgb);<br /> <br />} <br /><br /><br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink one of the lights<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkLights(int lightaddress, int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay){<br /> <br /> setLights(lightaddress,LOW);<br /> <br /> for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br /> <br /> for (int count = numberBlinks; count &gt; 0; count--){<br /> delay(blinkDelay);<br /> setLights(lightaddress,HIGH);<br /> delay(blinkDelay);<br /> setLights(lightaddress,LOW);<br /> }<br /> delay(groupDelay);<br /> }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink Both of the lights at the same time<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkBothLights(int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay,int doLed){<br /> <br /> // Turn off both Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> <br /> for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br /> <br /> for (int count = numberBlinks; count &gt; 0; count--){<br /> delay(blinkDelay);<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> if(doLed){<br /> setLED(STRUT_FL,D_WHITE);<br /> setLED(STRUT_FR,D_WHITE);<br /> }<br /> <br /> delay(blinkDelay);<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> if(doLed){<br /> setLED(STRUT_FL,D_OFF);<br /> setLED(STRUT_FR,D_OFF);<br /> }<br /> }<br /> delay(groupDelay);<br /> }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Set TourSport Lights during command loop<br />///////////////////////////////////////////////////////////////////////////////////////<br /><br />void setLoopTourSportLights(){<br /><br /> int strutMode;<br /> unsigned long currentTime = millis();<br /> <br /> if(currentTime &lt;= lightCycleStartTime + 30000){<br /> //<br /> // time to do Standard Lights Status<br /> //<br /><br /> if(myController.mode() == MANUAL) strutMode = myController.strutMode();<br /> else if (myController.mode() == AUTO) {<br /> strutMode = SOFT;<br /> for(int strut = 0; strut &lt; 4; strut++){<br /> if(myStruts[strut].desiredState() == MEDIUM &amp;&amp; strutMode == SOFT ) strutMode = MEDIUM;<br /> if(myStruts[strut].desiredState() == HARD &amp;&amp; ( strutMode == MEDIUM || strutMode == SOFT)) strutMode = HARD;<br /> }<br /> }<br /> switch (strutMode){<br /> case(HARD):<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> break;<br /><br /> case(MEDIUM):<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> break;<br /><br /> case(SOFT):<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> break;<br /> <br /> case(UNKNOWN):<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> break;<br /> } <br /><br /> signalWireFailureCycleStartTime = millis();<br /> <br /> }else if(lightCycleStartTime+30000 &lt; currentTime &amp;&amp; currentTime &lt;= lightCycleStartTime + 31500){<br /> //<br /> // time to do Signal Wire Status<br /> //<br /><br /> <br /> // if there was a problem start flashing shit.. to the do sport/tourlight error thing<br /> if (myStruts[STRUT_FL].errorStatus() == SIGNAL_WIRE_FAILURE || <br /> myStruts[STRUT_FR].errorStatus() == SIGNAL_WIRE_FAILURE ||<br /> myStruts[STRUT_RL].errorStatus() == SIGNAL_WIRE_FAILURE ||<br /> myStruts[STRUT_RR].errorStatus() == SIGNAL_WIRE_FAILURE ) {<br /> <br /><br /> if(currentTime &lt; signalWireFailureCycleStartTime + 250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 750 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 1000 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 1250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 1500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> }<br /> }<br /> commandFailureCycleStartTime = millis();<br /><br /> }else if(lightCycleStartTime+31500 &lt; currentTime &amp;&amp; currentTime &lt;= lightCycleStartTime + 33000){<br /> //<br /> // time to do Motor Command Status<br /> //<br /><br /> // if there was a problem start flashing shit.. to the do sport/tourlight error thing<br /> if (myStruts[STRUT_FL].errorStatus() == MOTOR_COMMAND_FAILURE || <br /> myStruts[STRUT_FR].errorStatus() == MOTOR_COMMAND_FAILURE ||<br /> myStruts[STRUT_RL].errorStatus() == MOTOR_COMMAND_FAILURE ||<br /> myStruts[STRUT_RR].errorStatus() == MOTOR_COMMAND_FAILURE ) {<br /> <br /><br /> if(currentTime &lt; commandFailureCycleStartTime + 250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 750 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 1000 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 1250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 1500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> }<br /> }<br /> } else if (currentTime &gt; lightCycleStartTime + 33000) lightCycleStartTime = millis();<br /> <br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Motors<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeMotorIOPin(int pin){<br /><br /> // Set pin used for strut motor as output <br /> pinMode(pin, OUTPUT);<br /><br /> // Make sure Motor is off<br /> digitalWrite(pin,LOW);<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Switches<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeStrutIOPin(int pin){<br /><br /> // Set pin used for strut motor as output <br /> pinMode(pin, INPUT);<br /><br /> // Pull Pin HIGH <br /> digitalWrite(pin,HIGH);<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set an LED to a color by Address<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLED(int ledaddress, int color){<br /> //<br /> // rgbEncode(int red,int green,int blue)<br /> // red,green,blue = 0, 255<br /> //<br /><br /> switch(color){<br /> case D_HARD:<br /> setLedColors(ledaddress,ledHardValue);<br /> break; <br /> case D_MEDIUM:<br /> setLedColors(ledaddress,ledMediumValue);<br /> break; <br /> case D_SOFT:<br /> setLedColors(ledaddress,ledSoftValue);<br /> break; <br /> case D_RED:<br /> setLedColors(ledaddress,ledRedValue);<br /> break; <br /> case D_GREEN:<br /> setLedColors(ledaddress,rgbEncode(0,10,0));<br /> break; <br /> case D_BLUE:<br /> setLedColors(ledaddress,rgbEncode(0,0,25));<br /> break; <br /> case D_TURQUOIS:<br /> setLedColors(ledaddress,rgbEncode(0,40,64));<br /> break; <br /> case D_PURPLE:<br /> setLedColors(ledaddress,rgbEncode(10,0,10));<br /> break; <br /> case D_YELLOW:<br /> setLedColors(ledaddress,rgbEncode(15,15,0));<br /> break;<br /> case D_GREENYELLOW:<br /> setLedColors(ledaddress,rgbEncode(55,128,0));<br /> break;<br /> case D_STARTUP:<br /> setLedColors(ledaddress,rgbEncode(2,2,2));<br /> break; <br /> case D_WHITE:<br /> setLedColors(ledaddress,rgbEncode(15,15,15));<br /> break; <br /> case D_OFF:<br /> setLedColors(ledaddress,rgbEncode(0,0,0));<br /> break; <br /> case D_UNKNOWN:<br /> setLedColors(ledaddress,rgbEncode(53,18,0));<br /> break; <br /><br /> <br /> }<br /> // delayMicroseconds(600); //Wait for 200us to go into reset<br /> <br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to turn an LED off<br />//////////////////////////////////////////////////////////////////////////////////////<br />void offLED(int ledaddress){<br /> <br /> setLedColors(ledaddress,LEDOFF);<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set either the Tour Or Sport light to an HIGH/ON or LOW/OFF state<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLights(int light, int state){<br /> digitalWrite(light,state);<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to turn off all strut motors<br />///////////////////////////////////////////////////////////////////////////////////////<br />void motorsOff(){<br /> myStruts[0].motorOff();<br /> myStruts[1].motorOff();<br /> myStruts[2].motorOff();<br /> myStruts[3].motorOff();<br />}<br /><br />//****************************************************<br />//<br />// Convert Desired HEX color into rgb values and store in LED array<br />//<br />//****************************************************<br /><br />void setLedColors(int LED,long hexValue) {<br /> <br /> if( ledCurrentValue[LED] == hexValue ) return;<br /> ledCurrentValue[LED] = hexValue ;<br /> ledColors[LED].r = ((hexValue &gt;&gt; 16) &amp; 0xFF) ; // Extract the RR byte<br /> ledColors[LED].g = ((hexValue &gt;&gt; 8) &amp; 0xFF) ; // Extract the GG byte<br /> ledColors[LED].b = ((hexValue) &amp; 0xFF) ; // Extract the BB byte<br /><br /> ledColors[LED+4].r = ledColors[LED].r ;<br /> ledColors[LED+4].g = ledColors[LED].g ;<br /> ledColors[LED+4].b = ledColors[LED].b ;<br /><br /><br /> LEDS.show(); //PUSH the current color definitions of ALL the LEDs out<br />}<br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// routine to read the switch pushes while setting LED brightness <br />//<br />//<br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readPushButtonSwitch(){<br /> int numberPushes =0;<br /> unsigned long currentTime;<br /> currentTime = millis();<br /><br /> //<br /> // If no presses have occurred or &lt; 0.5 secs since push then return<br /> //<br /> if(pushCount == 0) return(NOCHANGE); <br /> if(currentTime &lt; (pushTime + 1500)) {<br /> /*<br /> Serial.print(F("PushCount &lt; 2000ms ="));<br /> Serial.println(pushCount);<br /> */<br /> return(NOCHANGE);<br /> }<br /><br /> /*<br /> Serial.print(F("PushCount &gt;2000 ="));<br /> Serial.println(pushCount);<br /> */<br /><br /><br /> //<br /> // Make sure pushCount is in valid range - if out of range set back to zero<br /> //<br /> if(pushCount &gt; 4){<br /> pushCount = 0;<br /> return (NOCHANGE); <br /> }<br /> numberPushes = pushCount;<br /> pushCount = 0;<br /> return(numberPushes);<br /><br /> /* <br /> * Alternate code for more complex return<br /> //<br /> // Ok button has been pressed one or more times and 0.5 seconds has passed since last push <br /> // now decide what new state we are in<br /> //<br /> switch (pushCount){<br /> case 1:<br /> pushCount = 0;<br /> return(1);<br /> break;<br /><br /> case 2: <br /> pushCount = 0;<br /> return(2);<br /> break;<br /> <br /> case 3: <br /> pushCount = 0;<br /> return(3);<br /> break;<br /> <br /> case 4: <br /> pushCount = 0;<br /> return(4);<br /> break;<br /> break;<br /><br /><br /> }<br /> */<br /><br />}<br /><br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />// Reference 5v = 1023<br />//<br />// Hard 0.0v / xxx<br />// Cutoff 0.415 / 85<br />// Medium 0.83v / xxx<br />// Cutoff 1.245 / 255<br />// Soft 1.66v / xxx<br />// Cutoff 2.075 / 424<br />// Diag 2.5 / xxx<br />// Cutoff 2.915 / 596<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readControllerSwitch(){<br /> int voltage;<br /> int voltage2;<br /> float voltdiff;<br /><br /> unsigned long currentTime ;<br /><br /> // Based on the selector switch type do the appropriate read<br /> if( myController.selectorType() == ROTARY){<br /> <br /> voltage = analogRead(selectorSwitchPin)+1; // read the voltage <br /> delay(50); // debounce for 50 msec<br /> voltage2 = analogRead(selectorSwitchPin)+1;// read the voltage again<br /> voltdiff = abs(1.0-voltage/voltage2); // Calculate the percentage difference <br /><br /> //<br /> // as long as voltage difference &gt; 10% keep reading<br /> //<br /> while(voltdiff &gt; .1 ){<br /> voltage = analogRead(selectorSwitchPin)+1; // read the voltage <br /> delay(50); // debounce for 50 msec<br /> voltage2 = analogRead(selectorSwitchPin)+1;// read the voltage again<br /> voltdiff = abs(1.0-voltage/voltage2); // Calculate the percentage difference <br /> }<br /><br /> //<br /> // based on the converted voltage value and the range return the value<br /> //<br /> if(voltage &lt; 85){ <br /> // Selector is in HARD mode <br /> if (myController.strutMode() == HARD) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(MANUAL);<br /> myController.setControllerStrutMode(HARD);<br /> return(HARD);<br /> }<br /> <br /> }else if (voltage &gt;= 85 &amp;&amp; voltage &lt; 255){ <br /> // Selector is in MEDIUM mode <br /> if (myController.strutMode() == MEDIUM) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(MANUAL);<br /> myController.setControllerStrutMode(MEDIUM);<br /> return(MEDIUM);<br /> }<br /><br /> }else if (voltage &gt;= 255 &amp;&amp; voltage &lt; 424){<br /> // Selector is in SOFT mode <br /> if (myController.strutMode() == SOFT) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(MANUAL);<br /> myController.setControllerStrutMode(SOFT);<br /> return(SOFT);<br /> }<br /><br /> }else if(voltage &gt;= 424 &amp;&amp; voltage &lt;596){<br /> // Selector is in DIAG mode <br /> if (myController.mode() == DIAG) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(DIAG);<br /> return(DIAG);<br /> }<br /><br /> }else{<br /> myController.setMode(UNKNOWN);<br /> return (UNKNOWN);<br /><br /> }<br /> }else if( myController.selectorType() == PUSHBUTTON){<br /> currentTime = millis();<br /> //<br /> // If no presses have occurred or &lt; 1.5 secs since last push then return<br /> //<br /> if(pushCount ==0) return(NOCHANGE); <br /> if(currentTime &lt; (pushTime + 1500)) {<br /> /*<br /> Serial.print(F("PushCount &lt; 2000ms ="));<br /> Serial.println(pushCount);<br /> */<br /> return(NOCHANGE);<br /><br /> }<br /> /*<br /> Serial.print(F("PushCount &gt;2000 ="));<br /> Serial.println(pushCount);<br /> */<br /> //<br /> // Make sure pushCount is in valid range - if out of range set back to zero<br /> //<br /> if(pushCount &gt; 9){<br /> pushCount = 0;<br /> return (NOCHANGE); <br /> }<br /><br /> //<br /> // Ok button has been pressed one or more times and 1.5 seconds has passed since last push <br /> // now decide what new state we are in<br /> //<br /> switch (pushCount){<br /> //<br /> // Changing to next strut mode using sequence H -&gt; S -&gt; M -&gt; H -&gt; S -&gt; M -&gt;....<br /> case 1:<br /><br /> pushCount = 0;<br /><br /> //<br /> // If in Diag or readDiag mode and single push occurs treat this as an abort to go back to manual mode<br /> //<br /> if(myController.mode() == DIAG || myController.mode() == READDIAG){<br /> myController.setMode(MANUAL);<br /> return(myController.strutMode());<br /> }<br /> <br /> <br /> switch(myController.strutMode()){<br /> case HARD:<br /> myController.setControllerStrutMode(SOFT);<br /> return(SOFT);<br /> break;<br /><br /> case MEDIUM:<br /> if(myController.mode() == AUTO) myController.setControllerStrutMode(SOFT);<br /> else myController.setControllerStrutMode(HARD);<br /> return(HARD);<br /> break;<br /><br /> case SOFT:<br /> myController.setControllerStrutMode(MEDIUM);<br /> return(MEDIUM);<br /> break;<br /> }<br /> break;<br /> //<br /> // Changing Controller mode from Auto to Manual or vice versa<br /> case 2: <br /> motorsOff();<br /><br /> pushCount = 0;<br /> switch(myController.mode()){<br /> case MANUAL:<br /><br /> myController.setMode(AUTO); // Change controller to Auto MODE<br /> myController.setControllerStrutMode(SOFT);<br /><br /> //<br /> // Blink both lights alternating(3 times) to indicate auto mode<br /> //<br /> for(int i=0;i&lt;2;i++){<br /> setLights(SportLight,HIGH);<br /> setLED(STRUT_FR,D_WHITE);<br /> <br /> setLights(TourLight,LOW);<br /> setLED(STRUT_FL,D_OFF);<br /> <br /> delay(250);<br /> setLights(SportLight,LOW);<br /> setLED(STRUT_FR,D_OFF);<br /> <br /> setLights(TourLight,HIGH);<br /> setLED(STRUT_FL,D_WHITE);<br /> delay(250);<br /> }<br /><br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> setLED(STRUT_FL,D_OFF);<br /> setLED(STRUT_FR,D_OFF);<br /> <br /> return(myController.strutMode());<br /> break;<br /><br /> case AUTO:<br /> case DIAG:<br /> case READDIAG:<br /> <br /> myController.setMode(MANUAL); // Change Controller to Manual MODE<br /><br /> //<br /> // Blink both lights at together (3 times) to indicate manual mode<br /> //<br /> blinkBothLights(3,250,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> return(myController.strutMode());<br /><br /> break;<br /><br /> }<br /> break;<br /><br /> //<br /> // Changing Controller LED Settings<br /> //<br /> case 3: <br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /> myController.setMode(SETLED); // Change Controller to change LED MODE<br /><br /> //<br /> // Blink both lights at together (3 times) to indicate LED mode<br /> //<br /> blinkBothLights(3,500,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> break; <br /><br /> //<br /> // Set default startup Mode<br /> //<br /> case 4: <br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /><br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //<br /> // Blink both lights at together (4 times) to indicate saving startup mode<br /> //<br /> blinkBothLights(4,500,1,0,1);<br /><br /> myController.setMode(SETSTARTUP); // Change Controller to change LED MODE<br /> break; <br /><br /> //<br /> // Changing Controller to Read Diagnostics Mode<br /> //<br /> case 5: <br /> motorsOff();<br /> myController.setMode(READDIAG);<br /> pushCount = 0;<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //<br /> // Blink both lights at together (5 times) to indicate read diag mode<br /> //<br /> blinkBothLights(5,500,1,0,1);<br /> delay(500);<br /><br /> return(READDIAG);<br /><br /> break;<br /> <br /> //<br /> // Changing Controller to RUN Diagnostics Mode<br /> //<br /> case 6: <br /> motorsOff();<br /> myController.setMode(DIAG);<br /> pushCount = 0;<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //<br /> // Blink both lights at together (6 times) to indicate saving startup mode<br /> //<br /> blinkBothLights(6,500,1,0,1);<br /> delay(500);<br /><br /> return(DIAG);<br /> break;<br /><br /> //<br /> // Changing Controller to Set Thresholds Mode<br /> //<br /> case 7: <br /><br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /> myController.setMode(SETTHRESHOLD); // Change Controller to change LED MODE<br /><br /> //<br /> // Blink both lights at together (7 times) to indicate Set Threshold mode<br /> //<br /> blinkBothLights(7,500,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> return(SETTHRESHOLD);<br /> break;<br /><br /><br /> //<br /> // Toggle Controller in out of logging mode<br /> //<br /> case 8: <br /> //<br /> // Blink both lights at together (9 times) to indicate logging mode on or Off<br /> //<br /> pushCount = 0;<br /> blinkBothLights(8,500,1,0,1);<br /> if(logMode == 0) {<br /> logMode =1;<br /> logSequence++;<br /> Serial.println(F("**********************************************************************"));<br /> Serial.print(F("LogSequence #:"));<br /> Serial.println(logSequence);<br /> Serial.println(F("******** Strut GForce Transition Thresholds ********"));<br /> Serial.print(F("Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> readPrintEpromValues();<br /> Serial.print(F("1:Time, 2:Controller Mode, 3:Controller Target , 4:AccFWD, 5:AccLeftRight,6:AccUP,"));<br /> Serial.print(F("7:Strut Name, 8:FL Strut STatus, 9:FL Target, 10:FL Strut Actual,")); <br /> Serial.print(F("11:Strut Name, 12:FR Strut STatus, 13:FR Target, 14:FR Strut Actual,")); <br /> Serial.print(F("15:Strut Name, 16:RL Strut STatus, 17:RL Target, 18:RL Strut Actual")); <br /> Serial.println(F("19:Strut Name, 20:RR Strut STatus, 21:RR Target, 22:RR Strut Actual "));<br /> <br /><br /> }<br /> else if (logMode ==1) logMode =0;<br /> break;<br /> <br /><br /> //<br /> // Changing Controller to Calibrate Accelerometer Mode<br /> //<br /> case 9: <br /><br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /> myController.setMode(CALIBRATEACCEL); // Change Controller to <br /><br /> //<br /> // Blink both lights at together (8 times) to indicate Calibration mode<br /> //<br /> blinkBothLights(9,500,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> return(CALIBRATEACCEL);<br /> break;<br /> }<br /><br /> }<br />}<br /><br />//*******************************************************************************************<br />//<br />// Read Diagnostic routine<br />//<br />//<br />//*******************************************************************************************<br />void readDiagnostics(){<br /><br /> while(readControllerSwitch()== NOCHANGE){ <br /><br /> // Turn Off All LEDS<br /> offLED(FL_LED);<br /> offLED(FR_LED);<br /> offLED(RL_LED);<br /> offLED(RR_LED);<br /><br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /><br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> <br /> //SET strut LED 1-&gt;4 to White to indicate testing Front Left Strut <br /> setLED(strut,D_WHITE);<br /> delay(1000);<br /> <br /> // Blink Sport Light number of times based on strut nuber as group 4 times To indicate strut<br /> blinkLights(SportLight, (strut+1),300,4,1000); <br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> <br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> // Display Final test results for this Strut<br /> if (myStruts[strut].errorStatus() == NO_FAILURES){<br /> setLED(strut,D_GREEN);<br /> setLights(TourLight,HIGH);<br /> setLights(SportLight,HIGH);<br /> delay(2500);<br /><br /> }else if (myStruts[strut].errorStatus() == MOTOR_COMMAND_FAILURE){<br /> blinkBothLights(3,300,3,1000,0);<br /> setLED(strut,D_RED);<br /><br /> }else if (myStruts[strut].errorStatus() == SIGNAL_WIRE_FAILURE){ <br /> setLED(strut,D_YELLOW);<br /><br /> for(int i=0;i&lt;8;i++){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> delay(250);<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> delay(250);<br /> }<br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }<br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /> <br /> delay(2000);<br /> }<br /> }<br />}<br /><br />//*******************************************************************************************<br />//<br />// Diagnostic routine<br />//<br />//<br />//*******************************************************************************************<br />void runDiagnostics(){<br /> <br /> while(readControllerSwitch()== NOCHANGE){ <br /><br /> // Turn Off All LEDS<br /> offLED(FL_LED);<br /> offLED(FR_LED);<br /> offLED(RL_LED);<br /> offLED(RR_LED);<br /><br /><br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //SET strut LED 1-&gt;4 to White to indicate testing Front Left Strut <br /> setLED(strut,D_WHITE);<br /> <br /> delay(1000);<br /> <br /> // Blink Sport Light number of times based on strut nuber as group 4 times To indicate strut<br /> blinkLights(SportLight, (strut+1),300,4,1000); <br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> //Attempt to set Strut HARD MODE<br /> setStrutDirectMode( strut, HARD, CLEAR);<br /> delay(1000);<br /> <br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> //Attempt to set Front Left Strut Medium MODE<br /> setStrutDirectMode( strut, MEDIUM, DONT_CLEAR);<br /> delay(1000);<br /> <br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /> <br /> //Attempt to set Front Left Strut Soft MODE<br /> setStrutDirectMode( strut, SOFT, DONT_CLEAR);<br /> delay(1000);<br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> // Display Final test results for this Strut<br /> if (myStruts[strut].errorStatus() == NO_FAILURES){<br /> setLED(strut,D_GREEN);<br /> setLights(TourLight,HIGH);<br /> setLights(SportLight,HIGH);<br /> delay(2500);<br /> }else{<br /> blinkBothLights(3,300,3,1000,0);<br /> setLED(FL_LED,D_RED);<br /> }<br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /> <br /> delay(2000);<br /> }<br /> }<br />}<br /><br />/*************************************************************<br /> *<br /> *<br /> * Routine to read int from EProm<br /> *<br /> *<br /> *************************************************************/<br />int eepromReadInt(int address){<br /> int value = 0x0000;<br /> value = value | (EEPROM.read(address) &lt;&lt; 8);<br /> value = value | EEPROM.read(address+1);<br /> return value;<br />}<br /><br />/*************************************************************<br /> *<br /> *<br /> * Routine to Write int to EProm<br /> *<br /> *<br /> *************************************************************/<br />void eepromWriteInt(int address, int value){<br /> EEPROM.write(address, (value &gt;&gt; 8) &amp; 0xFF );<br /> EEPROM.write(address+1, value &amp; 0xFF);<br />}<br /><br /><br />//******************************************************************************************<br />//<br />//LSM303DLHC I2C Device Routines<br />//<br />//******************************************************************************************<br /> <br />// Send register address and the byte value you want to write the accelerometer and <br />// loads the destination register with the value you send<br /><br />void WriteDevRegister(int devAddress, byte data, int regaddress) {<br /> Wire.beginTransmission(devAddress); // device Address<br /> Wire.write(regaddress);<br /> Wire.write(data); <br /> Wire.endTransmission(); <br />}<br /><br />//<br />//Send register address to this function and it returns byte value<br />//for the accelerometer register's contents <br />byte ReadDevRegister(int devAddress, int regaddress) {<br /><br /> byte data;<br /> Wire.beginTransmission(devAddress); // device Address <br /> Wire.write(regaddress);<br /> Wire.endTransmission();<br /> <br /> delayMicroseconds(100);<br /><br /> Wire.requestFrom(devAddress,1); // device Address <br /> data = Wire.read();<br /> Wire.endTransmission(); <br /><br /> delayMicroseconds(100);<br /><br /> return data; <br />} <br /><br />//**************************************************************************<br />//<br />// Accelerometer Initialization Routine<br />//<br />//**************************************************************************<br /><br />void initAccelerometer(void) {<br /><br /> WriteDevRegister(LSM303_ADDRESS_ACC,0x67,0x20); // Enable accelerometer, 200Hz data output<br /><br /> WriteDevRegister(LSM303_ADDRESS_MAG,0x9c,0x00); // Enable temperature sensor, 220Hz data output<br /> WriteDevRegister(LSM303_ADDRESS_MAG,0x20,0x01); // set gain to +/-1.3Gauss<br /> WriteDevRegister(LSM303_ADDRESS_MAG,0x00,0x02); // Enable magnetometer constant conversions<br />}<br /><br />//**************************************************************************<br />//<br />// Read the X,Y,Z axis values from the accelerometer <br />//<br />//**************************************************************************<br />void readAccelerometerValues() {<br /><br /> float Acc[3];<br /> //<br /> // ForwardAxis axis<br /> // ForwardSign +Acc = Acceleration<br /><br /> // LeftRightAxis axis<br /> // LeftRightSign +Acc = Left Turn<br /><br /> // UpDownAxis axis<br /> // UpDownSign +Acc = Up <br /><br /> //<br /> // reinitialize globals to prepare for new averages<br /> //<br /> ForwardAcceleration = 0.0;<br /> LeftRightAcceleration = 0.0;<br /> UpDownAcceleration = 0.0;<br /><br /> //<br /> // Shift all the previous readings down in the array &amp; Start summing the existing valid entries for the average calculation<br /> //<br /> for ( int i = accelDataPoints -1; i &gt; 0 ; i--){<br /> ForwardAccelerationArray[i] = ForwardAccelerationArray[i-1];<br /> ForwardAcceleration = ForwardAcceleration + ForwardAccelerationArray[i]; <br /><br /> LeftRightAccelerationArray[i] = LeftRightAccelerationArray[i-1]; <br /> LeftRightAcceleration = LeftRightAcceleration + LeftRightAccelerationArray[i]; <br /><br /> UpDownAccelerationArray[i] = UpDownAccelerationArray[i-1];<br /> UpDownAcceleration = UpDownAcceleration + UpDownAccelerationArray[i]; <br /><br /> }<br /><br /> //<br /> // get the raw Accelerometer current values off the I2C device<br /> // <br /> getAccelerometer(Acc);<br /><br /> //<br /> // Alias and Assign the current reading to the correct mappings in the normilized array [0]<br /> //<br /> ForwardAccelerationArray[0] = float (ForwardSign * Acc[ForwardAxis]);<br /> LeftRightAccelerationArray[0] = float (LeftRightSign * Acc[LeftRightAxis]); <br /> UpDownAccelerationArray[0] = float (UpDownSign * Acc[UpDownAxis]); <br /><br /><br /> //<br /> // add the current acceleration reading to the global value and finish average calculation<br /> //<br /> ForwardAcceleration = (ForwardAcceleration + ForwardAccelerationArray[0]) / accelDataPoints;<br /> LeftRightAcceleration = (LeftRightAcceleration + LeftRightAccelerationArray[0]) / accelDataPoints;<br /> UpDownAcceleration = (UpDownAcceleration + UpDownAccelerationArray[0]) / accelDataPoints;<br /> <br /> if(logMode) {<br /> Serial.print(ForwardAcceleration);<br /> Serial.print(F(","));<br /> Serial.print(LeftRightAcceleration);<br /> Serial.print(F(","));<br /> Serial.print(UpDownAcceleration);<br /> Serial.print(F(","));<br /> }<br /><br />} <br /><br />// ******************************************************************************k<br />//Readsthe X,Y,Z axis values from the accelerometer and sends the values to the <br />// ******************************************************************************k<br />void getAccelerometer(float *Acc) {<br /><br /> // accelerometer values<br /> byte xh = ReadDevRegister(LSM303_ADDRESS_ACC,0x29);<br /> byte xl = ReadDevRegister(LSM303_ADDRESS_ACC,0x28);<br /> byte yh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2B);<br /> byte yl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2A);<br /> byte zh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2D);<br /> byte zl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2C);<br /> <br /> // need to convert the register contents into a righ-justified 16 bit value<br /> Acc[0] = (xh&lt;&lt;8|xl); <br /> Acc[1] = (yh&lt;&lt;8|yl); <br /> Acc[2] = (zh&lt;&lt;8|zl); <br /><br /> //<br /> // Convert raw Accelerometer readings to g's using calibration values <br /> //<br /> if( Acc[0] &gt;= 0) Acc[0] = float( Acc[0] / XAxisPositive1gCalibration) ;<br /> else Acc[0] = float(Acc[0] / XAxisNegative1gCalibration ); <br /><br /> if( Acc[1] &gt;= 0) Acc[1] = float( Acc[1] / YAxisPositive1gCalibration) ;<br /> else Acc[1] = float(Acc[1] / YAxisNegative1gCalibration ); <br /><br /> if( Acc[2] &gt;= 0) Acc[2] = float( Acc[2] / ZAxisPositive1gCalibration) ;<br /> else Acc[2] = float(Acc[2] / ZAxisNegative1gCalibration ); <br /><br />} <br />// ******************************************************************************k<br />//Readsthe X,Y,Z axis values from the accelerometer and sends the values to the <br />// ******************************************************************************k<br />void getRawAccelerometer(float *Acc) {<br /><br /> // accelerometer values<br /> byte xh = ReadDevRegister(LSM303_ADDRESS_ACC,0x29);<br /> byte xl = ReadDevRegister(LSM303_ADDRESS_ACC,0x28);<br /> byte yh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2B);<br /> byte yl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2A);<br /> byte zh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2D);<br /> byte zl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2C);<br /> <br /> // need to convert the register contents into a righ-justified 16 bit value<br /> Acc[0] = (xh&lt;&lt;8|xl); <br /> Acc[1] = (yh&lt;&lt;8|yl); <br /> Acc[2] = (zh&lt;&lt;8|zl); <br /><br />} <br /><br />//******************************************************************<br />//Read &amp; Print Eprom Calibration Values<br />//******************************************************************<br />void readPrintEpromValues(){<br /> //<br /> // Now Read Back all the Values<br /> //<br /> XAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_X_NEG_1G); <br /> YAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Y_NEG_1G); <br /> ZAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Z_NEG_1G); <br /> XAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_X_POS_1G); <br /> YAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Y_POS_1G); <br /> ZAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Z_POS_1G); <br /><br /> <br /> Serial.println("**************** Accelerometer Calibration Values *********************"); <br /> Serial.print(F("EProm X Neg Axis 1g Calibration: "));<br /> Serial.println(XAxisNegative1gCalibration);<br /> Serial.print(F("EProm Y Neg Axis 1g Calibration: "));<br /> Serial.println(YAxisNegative1gCalibration);<br /> Serial.print(F("EProm Z Neg Axis 1g Calibration: "));<br /> Serial.println(ZAxisNegative1gCalibration);<br /> Serial.print(F("EProm X Pos Axis 1g Calibration: "));<br /> Serial.println(XAxisPositive1gCalibration);<br /> Serial.print(F("EProm Y Pos Axis 1g Calibration: "));<br /> Serial.println(YAxisPositive1gCalibration);<br /> Serial.print(F("EProm Z Pos Axis 1g Calibration: "));<br /> Serial.println(ZAxisPositive1gCalibration);<br /> Serial.println("*************************************************************************"); <br />}<br />//*************************************************************************************<br />//<br />//<br />// calibrateAccelerometer()<br />//<br />//<br />//************************************************************************************<br />void calibrateAccelerometer(){<br /><br /> float Acc[3];<br /> float prevAcc[3];<br /> <br /> <br /> int XAxis;<br /> int YAxis;<br /> int ZAxis;<br /><br /> int button;<br /><br /> //<br /> // Min Max accumulation variables<br /> //<br /> int XAxis_min = 0;<br /> int XAxis_max = 0;<br /> int YAxis_min = 0;<br /> int YAxis_max = 0;<br /> int ZAxis_min = 0;<br /> int ZAxis_max = 0; <br /> //<br /> // Initialize Min Max accumulation variables<br /> //<br /> XAxis_min = 0;<br /> XAxis_max = 0;<br /> YAxis_min = 0;<br /> YAxis_max = 0;<br /> ZAxis_min = 0;<br /> ZAxis_max = 0;<br /><br /> /*<br /> * Intialize to Zero<br /> */ <br /> for(int i=0; i&lt;3 ; i++){<br /> Acc[i] = 0;<br /> }<br /> <br /> // Turn off Lights while setting LEDs<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> Serial.println(F("********************* Calibration Routine ****************************"));<br /> Serial.println(" o Press Once to see EEprom Calibration &amp; Threshold values "); <br /> Serial.println(" o Press Twice To Exit Calibration Mode "); <br /> Serial.println(" o Press Three Times To Begin Calibration Mode "); <br /> Serial.println(" During Calibration - Press Once to Save Calibration Values &amp; Exit Calibration Routine"); <br /> Serial.println(F("**********************************************************************"));<br /> <br /> /*<br /> * Stay in this loop until button is pressed multple times to indicate save<br /> */ <br /> while(1){ <br /> <br /> //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br /> button = readPushButtonSwitch();<br /> switch(button){<br /><br /> case NOCHANGE:<br /> break;<br /><br /> case 1:<br /> //<br /> // Ok they want to print current calibration &amp; threshold values<br /> //<br /> //<br /> Serial.println(F("************************ Thresholds **********************************"));<br /> Serial.print(F("Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> Serial.println(F("**********************************************************************"));<br /> readPrintEpromValues();<br /> Serial.println(" o Press Once to see EEprom Calibration values "); <br /> Serial.println(" o Press Twice To Exit Calibration Mode "); <br /> Serial.println(" o Press Three Times To Begin Calibration Mode "); <br /> Serial.println(" During Calibration - Press Once to Save Calibration Values &amp; Exit Calibration Routine"); <br /><br /> break;<br /><br /><br /><br /> case 2:<br /> //<br /> // Ok they want to Exit <br /> //<br /> <br /> Serial.println(F("********************* Exiting Calibration Routine ****************************"));<br /> Serial.println(F("********************* NO Save Performed ****************************"));<br /> //<br /> // Blink both lights at together (3 times) to indicate exiting calibration<br /> //<br /> blinkBothLights(3,500,1,0,1);<br /> <br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /> break;<br /><br /> case 3:<br /> //<br /> // Ok they want to Calibrate Accelerometer <br /> //<br /> <br /> while(1){<br /> <br /> button = readPushButtonSwitch();<br /> if (button != NOCHANGE) break;<br /> <br /> for(int i=0; i&lt;3 ; i++){<br /> prevAcc[i] = Acc[i];<br /> }<br /> delay(100);<br /> getRawAccelerometer(Acc);<br /><br /> XAxis =Acc[0];<br /> YAxis =Acc[1];<br /> ZAxis =Acc[2];<br /> //<br /> // Check to make sure previous value = current value<br /> // to weed out noise from acc.<br /> //<br /> if(prevAcc[0] == Acc[0]){ <br /> if(Acc[0] &lt; XAxis_min) XAxis_min=XAxis;<br /> if(Acc[0] &gt; XAxis_max) XAxis_max=XAxis;<br /> }<br /><br /> if(prevAcc[1] == Acc[1]){ <br /> if(Acc[1] &lt; YAxis_min) YAxis_min=YAxis;<br /> if(Acc[1] &gt; YAxis_max) YAxis_max=YAxis;<br /> }<br /><br /> if(prevAcc[2] == Acc[2]){ <br /> if(Acc[2] &lt; ZAxis_min) ZAxis_min=ZAxis;<br /> if(Acc[2] &gt; ZAxis_max) ZAxis_max=ZAxis;<br /> }<br /><br />/*<br /> Serial.print(" X ");<br /> Serial.print(XAxis ); <br /><br /> Serial.print(" Y ");<br /> Serial.print(YAxis ); <br /><br /> Serial.print(" Z ");<br /> Serial.println(ZAxis ); <br />*/<br /> Serial.print("MIN X=");<br /> Serial.print(XAxis_min); <br /> Serial.print(" ");<br /> Serial.print("Y=");<br /> Serial.print(YAxis_min); <br /> Serial.print(" ");<br /> Serial.print("Z=");<br /> Serial.print(ZAxis_min); <br /><br /> Serial.print(" MAX X=");<br /> Serial.print(XAxis_max); <br /> Serial.print(" ");<br /> Serial.print("Y=");<br /> Serial.print(YAxis_max); <br /> Serial.print(" ");<br /> Serial.print("Z=");<br /> Serial.println(ZAxis_max); <br /><br /> }<br /><br /> // <br /> // ok their done calibrtaing save values<br /> //<br /> XAxis_min = abs(XAxis_min) ;<br /> eepromWriteInt(EPROM_ACC_X_NEG_1G , XAxis_min);<br /><br /> YAxis_min = abs(YAxis_min) ;<br /> eepromWriteInt(EPROM_ACC_Y_NEG_1G , YAxis_min);<br /><br /> ZAxis_min = abs(ZAxis_min) ;<br /> eepromWriteInt(EPROM_ACC_Z_NEG_1G , ZAxis_min);<br /><br /> eepromWriteInt(EPROM_ACC_X_POS_1G , XAxis_max);<br /> eepromWriteInt(EPROM_ACC_Y_POS_1G , YAxis_max);<br /> eepromWriteInt(EPROM_ACC_Z_POS_1G , ZAxis_max);<br /><br /><br /><br /> //<br /> // Now Read Back &amp; Print all the Values<br /> //<br /> Serial.println(F("************************** Thresholds *****************************"));<br /> Serial.print(F("Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> Serial.println(F("**********************************************************************"));<br /><br /> readPrintEpromValues(); <br /><br /> Serial.println(F("New Threshold Values have been saved - exiting calibration routine"));<br /><br /> //<br /> // Blink both lights at together (8 times) to indicate saving calibrations &amp; exiting.<br /> //<br /> blinkBothLights(8,500,1,0,1);<br /><br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /> break;<br /> <br /><br /> }<br /> }<br />}<br /><br /></pre> <pre>//****************************************************************************<br />//<br />// Description of Software: <br />//<br />// Mitsubishi 3000gt/Stealth Controller Program<br />// for controlling stock OEM mitsubishi ECS Struts.<br />// Mitsubish 3000gt model years 1991 - 1999.<br />// Note:models years 1995-1999 are retrofit of this system+Original Struts.<br />// Hardware Platform: ATAMega 328 MicroController<br />//<br />// Copyright (C) 2010,2011,2012,2013,2014,2015,2016<br />// Marcus Diaz, RenegadeTechWorks, LLC<br />//<br />//****************************************************************************<br />// Licensing:<br />// Licensed under GNU GPL Version 2<br />//<br />// This program is free software; you can redistribute it and/or modify<br />// it under the terms of the GNU General Public License as published by<br />// the Free Software Foundation under Version 2 of the License<br />//<br />// This program is distributed in the hope that it will be useful,<br />// but WITHOUT ANY WARRANTY; without even the implied warranty of<br />// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />// GNU General Public License for more details.<br />//<br />// You should have received a copy of the GNU General Public License along<br />// with this program; if not, write to the Free Software Foundation, Inc.,<br />// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.<br />//****************************************************************************<br />// VERSION HISTORY<br />//<br />//--------------------------------------------------------------------<br />// SW Ver : v3.00 <br />// HWDesign: ECM M03 PCB Rev <br />// Date: 03/08/2016 <br />// Comments: <br />// This code represents a branch and re-write for the Model 03 of <br />// of theTechWorks ECS controller.<br />// The new Gen3/ Model03(M03) Controller uses a modified MO1 design<br />// New Features are<br />// -- freed up I/O ports to support I2C buss devices<br />// -- includes add on daughther board support for an DLH303 6-axis Accellerometer<br />// -- Code has been rewritten in more Ojbect Oriented style.<br />// -- Auto Mode added to use accelerometer data to auto set struts<br />// -- Manual Mode remains<br />// <br />//<br />//--------------------------------------------------------------------<br />// SW Ver : v3.01 <br />// HWDesign: ECM M03 PCB Rev <br />// Date: 07/05/2016 <br />// Comments: <br />// - Added to startup sequence to retrieve from eprom the <br />// default startup mode<br />// - Added Sport/Tour light blinking patterns to startup sequence that<br />// shows what default startup mode is <br />// - Added Set Led Brightness Mode<br />// - Added Save current mode as default startup mode<br />// - Modes are:<br />// 1 push - set next stiffnes setting<br />// 2 pushes - toggle auto/manual modes<br />// 3 pushes - set led brightness<br />// 4 pushes - save curreent mode as default startup mode<br />// 5 pushes - read diagnostics mode<br />// 6 pushes - run diagnostics mode<br />//--------------------------------------------------------------------<br />// SW Ver : v3.01 <br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 07/09/2016 <br />// Comments: <br />// - fixed logic in sportTourLoopLights routine to correctly display <br />// command mode &amp; signal wire failures<br />// - changed manual strut mode to do struts one at a time<br />//<br />///--------------------------------------------------------------------<br />// SW Ver : v3.01 <br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 07/16/2016 <br />// Comments: <br />// - changed manual strut mode back to parrallel mode <br />// - change H/W design to use 1.5 amp 9v VReg for old strut<br />///--------------------------------------------------------------------<br />// SW Ver : v3.2<br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 07/25/2016 <br />// Comments: <br />// - added modes to set threshold values<br />// - added modes to Calibrate accelerometer<br />// - added code to use accelerometer calibration values<br />// - added code in setStrutMode to turn motors back on for old struts<br />///--------------------------------------------------------------------<br />// SW Ver : v3.3 Official Production Release Used in Shipping M03 Controllers<br />// HWDesign: ECM M03 PCB Rev 3.1<br />// Date: 10/17/2016 <br />// Comments: <br />// - added logging Mode<br />// - added firmware version &amp; startup values to print at startup<br />// - added seperate maxtries params for Auto vs Manual strut modes<br />// - added LED = OFF when strut state cannot be determined during transition<br />// - added LED = GREENYELLOW if strut goes into mode but cannot stay there<br />// - reworked the logic &amp; assignments in readStrutState() - to match reality<br />// - changed sport tour lights for automode to show max target across the four struts<br />///***********************************************************************************<br />#include &lt;EEPROM.h&gt;<br />#include &lt;FastLED.h&gt;<br />#include &lt;PinChangeInt.h&gt;<br />#include &lt;Wire.h&gt;<br /><br />//<br />//********* Firmware Version ****************<br />//<br />#define FIRMWARE_VERSION "Firmware Version M03 v3.3"<br /><br /><br />//<br />//********* LSM303DLHC Accelerometer/ Compass I2C Addresses ****************<br />//<br />#define LSM303_ADDRESS_ACC (0x32 &gt;&gt; 1) // 0011001x<br />#define LSM303_ADDRESS_MAG (0x3C &gt;&gt; 1) // 0011110x<br /><br />#define MANUAL 1<br />#define AUTO 2<br />#define DIAG 3<br />#define READDIAG 4<br />#define SETSTARTUP 5<br />#define SETLED 6<br />#define SETTHRESHOLD 7<br />#define CALIBRATEACCEL 8<br /><br />#define NOMINAL 0<br />#define CHANGING 1<br />#define FAILED 2<br />#define OLDSTRUTS 3<br />#define PRENOMINAL 4<br /><br />#define STRUT_FL 0<br />#define STRUT_FR 1<br />#define STRUT_RL 2<br />#define STRUT_RR 3<br /><br />#define HARD 1<br />#define MEDIUM 2<br />#define SOFT 3<br />#define UNKNOWN 7<br />#define NOCHANGE 10<br />#define STARTUP 11<br /><br />#define D_RED 1<br />#define D_GREEN 2<br />#define D_BLUE 3<br />#define D_TURQUOIS 4<br />#define D_PURPLE 5<br />#define D_YELLOW 6<br />#define D_GREENYELLOW 7<br />#define D_STARTUP 8<br />#define D_HARD 9<br />#define D_MEDIUM 10<br />#define D_SOFT 11<br />#define D_WHITE 12<br />#define D_OFF 13 <br />#define D_UNKNOWN 14 <br /><br /><br />#define RED100 0xFF0000 // Bright Red<br />#define GREEN100 0x00FF00 // Bright Green<br />#define BLUE100 0x0000FF // Bright Blue<br />#define RED001 0x010000 // Faint red<br />#define RED050 0x800000 // 1/2 red (0x80 = 128 out of 256)<br />#define RED025 0x300000 // 25% RED<br />#define GREEN050 0x008000 // half Green<br />#define GREEN025 0x003000 // 25% GREEN<br />#define BLUE050 0x000080 // half Blue<br />#define BLUE025 0x000030 // 25% BLUE<br />#define WHITE015 0x151515 // 15% White<br />#define WHITE010 0x101010 // 10% White<br />#define WHITE005 0x050505 // 05% White<br />#define WHITE001 0x020202 // 05% White<br />#define WHITE100 0xFFFFFF // 100% White<br /><br />#define LEDOFF 0x000000 <br />#define RED 0xFF0000 <br />#define ORANGE 0xFF5500<br />#define ORANGEYELLOW 0xFFA000<br />#define YELLOW 0xFFFF00<br />#define YELLOWGREEN 0xA0FF00<br />#define GREENYELLOW 0x50FF00<br />#define GREEN 0x00FF00<br />#define GREENCYAN 0x00FF4B<br />#define CYAN 0x00FFFF<br />#define CYANBLUE 0x00A0FF<br />#define BLUECYAN 0x005AFF <br />#define BLUE 0x0000FF<br />#define BLUEVIOLET 0x4800FF<br />#define VIOLETBLUE 0x7500FF<br />#define MAGENTA 0xFF00FF<br />#define PINKDEEP 0xFF1493<br />#define PINKHOT 0xFF69B4<br />#define PINK 0xF3967A<br /> <br />#define VIOLET 0xEE82EE<br /><br /><br />#define ERROR 1<br />#define NOERROR 0<br /><br />#define CLEAR 1<br />#define DONT_CLEAR 0<br /><br />#define SIGNAL_WIRE_FAILURE 2<br />#define MOTOR_COMMAND_FAILURE 1<br />#define NO_FAILURES 0<br /><br />#define AUTO_MAXTRIES 475 // maxtries = 475 = approximately 2.75 secs of Motor on<br /> // AUTO_MAXTRIES time equivalent should be less than STRUT_HOLD_INTERVAL<br />#define MANUAL_MAXTRIES 10000 // maxtries = 10000 = approximately 4 secs of Motor on<br />#define MAXFAILURES 100 // maxiumum number of faliures allowed for a strut during a power on cycle<br /><br /><br />#define MAXLIGHTLOOPS 20<br /><br />#define ROTARY 1<br />#define PUSHBUTTON 2<br />#define SAMEMODE 1<br />#define NEXTMODE 2<br /><br />#define LED_LEVEL1 1 // minimum brightness level<br />#define LED_LEVEL2 2<br />#define LED_LEVEL3 3<br />#define LED_LEVEL4 4 // Maximum brightness level<br /><br />// ******************************************************************<br />//<br />// EPROM ADDRESSES<br />//<br />// ******************************************************************<br />#define EPROM_LEDMODE 1 // address in EPROM for default start up LED Brightness Level<br />#define EPROM_CONTROLLER_MODE 3 // address in EPROM for default start up Controller Mode (Auto or Manual)<br />#define EPROM_STRUT_MODE 5 // address in EPROM for default start up Strut Stiffness Mode<br />#define EPROM_MEDIUM_THRESHOLD 7 // address in EPROM for default start up Strut Stiffness Mode<br />#define EPROM_HARD_THRESHOLD 9 // address in EPROM for default start up Strut Stiffness Mode<br />#define EPROM_ACC_X_NEG_1G 11 // address in EPROM for Accelerometer Negative X axis 1G Calibration Value <br />#define EPROM_ACC_Y_NEG_1G 13 // address in EPROM for Accelerometer Negative Y axis 1G Calibration Value <br />#define EPROM_ACC_Z_NEG_1G 15 // address in EPROM for Accelerometer Negative Z axis 1G Calibration Value <br />#define EPROM_ACC_X_POS_1G 17 // address in EPROM for Accelerometer Positive X axis 1G Calibration Value <br />#define EPROM_ACC_Y_POS_1G 19 // address in EPROM for Accelerometer Positive Y axis 1G Calibration Value <br />#define EPROM_ACC_Z_POS_1G 21 // address in EPROM for Accelerometer Positive Z axis 1G Calibration Value <br /><br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />// Map Board I/O Pins to World<br />//<br />// /////////////////////////////////////////////////////////////////////////////////////<br />// Front Left Strut Pins<br />#define STRUT_FL_SWITCH1 3 <br />#define STRUT_FL_SWITCH2 2<br />#define STRUT_FL_MOTOR 10<br /><br />// Front Right Strut Pins<br />#define STRUT_FR_SWITCH1 5 <br />#define STRUT_FR_SWITCH2 4<br />#define STRUT_FR_MOTOR 11<br /><br />// Rear Left Strut Pins<br />#define STRUT_RL_SWITCH1 7 <br />#define STRUT_RL_SWITCH2 6<br />#define STRUT_RL_MOTOR 14<br /><br />// Rear Right Strut Pins<br />#define STRUT_RR_SWITCH1 9 <br />#define STRUT_RR_SWITCH2 8<br />#define STRUT_RR_MOTOR 15<br /><br />//<br />// LED Light Ports <br />//<br />#define DATA_PIN 16 // Data pin that led data will be written out over<br /><br />//<br />// Switch State <br />//<br />#define NO_PUSH_TO_PROCESS 0<br />#define NEW_PUSH_OCCURED 1<br />#define CONTINUE_PROCESSING 2<br /><br /><br /><br />#define ACC_DATA_POINTS 10 // maximum number of Acceleration DataPoints<br />#define STRUT_HOLD_INTERVAL 3000 // numbers of milliseconds to hold a strut higher state;<br /><br />#define X_AXIS 0 // Acceleromter X Axis in Array<br />#define Y_AXIS 1 // Acceleromter Y Axis in Array <br />#define Z_AXIS 2 // Acceleromter Z Axis in Array <br /><br />//<br />// Logging modes<br />//<br />#define LOG_TIME 1<br />#define LOG_STRUTS_NAME 2<br />#define LOG_ACCELEROMETER 3<br /><br />//***************************************************************************************<br />//<br />// Create and Intialize (some ) Global Variables<br />//<br />//***************************************************************************************<br /><br />// <br />// Map Physical Accelerometer axis &amp; sign to normalized variables<br />//<br /><br />int ForwardAxis = Y_AXIS; // default to Y axis<br />int ForwardSign = 1; // default to Y+ +Acc = Acceleration<br /><br />int LeftRightAxis = Z_AXIS; // default to Z axis<br />int LeftRightSign = 1; // default to Z+ +Acc = Left Turn<br /><br />int UpDownAxis = X_AXIS; // default to X axis<br />int UpDownSign = 1; // default to X+ +Acc = Up <br />int accelDataPoints = 10; // default number of acceleration data points to average<br /><br />float ForwardAccelerationArray[ACC_DATA_POINTS] ;<br />float LeftRightAccelerationArray[ACC_DATA_POINTS] ;<br />float UpDownAccelerationArray[ACC_DATA_POINTS] ;<br /><br />float ForwardAcceleration;<br />float LeftRightAcceleration;<br />float UpDownAcceleration;<br /><br />//<br />// X Y Z Axis Calibration Variables<br />//<br />int XAxisNegative1gCalibration = 0;<br />int YAxisNegative1gCalibration = 0;<br />int ZAxisNegative1gCalibration = 0;<br />int XAxisPositive1gCalibration = 0;<br />int YAxisPositive1gCalibration = 0;<br />int ZAxisPositive1gCalibration = 0;<br /><br />//<br />// variables to contain integer level value that corresponds to actual decimal value of threshold<br />// 1 = 0.1g 2= 0.2g 3= 0.3g 4= 0.4g 5 =0.5g 6= 0.6g 7=0.7g<br />int mediumThresholdLevel; <br />int hardThresholdLevel; <br />//<br />// Default Threshold variables for Auto Mode Strut transitions in G's of force<br />//<br />float mediumThreshold = 0.2;<br />float hardThreshold = 0.4;<br /><br />//Mode Selector Switch Pin<br />#define SELECTOR A3 // A3=Analog pin A3=physical pin 26 on AT328) is part of the PinChangeInt.h library defines<br />int selectorSwitchPin = 17;<br /><br />//Switch Type Selector Option Pin<br />int switchTypeSelect = 1;<br /><br />//LED Type Selector Option Pin<br />int LEDTypeSelect = 0;<br /><br />// Sport Light Port<br />int SportLight = 12; <br /><br />// Tour Light Port<br />int TourLight = 13;<br /><br />//Print Mode 1=log/print 0=no logging<br />int logMode = 0;<br />int logSequence = 0;<br /> <br />long ledHardValue ;<br />long ledMediumValue;<br />long ledSoftValue;<br />long ledRedValue;<br /><br />#define FL_LED 0 // Front Left Strut LED<br />#define FR_LED 1 // Front Right Strut LED<br />#define RL_LED 2 // Rear Left Strut LED<br />#define RR_LED 3 // Rear Right Strut LED <br /><br />int lightLoopCounter = MAXLIGHTLOOPS;<br />int lightLoopCounterValue;<br /><br />int accelerometerStrutTargets[4];<br /><br />int previousMode;<br />//<br />// Allocate Structure For LED Strip<br />//<br />#define NUM_LEDS 8<br />struct CRGB ledColors[NUM_LEDS];<br />long ledCurrentValue[4];<br /><br /><br />unsigned long lightCycleStartTime =0;<br />unsigned long signalWireFailureCycleStartTime;<br />unsigned long commandFailureCycleStartTime;<br /><br />// **********************************<br />// Selector Switch variables &amp; Intterupt function<br />// **********************************<br /><br />/*<br />// Notice that anything that gets modified inside an interrupt, that I wish to access<br />// outside the interrupt, is marked "volatile". That tells the compiler not to optimize<br />// them.<br />*/<br />volatile uint8_t pushCount=0;<br />volatile uint8_t pushEvent= NO_PUSH_TO_PROCESS;<br />volatile unsigned long pushTime=0;<br /><br />int firstTime=1;<br /><br />//<br />// Interupt functgion for Selector Switch<br />//<br />void selectorInterruptfunc() {<br /> pushCount = pushCount +1;<br /> pushTime=millis();<br /> pushEvent = NEW_PUSH_OCCURED;<br />}<br /> <br /><br /><br />// **********************************<br />// Strut Object Class Definition<br />// **********************************<br />class Strut{<br /> public:<br /> Strut(){<br /><br /><br /> }<br /><br /> ~Strut(){}<br /><br /> //************************************<br /> int strutPosition() { <br /> return vstrutPosition; <br /> }<br /> int setStrutPosition(int position) { <br /> vstrutPosition = position; <br /> }<br /> //************************************<br /> int desiredState() { <br /> return vdesiredState; <br /> }<br /> int setDesiredState(int target) { <br /> vdesiredState = target; <br /> } <br /> //************************************<br /> unsigned long lastChangeTime() { <br /> return vlastChangeTime; <br /> }<br /> void setLastChangeTime() { <br /> vlastChangeTime= millis(); <br /> } <br /> //************************************<br /> int actualState(int display) { <br /> vstate = readStrutState(vstrutPosition,display);<br /> return vstate; <br /> }<br /> //***********************************<br /> int transitionStatus() { <br /> return vtransitionStatus; <br /> }<br /> int setTransitionStatus(int status) { <br /> vtransitionStatus = status; <br /> }<br /> //************************************<br /> int switch1() { <br /> return vs1Pin; <br /> }<br /><br /> int setSwitch1(int pin) { <br /> vs1Pin = pin; <br /> }<br /> //************************************<br /> int switch2() { <br /> return vs2Pin; <br /> }<br /> int setSwitch2(int pin) { <br /> vs2Pin = pin; <br /> }<br /> //************************************<br /> int motorPin() { <br /> return vmotorPin; <br /> }<br /> int setMotor(int pin) { <br /> vmotorPin = pin; <br /> }<br /> int motorOn() { <br /> digitalWrite(vmotorPin, HIGH);<br /> }<br /> int motorOff() { <br /> digitalWrite(vmotorPin, LOW);<br /> }<br /> //************************************<br /> int tryCount() { <br /> return vtryCount; <br /> }<br /> void clearTryCount() { <br /> vtryCount =0; <br /> }<br /> void incrementTryCount() { <br /> vtryCount++; <br /> }<br /> //************************************<br /> int failures() { <br /> return vtotalFailures; <br /> }<br /> void clearFailures() { <br /> vtotalFailures = 0; <br /> }<br /> void incrementFailures() { <br /> vtotalFailures++; <br /> }<br /><br /> //************************************<br /> int errorStatus() { <br /> return verrorStatus; <br /> }<br /> void setErrorStatus(int status) { <br /> verrorStatus = status;<br /> }<br /><br /><br /><br /> private:<br /> int vstrutPosition; // Strut Postion: STRUT_FL _FR _RL _RR <br /> int vstate; // Actual State of the Struts: HARD, MEDIUM, SOFT<br /> int vdesiredState; // Desired Target State based on last command loop execution: H, M, S<br /> <br /> int vtransitionStatus; // Indicate if the Strut in transition from last command mode to a new desired target or<br /> // Or is stable and in quiet state<br /> // Modes Are:<br /> // NOMINAL = Strut Actual Mode matches the DesiredCommand Target. <br /> // Motors should be off<br /> // CHANGING = Strut In a commanded transitional state changing<br /> // from Medium to Hard for example. Motors Should be On<br /> // Neutral = Everything has been shut down and Motors are off. Command Target is not guaranteed<br /> // Strut is whatever state it is in.<br /> <br /> int verrorStatus = NO_FAILURES; // Indicates current Error Status of Strut Possible Error Status modes are<br /> // NO_FAILURES Means no failure has previously Been detected Since Last Command given<br /> // SIGNAL_WIRE_FAILURE Means a signal wire failure has been previously detected <br /> // MOTOR_COMMAND_FAILURE Means a Motor Command failure has been previously detected <br /> <br /> int vtryCount = 0; // Indicates how many times previously a Strut has has passed thru the command loop<br /> // while trying to achive the desired targetState<br /> <br /> int vtotalFailures = 0; // Indicates how many total times previously a Strut has exceeded maxtries/tryCount<br /> // while trying to achive the desired targetState<br /><br /> int vs1Pin;<br /> int vs2Pin;<br /> int vmotorPin;<br /> unsigned long vlastChangeTime;<br />};<br /><br />// *********************************************************<br />// Controller Object Class Definition<br />// *********************************************************<br />class Controller{<br /> public:<br /> Controller(){<br /><br /><br /> }<br /><br /> ~Controller(){}<br /><br /> int readSwitch(){<br /> return(readControllerSwitch());<br /> }<br /> <br /> int mode() { <br /> return vcontrollerMode; <br /> }<br /> int setMode(int mode) { <br /> vcontrollerMode = mode; <br /> }<br /> int strutMode() { <br /> return vcontrollerStrutMode; <br /> }<br /> int setControllerStrutMode(int mode) { <br /> vcontrollerStrutMode = mode; <br /> }<br /><br /> int selectorType() { <br /> return(vselectorType);<br /> }<br /> int setSelectorType(int type) { <br /> vselectorType = type;<br /> }<br /><br /> //<br /> // Maybe put read switch as function of Controller??<br /> //<br /> private:<br /> int vcontrollerMode; // Auto, Manual, Diag<br /> int vcontrollerStrutMode; // Hard, Med, Soft<br /> int vselectorType = ROTARY; // selectorType captures what kind of selector switch is hooked up to unit<br />};<br /><br /><br /><br />//<br />// ** instantiate &amp; Initialize Strut Array Object **<br />//<br />Strut myStruts[4];<br /><br /><br />//<br />// ** instantiate &amp; Initialize Controller Array Object **<br />//<br />Controller myController;<br /><br /><br />int ledMode = LED_LEVEL2;<br /><br /><br />// Previous State of Selector Switch<br />int currentSelectorState = STARTUP;<br /><br /><br /><br /><br /> <br />///////////////////////////////////////////////////////////////////////////////////////<br />// setup initializes startup <br />///////////////////////////////////////////////////////////////////////////////////////<br />void setup(){<br /><br /> int strutStateFL; <br /> int strutStateFR; <br /> int strutStateRL; <br /> int strutStateRR; <br /><br /> int selectorValue;<br /> int diagLoopNumber;<br /> int epromValue;<br /><br /> <br /> // Initialize the Sport &amp; Tour Light OutPut Pins<br /> pinMode(SportLight, OUTPUT); <br /> pinMode(TourLight, OUTPUT);<br /> <br /> //<br /> // intialize all the motor &amp; Switch state pins<br /> //<br /> myStruts[0].setSwitch1(STRUT_FL_SWITCH1);<br /> myStruts[0].setSwitch2(STRUT_FL_SWITCH2);<br /> myStruts[0].setMotor(STRUT_FL_MOTOR);<br /> myStruts[0].setStrutPosition(STRUT_FL);<br /> myStruts[1].setSwitch1(STRUT_FR_SWITCH1);<br /> myStruts[1].setSwitch2(STRUT_FR_SWITCH2);<br /> myStruts[1].setMotor(STRUT_FR_MOTOR);<br /> myStruts[1].setStrutPosition(STRUT_FR);<br /> myStruts[2].setSwitch1(STRUT_RL_SWITCH1);<br /> myStruts[2].setSwitch2(STRUT_RL_SWITCH2);<br /> myStruts[2].setMotor(STRUT_RL_MOTOR);<br /> myStruts[2].setStrutPosition(STRUT_RL);<br /> myStruts[3].setSwitch1(STRUT_RR_SWITCH1);<br /> myStruts[3].setSwitch2(STRUT_RR_SWITCH2);<br /> myStruts[3].setMotor(STRUT_RR_MOTOR);<br /> myStruts[3].setStrutPosition(STRUT_RR);<br /><br /> for (int i=0; i &lt; 4; i++){<br /> initializeMotorIOPin(myStruts[i].motorPin());<br /> initializeStrutIOPin(myStruts[i].switch1());<br /> initializeStrutIOPin(myStruts[i].switch2());<br /> }<br /><br /> //<br /> // initialize current led value array<br /> //<br /> for (int i=0; i &lt; 4; i++){<br /> ledCurrentValue[i] = 0x00;<br /> }<br /> <br /> //<br /> //<br /> // Initialize Accelerometer <br /> //<br /> Wire.begin(); // Start up I2C, required for LSM303 communication<br /> initAccelerometer();<br /><br /><br /> // ************************************************************<br /> //<br /> // LED Setup &amp; Initialization<br /> //<br /> // ************************************************************<br /> <br /><br /> FastLED.addLeds&lt;WS2812B, DATA_PIN, RGB&gt;(ledColors, NUM_LEDS);<br /><br /> // Clear Out the LED arrays<br /> memset(ledColors, 0, NUM_LEDS * sizeof(struct CRGB)); <br /> LEDS.show(); //Push the current color frame to the LEDs<br /><br /> //<br /> // initialize current led value array<br /> //<br /> for (int i=0; i &lt; 4; i++){<br /> ledCurrentValue[i] = 0x00;<br /> }<br /><br /> // analyze Selector TYPE Input Pin To figure out if Rotary of Pushbutton is connected <br /> // HIGH = Rotary LOW = PUSHBUTTON<br /> pinMode(switchTypeSelect, INPUT);<br /> digitalWrite(switchTypeSelect,HIGH); <br /><br /> // Read the pin to see if it's HIGH or LOW <br /> if(digitalRead(switchTypeSelect) == HIGH){<br /> myController.setSelectorType(PUSHBUTTON);<br /><br /> /* original non-interrupt switch config<br /> pinMode(selectorSwitchPin, INPUT);<br /> */<br /> <br /> //<br /> // new code to use interrupt for pushbutton switch<br /> //<br /> pinMode(SELECTOR, INPUT_PULLUP);<br /> attachPinChangeInterrupt(SELECTOR, selectorInterruptfunc , RISING); // Rising state change will trigger the interrupt.<br /> // attachPinChangeInterrupt(SELECTOR, selectorInterruptfunc , CHANGE); // Any state change will trigger the interrupt.<br /> // attachPinChangeInterrupt(SELECTOR, selectorInterruptfunc , FALLING); // Falling state change will trigger the interrupt.<br />/*<br /> digitalWrite(selectorSwitchPin, HIGH);<br />*/<br /><br /> lightLoopCounterValue = 900; // the lightloopcounter is timing dependent based on selector type<br /> diagLoopNumber = 450;<br /> }else{<br /> myController.setSelectorType(ROTARY);<br /> pinMode(selectorSwitchPin, INPUT);<br /> digitalWrite(selectorSwitchPin, LOW);<br /> lightLoopCounterValue = 60; // the lightloopcounter is timing dependent based on selector type<br /> diagLoopNumber = 40;<br /> } <br /> lightLoopCounter = lightLoopCounterValue;<br /><br /><br /> //<br /> // Retrieve LED Brigtness level from EPROM - if not within range set it to 2nd level and write it back<br /> //<br /> ledMode = eepromReadInt(EPROM_LEDMODE); <br /><br /> //<br /> // Make sure the value is with in range<br /> //<br /> if (ledMode != LED_LEVEL1 &amp;&amp; ledMode != LED_LEVEL2 &amp;&amp; ledMode != LED_LEVEL3 &amp;&amp; ledMode != LED_LEVEL4){<br /> // It's not within range - set to LEVEL two and write it back<br /> ledMode = LED_LEVEL2;<br /> eepromWriteInt(EPROM_LEDMODE,ledMode);<br /> } <br /><br /> //<br /> // Set the LED brightness values based on EProm value<br /> //<br /> setLedValues();<br /><br /> <br /> // EPROM_MEDIUM_THRESHOLD <br /> // EPROM_HARD_THRESHOLD <br /> <br /> //<br /> // Retrieve medium threshold level from EPROM - if not within range set it to 2nd level and write it back <br /> //<br /> mediumThresholdLevel = eepromReadInt(EPROM_MEDIUM_THRESHOLD ); <br /> //<br /> // Make sure the value is with in range<br /> //<br /> if (mediumThresholdLevel &lt; 1 || mediumThresholdLevel &gt; 7 ){<br /> // It's not within range - set to LEVEL two and write it back<br /> mediumThresholdLevel = 2 ; <br /> eepromWriteInt(EPROM_MEDIUM_THRESHOLD, mediumThresholdLevel );<br /> } <br /> //<br /> // Set g threshold level based on EEProm<br /> // <br /> mediumThreshold = float( mediumThresholdLevel/10.0) ; <br /> <br /> //<br /> // Retrieve hard threshold level from EPROM - if not within range set it to 2nd level and write it back <br /> //<br /> hardThresholdLevel = eepromReadInt(EPROM_HARD_THRESHOLD ); <br /> //<br /> // Make sure the value is with in range<br /> //<br /> if (hardThresholdLevel &lt; 1 || hardThresholdLevel &gt; 9 ){<br /> // It's not within range - set to LEVEL two and write it back<br /> hardThresholdLevel = 4 ; <br /> eepromWriteInt(EPROM_HARD_THRESHOLD, hardThresholdLevel );<br /> } <br /> //<br /> // Set g threshold level based on EEProm<br /> // <br /> hardThreshold = float( hardThresholdLevel/10.0) ; <br /> <br /><br /> <br /> //<br /> // Retrieve Strut Mode from EPROM <br /> epromValue= eepromReadInt(EPROM_STRUT_MODE);<br /><br /> /*<br /> Serial.print(F(" Eprom Strut Mode ="));<br /> Serial.println(epromValue);<br /> */<br /><br /> //<br /> // if not valid eprom value set it to Soft; <br /> //<br /> //<br /> if (epromValue != HARD &amp;&amp; epromValue != MEDIUM &amp;&amp; epromValue != SOFT ){<br /> // It's not within range - set to Soft as default<br /> epromValue = SOFT;<br /> eepromWriteInt(EPROM_STRUT_MODE,epromValue);<br /> } <br /> myController.setControllerStrutMode(epromValue);<br /><br /> <br /><br /><br /> //<br /> // Retrieve X Axis Neg 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> XAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_X_NEG_1G); <br /> if (XAxisNegative1gCalibration &gt;32000 || XAxisNegative1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> XAxisNegative1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_X_NEG_1G , XAxisNegative1gCalibration);<br /> } <br /><br /> //<br /> // Retrieve Y Axis Neg 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> YAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Y_NEG_1G); <br /> if (YAxisNegative1gCalibration &gt;32000 || YAxisNegative1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> YAxisNegative1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Y_NEG_1G , YAxisNegative1gCalibration);<br /> } <br /><br /> //<br /> // Retrieve Z Axis Neg 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> ZAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Z_NEG_1G); <br /> if (ZAxisNegative1gCalibration &gt;32000 || ZAxisNegative1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> ZAxisNegative1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Z_NEG_1G , ZAxisNegative1gCalibration);<br /> } <br /><br /> //<br /> // Retrieve X Axis Positive 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> XAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_X_POS_1G); <br /> if (XAxisPositive1gCalibration &gt; 32000 || XAxisPositive1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> XAxisPositive1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_X_POS_1G , XAxisPositive1gCalibration );<br /> } <br /><br /> //<br /> // Retrieve Y Axis Positive 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> YAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Y_POS_1G); <br /> if (YAxisPositive1gCalibration &gt; 32000 || YAxisPositive1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> YAxisPositive1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Y_POS_1G , YAxisPositive1gCalibration );<br /> } <br /><br /> //<br /> // Retrieve Z Axis Positive 1g Galibration Value From Eprom if not with in range set to default value and write back <br /> //<br /> ZAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Z_POS_1G); <br /> if (ZAxisPositive1gCalibration &gt; 32000 || ZAxisPositive1gCalibration &lt; 1 ){<br /> // It's not within range - set to default and write it back<br /> ZAxisPositive1gCalibration = 16380;<br /> eepromWriteInt(EPROM_ACC_Z_POS_1G , ZAxisPositive1gCalibration );<br /> } <br /><br /><br /><br /> //<br /> // Turn on the Sport / Tour Lights at startup for 1 sec as test to driver to show they are working.<br /> //<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> delay(1000); <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> delay(300); <br /><br /> <br /> //<br /> // Retrieve Controller Mode from EPROM <br /> //<br /> epromValue= eepromReadInt(EPROM_CONTROLLER_MODE);<br /> /*<br /> Serial.print(F("Eprom Controller Mode ="));<br /> Serial.print(epromValue);<br /> */<br /> //<br /> // if controller mode not Auto or Manual set to manual<br /> //<br /> if (epromValue != AUTO &amp;&amp; epromValue != MANUAL ){<br /> // It's not within range - set to Manual as default<br /> epromValue = MANUAL;<br /> eepromWriteInt(EPROM_CONTROLLER_MODE,epromValue);<br /> } <br /> myController.setMode(epromValue);<br /><br /> //<br /> // Based on Controller default startup mode flash lights accordingly<br /> //<br /> if(epromValue == AUTO){<br /><br /> //<br /> // Blink both lights alternating(3 times) to indicate auto mode<br /> //<br /> for(int i=0;i&lt;2;i++){<br /> setLights(SportLight,HIGH);<br /> setLED(STRUT_FR,D_WHITE);<br /><br /> setLights(TourLight,LOW);<br /> setLED(STRUT_FL,D_OFF);<br /> <br /> delay(250);<br /> setLights(SportLight,LOW);<br /> setLED(STRUT_FR,D_OFF);<br /><br /> setLights(TourLight,HIGH);<br /> setLED(STRUT_FL,D_WHITE);<br /> delay(250);<br /> }<br /> }else if (epromValue == MANUAL){<br /> //<br /> // Blink both lights at together (3 times) to indicate manual mode<br /> //<br /> blinkBothLights(3,250,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> }<br /><br /><br /> lightCycleStartTime =millis();<br /> pushCount=0;<br /><br /> //<br /> // ********* Set Up for Logging &amp; Print some Initial Values<br /> //<br /> Serial.begin(115200); <br /> while (!Serial) {<br /> ; // wait for serial port to connect. Needed for Leonardo only<br /> };<br /> Serial.println(F("*************************************************************************"));<br /> Serial.println(F("Renegade Techworks Mitsubishi 3000gt/Stealth ECS Controller"));<br /> Serial.println(FIRMWARE_VERSION);<br /> Serial.print(F("Strut G-Force Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Strut G-Force Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> readPrintEpromValues();<br /><br />}<br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />//<br />// main loop <br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br /><br />void loop()<br />{<br /> <br /> //<br /> // Check for updates to the state of the Controller Mode <br /> // <br /> //<br /> myController.readSwitch();<br /><br /><br /> if(logMode) doLogMode(LOG_TIME,0,0);<br /><br /> switch(myController.mode()){<br /><br /> case(MANUAL):<br /> if(logMode) doLogMode(LOG_ACCELEROMETER,0,0);<br /> setManualStrutTargets(); <br /> setStruts(); <br /> // setManualStruts(); <br /> break;<br /><br /> case(AUTO):<br /> readAccelerometerValues(); <br /> setAccelerometerTargets();<br /> setAutoStrutTargets();<br /> setStruts(); <br /> break;<br /><br /> case(SETLED):<br /> setLed();<br /> break;<br /><br /> case(SETSTARTUP):<br /> setStartup(); <br /> break;<br /> <br /> case(DIAG):<br /> runDiagnostics();<br /> break;<br /><br /> case(READDIAG):<br /> readDiagnostics();<br /> break;<br /> <br /> case(SETTHRESHOLD):<br /> setAutoThreshold();<br /> break;<br /><br /> case(CALIBRATEACCEL):<br /> calibrateAccelerometer();<br /> break;<br /><br /> }<br /><br /> //<br /> // display current state of Sport-Tour lights<br /> // <br /> setLoopTourSportLights();<br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setAutoThreshold()<br />//<br />//<br />//************************************************************************************<br />void setAutoThreshold(){<br /> <br /> // Turn off Lights while setting LEDs<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> int autoThresholdSet = 0;<br /><br /> mediumThresholdLevel = 2;<br /> hardThresholdLevel = 4;<br /> <br /> /*<br /> * Stay in this loop until button is pressed multple times to indicate save<br /> */ <br /> while(1){ <br /> <br /> //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br /> switch(readPushButtonSwitch()){<br /><br /> case NOCHANGE:<br /> break;<br /><br /> case 1:<br /> //<br /> // Ok they want to change to next Threshold Values <br /> //<br /><br /> if(autoThresholdSet == 5) autoThresholdSet =1;<br /> else autoThresholdSet++;<br /><br /> //<br /> // Blink both lights at together x times to indicate which threshold set is selected<br /> //<br /> blinkBothLights(autoThresholdSet,500,1,0,1);<br /> switch(autoThresholdSet){<br /> case 1:<br /> mediumThresholdLevel = 1;<br /> hardThresholdLevel = 3;<br /> break;<br /><br /> case 2:<br /> mediumThresholdLevel = 2;<br /> hardThresholdLevel = 4;<br /> break;<br /> <br /> case 3:<br /> mediumThresholdLevel = 3;<br /> hardThresholdLevel = 5;<br /> break;<br /><br /> case 4:<br /> mediumThresholdLevel = 4;<br /> hardThresholdLevel = 6;<br /> break;<br /> <br /> case 5:<br /> mediumThresholdLevel = 5;<br /> hardThresholdLevel = 7;<br /> break;<br /> <br /> }<br /><br /> break;<br /><br /> case 2:<br /> case 3:<br /> case 4:<br /> //<br /> // Ok they want to save the auto Threshhold values and exit<br /> //<br /> eepromWriteInt(EPROM_MEDIUM_THRESHOLD,mediumThresholdLevel);<br /> eepromWriteInt(EPROM_HARD_THRESHOLD,hardThresholdLevel);<br /><br /> mediumThreshold = float( mediumThresholdLevel/10.0) ; <br /> hardThreshold = float( hardThresholdLevel/10.0) ; <br /> <br /> //<br /> // Blink both lights at together (7 times) to indicate saving Threshold mode<br /> //<br /> blinkBothLights(7,500,1,0,1);<br /> delay(500);<br /> //<br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /><br /> break;<br /> }<br /> }<br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setLed()<br />//<br />//<br />//************************************************************************************<br />void setLed(){<br /> <br /> // Turn off Lights while setting LEDs<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> /*<br /> * Display LedValues<br /> */<br /> setLedValues();<br /><br /> <br /> /*<br /> * Stay in this loop until button is pressed multple times to indicate save<br /> */ <br /> while(1){ <br /> <br /> //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br /> switch(readPushButtonSwitch()){<br /><br /> case NOCHANGE:<br /> break;<br /><br /> case 1:<br /> //<br /> // Ok they want to change to next LED Value <br /> //<br /><br /> if(ledMode == 4) ledMode =1;<br /> else ledMode++;<br /> <br /> setLedValues();<br /> break;<br /><br /> case 2:<br /> case 3:<br /> case 4:<br /> //<br /> // Ok they want to save the current LED Brightness value and exit<br /> //<br /> eepromWriteInt(EPROM_LEDMODE,ledMode);<br /> <br /> //<br /> // Blink both lights at together (3 times) to indicate LED mode<br /> //<br /> blinkBothLights(3,500,1,0,1);<br /><br /> //<br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /><br /> break;<br /> }<br /> }<br />}<br /><br /><br />//*************************************************************************************<br />//<br />//<br />// setLedValues<br />//<br />//<br />//************************************************************************************<br />void setLedValues(){<br /> <br /> switch(ledMode){<br /> case LED_LEVEL1:<br /> //SET the LEDs to Minimum Brightness Mode <br /> ledHardValue = rgbEncode(2,0,2);<br /> ledMediumValue = rgbEncode(0,2,2);<br /> ledSoftValue = rgbEncode(0,0,2);<br /> ledRedValue = rgbEncode(2,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /><br /> case LED_LEVEL2:<br /> //SET the LEDs to 2nd to Minimum Brightness Mode <br /> ledHardValue = rgbEncode(10,0,10);<br /> ledMediumValue = rgbEncode(0,10,5);<br /> ledSoftValue = rgbEncode(0,0,10);<br /> ledRedValue = rgbEncode(10,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /><br /> case LED_LEVEL3:<br /> //SET the LEDs to Medium Brightness Mode <br /> ledHardValue = rgbEncode(70,0,70);<br /> ledMediumValue = rgbEncode(0,70,35);<br /> ledSoftValue = rgbEncode(0,0,70);<br /> ledRedValue = rgbEncode(70,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /> <br /> case LED_LEVEL4:<br /> //SET the LEDs to Maximum Brightness Mode <br /> ledHardValue = rgbEncode(120,0,120);<br /> ledMediumValue = rgbEncode(0,120,60);<br /> ledSoftValue = rgbEncode(0,0,150);<br /> ledRedValue = rgbEncode(120,0,0);<br /> <br /> setLED(FL_LED, D_HARD);<br /> setLED(FR_LED, D_MEDIUM);<br /> setLED(RL_LED, D_SOFT);<br /> setLED(RR_LED, D_RED);<br /> break;<br /> }<br /><br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setSTARTUP()<br />//<br />//<br />//************************************************************************************<br />void setStartup(){<br /> int smode;<br /> //<br /> // Save current controller mode <br /> //<br /> eepromWriteInt(EPROM_CONTROLLER_MODE,previousMode);<br /> <br /> /*<br /> Serial.print(F(" Saving Controller Mode ="));<br /> Serial.print(previousMode);<br /> */ <br /><br /> //<br /> // Save current current controller strut mode<br /> //<br /> smode = myController.strutMode();<br /> eepromWriteInt(EPROM_STRUT_MODE,smode);<br /><br /> /*<br /> Serial.print(F(" Saving Strut Mode ="));<br /> Serial.println(smode);<br /> */ <br /><br /> //<br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode); <br /><br /> <br />}<br />//*************************************************************************************<br />//<br />//<br />// setStruts()<br />//<br />//<br />//************************************************************************************ <br />void setStruts(){<br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> if(logMode) doLogMode(LOG_STRUTS_NAME , strut,0);<br /> setStrutMode(strut);<br /> }<br /> <br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setManualStruts()<br />//<br />//<br />//************************************************************************************ <br />void setManualStruts(){<br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> setStrutDirectMode( strut, myStruts[strut].desiredState(), DONT_CLEAR);<br /> }<br /> <br />}<br /><br />//*************************************************************************************<br />//<br />//<br />// setManualStrutsTargets()<br />//<br />//<br />//************************************************************************************<br />void setManualStrutTargets(){<br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /><br /> <br /> //<br /> // See if we are changing states<br /> //<br /> if (myStruts[strut].desiredState() != myController.strutMode()){<br /><br /> //<br /> // Yes the human wants a new state - put it in motion<br /> //<br /> myStruts[strut].setDesiredState(myController.strutMode());<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> myStruts[strut].setLastChangeTime();<br /> myStruts[strut].clearTryCount();<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> }else {<br /> //<br /> // No Change - do nothing<br /> //<br /> }<br /> }<br /> <br />}<br />//*************************************************************************************<br />//<br />//<br />// setAutoStrutsTargets()<br />//<br />//<br />//************************************************************************************<br />void setAutoStrutTargets(){<br /><br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> //<br /> // See if we are changing states<br /> //<br /> if (myStruts[strut].desiredState() != accelerometerStrutTargets[strut]){<br /><br /> //<br /> // Yes the accelerometer readings dictate a new state - put it in motion<br /> //<br /> myStruts[strut].setDesiredState(accelerometerStrutTargets[strut]);<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> myStruts[strut].clearTryCount();<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> }else {<br /> //<br /> // No Change - do nothing<br /> //<br /> }<br /> }<br /> <br />}<br /><br /><br /><br />//*******************************************************************************************<br />//<br />//<br />// Rountine to only allow a strut downward change to Medium or Soft<br />// until after the hold interval has passed <br />//<br />//<br />//*******************************************************************************************<br />void setAutoStrutState(int strut, int mode){<br /> <br />unsigned long currentTime = millis();<br /><br /> if ( mode == SOFT &amp;&amp; myController.strutMode() == MEDIUM) mode = MEDIUM;<br /><br /> if (mode == HARD){<br /> <br /> accelerometerStrutTargets[strut] = mode;<br /> myStruts[strut].setLastChangeTime();<br /> <br /> } else if (mode == MEDIUM){<br /> if( accelerometerStrutTargets[strut] == HARD &amp;&amp; currentTime &lt; (myStruts[strut].lastChangeTime() + STRUT_HOLD_INTERVAL)) return;<br /><br /> accelerometerStrutTargets[strut] = MEDIUM;<br /> myStruts[strut].setLastChangeTime();<br /><br /> <br /> } else if (mode == SOFT){<br /><br /> if( (accelerometerStrutTargets[strut] == HARD ||accelerometerStrutTargets[strut] == MEDIUM) <br /> &amp;&amp; currentTime &lt; (myStruts[strut].lastChangeTime() + STRUT_HOLD_INTERVAL)) return;<br /><br /> accelerometerStrutTargets[strut] = SOFT;<br /> myStruts[strut].setLastChangeTime();<br /> }<br />}<br /><br /><br />//*******************************************************************************************<br />//<br />//<br />// Rountine to read current Accelerometer values and set Strut Targets <br />//<br />//<br />//*******************************************************************************************<br />void setAccelerometerTargets(){<br /><br /> <br /> /*<br /> ForwardAcceleration = 0.0;<br /> LeftRightAcceleration = 0.0;<br /> UpDownAcceleration = 0.0;<br /> STRUT_FL <br /> STRUT_FR <br /> STRUT_RL <br /> STRUT_RR<br /> */<br /><br /> /*<br /> Serial.print(F("Forward Accleration ="));<br /> Serial.print(ForwardAcceleration );<br /> Serial.print(F(" Left Right Accleration ="));<br /> Serial.println(LeftRightAcceleration );<br /> */<br /><br /> //<br /> // Check to see if we doing NO Acceleration or Braking ?<br /> //<br /> if (ForwardAcceleration &gt; (-1)*mediumThreshold &amp;&amp; ForwardAcceleration &lt; mediumThreshold ){ <br /> //<br /> // No Foward or Braking Acceleration <br /> // <br /><br /> /*<br /> Serial.println(F("No Forward or Braking Acceleration"));<br /> */ <br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set all struts to soft<br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // set rear struts to SOFT &amp; right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // set rear to SOFT &amp; left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // set rear struts to SOFT &amp; left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /> }<br /> <br /><br /> //<br /> //Check to see if we are doing MEDIUM range Forward Acceleration ?<br /> // <br /> } else if(ForwardAcceleration &gt;= mediumThreshold &amp;&amp; ForwardAcceleration &lt; hardThreshold ){<br /> //<br /> // Doing MEDIUM Forward Acceleration between mediumThresholdg's and hardThreshold <br /> // In this range the Rear Struts will be set to Medium Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /><br /> /*<br /> Serial.println(F("MEDIUM Forward Acceleration"));<br /> */<br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just rear struts to medium<br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set RIGHT side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // set RIGHT side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // set LEFT side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // set LEFT side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /> }<br /> <br /> //<br /> //Check to see if we are doing Hard range Forward Acceleration ?<br /> // <br /> } else if(ForwardAcceleration &gt;= hardThreshold ){ <br /> //<br /> // Doing HARD Forward Acceleration greater than hardThresholdgs <br /> // In this range the Rear Struts will be set to HARD Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /><br /> /*<br /> Serial.println(F("HARD Forward Acceleration"));<br /> */<br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just rear struts to HARD<br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // set rear struts to medium &amp; right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, SOFT);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // set rear &amp; left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // set rear struts to medium &amp; left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, SOFT);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> }<br /> <br /> //<br /> //Check to see if we are doing MEDIUM range Braking<br /> // <br /> } else if(ForwardAcceleration &gt; (-1)*hardThreshold &amp;&amp; ForwardAcceleration &lt; (-1)*mediumThreshold){ <br /> //<br /> // Doing MEDIUM range Braking between 2.0 &amp; 4.0 g's <br /> // In this range the Front Struts will be set to MEDIUM Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /><br /> /*<br /> * Serial.println(F("MEDIUM Braking"));<br /> */<br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just Front struts to HARD<br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // Set right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // Set left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, MEDIUM);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, MEDIUM);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /> }<br /> <br /> //<br /> //Check to see if we are doing HARD range Braking<br /> // <br /> } else if(ForwardAcceleration &lt;= (-1)*hardThreshold ){ <br /> //<br /> // Doing HARD range Braking greater than 4.0 g's <br /> // In this range the Front Struts will be set to Hard Mode<br /> // And any Lateral accelerations due to turns will set the LEFT or RIGHT Struts<br /> // <br /> <br /> /*<br /> Serial.println(F("Hard Braking"));<br /> */<br /><br /> if (LeftRightAcceleration &gt; (-1)*mediumThreshold &amp;&amp; LeftRightAcceleration &lt;mediumThreshold ) {<br /> //<br /> // No lateral Acceleration - set just Front struts to HARD<br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &gt;= mediumThreshold &amp;&amp; LeftRightAcceleration &lt;hardThreshold ) {<br /> //<br /> // Doing a MEDIUM Left Turn at same time <br /> // Set right side struts to medium <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, MEDIUM);<br /><br /> }else if (LeftRightAcceleration &gt;= hardThreshold ) {<br /> //<br /> // Doing a HARD Left Turn at same time- <br /> // Set right side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, SOFT);<br /> setAutoStrutState(STRUT_RR, HARD);<br /> <br /> }else if (LeftRightAcceleration &gt; (-1)*hardThreshold &amp;&amp; LeftRightAcceleration &lt;= (-1)*mediumThreshold ) {<br /> //<br /> // Doing a MEDIUM RIGHT Turn at same time- <br /> // Set left side struts to medium &amp; <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, MEDIUM);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /><br /> }else if (LeftRightAcceleration &lt;= (-1)*hardThreshold ) {<br /> //<br /> // Doing a HARD RIGHT Turn at same time- <br /> // left side struts to Hard <br /> // <br /> setAutoStrutState(STRUT_FL, HARD);<br /> setAutoStrutState(STRUT_FR, HARD);<br /> setAutoStrutState(STRUT_RL, HARD);<br /> setAutoStrutState(STRUT_RR, SOFT);<br /> }<br /> <br /> } <br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to make one pass thru a Strut to Set the desired strut to a particular mode <br />// this routine must be called in a loop<br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />void setStrutMode( int strut ){<br /><br /> int maxTries; <br /> int currentState;<br /> unsigned long currentTime;<br /><br /> currentTime = millis();<br /><br /> if (myController.mode() == MANUAL) maxTries = MANUAL_MAXTRIES;<br /> else if (myController.mode() == AUTO) maxTries = AUTO_MAXTRIES; <br /><br /> //<br /> // If this strut has exceeded maximum allowed failures bail <br /> // <br /> if (myStruts[strut].failures() &gt; MAXFAILURES){<br /> // Make sure Motor is Off<br /> myStruts[strut].motorOff();<br /> if(logMode){<br /> Serial.print("MAXFAILS,"); <br /> Serial.print("FAILED,"); <br /> Serial.println("FAILED"); <br /> }<br /> return;<br /> }<br /><br /> //<br /> // Jump to strut state<br /> // <br /> switch (myStruts[strut].transitionStatus()){<br /> <br /><br /> case OLDSTRUTS:<br /> //<br /> // This case deals with old struts where the motors &amp; mechansim<br /> // can cause the strut to have problems getting into mode<br /> // This state will purposely run this strut motor <br /> // for an additional 20msec to try an force it into mode <br /> // turn off the motors and then see if the strut is in mode<br /> // and then set state accordingly<br /> <br /><br /> <br /> if( currentTime &lt; myStruts[strut].lastChangeTime() + 20) break;<br /> <br /> //<br /> // OK the motors for this strut have been forced on for 20 msecs<br /> // Turn them off or make sure their still off. <br /> myStruts[strut].motorOff();<br /> <br /> //<br /> // Wait for an additional 10 msecs( 20+10) for strut &amp; switches to stablize<br /> //<br /> if( currentTime &lt; myStruts[strut].lastChangeTime() + 30) break;<br /> <br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if( currentState == myStruts[strut].desiredState() ){<br /><br /> // Ok Struts have held desired mode for 5 msecs<br /> // Switch to Nominal<br /> <br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(NOMINAL);<br /> myStruts[strut].setLastChangeTime();<br /><br /> }else{<br /> //<br /> // Ok we were not able to get the struts into the correct <br /> // mode using old struts mode<br /> // flag them as bad<br /> //<br /> myStruts[strut].motorOff();<br /> myStruts[strut].setLastChangeTime();<br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].setErrorStatus(MOTOR_COMMAND_FAILURE); <br /> myStruts[strut].incrementFailures();<br /> setLED(strut,D_GREENYELLOW);<br /><br /> }<br /> break;<br /> <br /> case PRENOMINAL:<br /><br /> //<br /> // This state is used just after a strut has indicated it<br /> // has achieved the desired mode<br /> // it will double check the state of after 5 msec<br /> // and declare it Nominal if it stays in the desired mode<br /> //<br /><br /> //<br /> // wait 10 msec before checking status<br /> //<br /> if(currentTime &lt; myStruts[strut].lastChangeTime() + 10) break;<br /><br /> currentState = myStruts[strut].actualState(ERROR); <br /><br /> if( currentState == myStruts[strut].desiredState() ){<br /><br /> // Ok Struts have held desired mode<br /> // Switch to Nominal<br /> <br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(NOMINAL);<br /> myStruts[strut].setLastChangeTime();<br /><br /> }else{<br /> //<br /> // Ok it looks like we might be dealing with old struts<br /> // turn the motors back on and set to Old Struts mode<br /> //<br /> myStruts[strut].motorOn();<br /> myStruts[strut].incrementTryCount();<br /> myStruts[strut].setLastChangeTime();<br /> myStruts[strut].setTransitionStatus(OLDSTRUTS);<br /><br /> }<br /> break;<br /><br /> case FAILED:<br /> if (myStruts[strut].errorStatus() == SIGNAL_WIRE_FAILURE){<br /> setLED(strut,D_YELLOW);<br /> }else if (myStruts[strut].errorStatus() == MOTOR_COMMAND_FAILURE){<br /> setLED(strut,D_RED);<br /> }<br /> break;<br /><br /> case NOMINAL:<br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if (currentState != myStruts[strut].desiredState()) {<br /> setLED(strut,D_YELLOW);<br /> myStruts[strut].setErrorStatus(SIGNAL_WIRE_FAILURE); <br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /> }<br /><br /> break;<br /><br /> case CHANGING:<br /> <br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if( myStruts[strut].tryCount() &lt; maxTries ){<br /><br /><br /> if( currentState == myStruts[strut].desiredState() ){<br /> //<br /> // Ok.. we just got the strut into the desired state<br /> //<br /> //<br /> <br /> // make sure the motors are off<br /> myStruts[strut].motorOff();<br /> <br /> // Strut looks like it is now in desired mode<br /> // change state to PRENOMINAL - that state will figure out if strut <br /> // really stayed in desired state<br /> //<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(PRENOMINAL);<br /> myStruts[strut].setLastChangeTime();<br /><br /> }else {<br /> //<br /> // Ok still not in correct state keep motors on<br /> myStruts[strut].motorOn();<br /> myStruts[strut].incrementTryCount();<br /> } <br /><br /> }else {<br /> //<br /> // Maxtries was exceeded - display RED <br /> //<br /><br /> // Turn the Motor Off<br /> myStruts[strut].motorOff();<br /><br /> setLED(strut,D_RED);<br /> myStruts[strut].setErrorStatus(MOTOR_COMMAND_FAILURE);<br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /><br /> }<br /> break;<br /> }<br /><br /> if(logMode){<br /> if (myStruts[strut].errorStatus() == SIGNAL_WIRE_FAILURE){<br /> Serial.print("WIRE_FAIL,"); <br /> }else if (myStruts[strut].errorStatus() == MOTOR_COMMAND_FAILURE){<br /> Serial.print("MOTOR_FAIL,"); <br /> }else if (myStruts[strut].errorStatus() == NO_FAILURES){<br /> Serial.print("STRUT_OK,"); <br /> }<br /><br /> if (myStruts[strut].desiredState() == HARD){<br /> Serial.print("T=HARD,"); <br /> }else if (myStruts[strut].desiredState() == MEDIUM){<br /> Serial.print("T=MEDIUM,"); <br /> }else if (myStruts[strut].desiredState() == SOFT){<br /> Serial.print("T=SOFT,"); <br /> }<br /> if(strut == 3){<br /> if (currentState == HARD){<br /> Serial.println("HARD"); <br /> }else if (currentState == MEDIUM){<br /> Serial.println("MEDIUM"); <br /> }else if (currentState == SOFT){<br /> Serial.println("SOFT"); <br /> }else Serial.println("Unknown"); <br /> }else{<br /> if (currentState == HARD){<br /> Serial.print("HARD, "); <br /> }else if (currentState == MEDIUM){<br /> Serial.print("MEDIUM, "); <br /> }else if (currentState == SOFT){<br /> Serial.print("SOFT, "); <br /> }else Serial.print("Unknown, "); <br /> }<br /> }<br /><br /><br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to Read actual status of strut , set LED color apprropriately and return value<br />//<br />//<br />///////////////////////////////////////////////////////////////////////////////////////<br />int readStrutState( int strut, int displayMode){<br /><br /> int strutS1;<br /> int strutS2;<br /> <br /> int switch1;<br /> int switch2;<br /> int switch1b;<br /> int switch2b;<br /><br /> int timeout = 0;<br /> int numberTries = 0;<br />/*<br /> unsigned long timestart;<br /> unsigned long timenow;<br /> timestart = millis();<br /> */<br /><br /> strutS1 = myStruts[strut].switch1();<br /> strutS2 = myStruts[strut].switch2();<br /><br /> //<br /> // intial switch reads<br /> //<br /> switch1 = digitalRead(strutS1);<br /> switch2 = digitalRead(strutS2);<br /> delayMicroseconds(50); //Wait for 100us to debounce contacts<br /> switch1b = digitalRead(strutS1);<br /> switch2b = digitalRead(strutS2);<br /><br /><br /> //<br /> // See if the switches are reading the same<br /> //<br /> while ((switch2 != switch2b || switch1 != switch1b)&amp;&amp; numberTries &lt; 40){<br /> switch1 = digitalRead(strutS1);<br /> switch2 = digitalRead(strutS2);<br /> delayMicroseconds(50); //Wait for 50us to debounce contacts<br /> switch1b = digitalRead(strutS1);<br /> switch2b = digitalRead(strutS2);<br /> numberTries++;<br /> <br /> }<br /> <br /> <br /> if (switch2 == HIGH &amp;&amp; switch1 == LOW &amp;&amp; numberTries &lt;40 ){<br /> // Strut is in HARD mode<br /> setLED(strut, D_HARD);<br /> return(HARD);<br /> <br /> }else if (switch2 == LOW &amp;&amp; switch1 == LOW &amp;&amp; numberTries &lt;40 ){<br /> // If Strut is in MEDIUM mode <br /> setLED(strut, D_MEDIUM);<br /> return(MEDIUM);<br /> <br /> }else if (switch2 == LOW &amp;&amp; switch1 == HIGH &amp;&amp; numberTries &lt;40 ){<br /> // Strut is in SOFT Mode<br /> setLED(strut, D_SOFT);<br /> return(SOFT);<br /> }else {<br /> // setLED(strut, D_UNKNOWN);<br /> setLED(strut, D_OFF);<br /> return(UNKNOWN);<br /> }<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// Routine to set a Strut to a desired mode <br />// this routine has it's own internal loop<br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />void setStrutDirectMode( int strut, int Mode,int Clear ){<br /> <br /> int currentState;<br /> int maxTries = 20000;<br /><br /><br /> /*<br /> SIGNAL_WIRE_FAILURE <br /> MOTOR_COMMAND_FAILURE <br /> NO_FAILURES <br /> if (myStruts[strut].desiredState() != myController.strutMode()){*<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setDesiredState(myController.strutMode());<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> STRUT_FL 0<br /> STRUT_FR 1<br /> STRUT_RL 2<br /> STRUT_RR 3<br /> */<br /><br /> if(Clear == CLEAR){<br /> myStruts[strut].clearFailures();<br /> myStruts[strut].clearTryCount();<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> <br /> } <br /><br /> myStruts[strut].setDesiredState(Mode);<br /> myStruts[strut].setTransitionStatus(CHANGING);<br /> <br /> while (myStruts[strut].transitionStatus() == CHANGING ){<br /> <br /> if (myStruts[strut].failures() &gt; MAXFAILURES){<br /> // Make sure Motor is Off<br /> myStruts[strut].motorOff();<br /> return;<br /> }<br /><br /> switch (myStruts[strut].transitionStatus()){<br /><br /> case FAILED:<br /> setLED(strut,D_RED);<br /> return;<br /> break;<br /><br /> case NOMINAL:<br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if (currentState != myStruts[strut].desiredState()) {<br /> setLED(strut,D_YELLOW);<br /> myStruts[strut].setErrorStatus(SIGNAL_WIRE_FAILURE); <br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /> }<br /> return;<br /><br /> case CHANGING:<br /> <br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if( myStruts[strut].tryCount() &lt; maxTries ){<br /><br /><br /> if( currentState == myStruts[strut].desiredState() ){<br /> //<br /> // Ok.. we just got the strut into the desired state<br /><br /> // delay for X milliseconds to make sure into mode.<br /> // delay(30); <br /> <br /> // Turn the Motor Off<br /> myStruts[strut].motorOff();<br /> <br /> // Strut is now in desired mode<br /> myStruts[strut].setErrorStatus(NO_FAILURES);<br /> myStruts[strut].setTransitionStatus(NOMINAL);<br /><br /> //<br /> // Pause for 1 second and do a final check to see if strut stayed in the desired mode<br /> //<br /> delay(1000);<br /><br /> currentState = myStruts[strut].actualState(ERROR); <br /> <br /> if (currentState != myStruts[strut].desiredState()) {<br /> setLED(strut,D_YELLOW);<br /> myStruts[strut].setErrorStatus(SIGNAL_WIRE_FAILURE); <br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /> }<br /><br /> }else {<br /> //<br /> // Ok still not in correct state keep motors on<br /> myStruts[strut].motorOn();<br /> myStruts[strut].incrementTryCount();<br /> } <br /><br /> }else {<br /> //<br /> // Maxtries was exceeded - display RED <br /> //<br /><br /> // Turn the Motor Off<br /> myStruts[strut].motorOff();<br /><br /> setLED(strut,D_RED);<br /> myStruts[strut].setErrorStatus(MOTOR_COMMAND_FAILURE);<br /> myStruts[strut].setTransitionStatus(FAILED);<br /> myStruts[strut].incrementFailures();<br /><br /> }<br /> break;<br /> }<br /> }<br />}<br /><br /><br /><br /><br />//*********************************************************************************************<br />//<br />//<br />// Logging Routine<br />//<br />//<br />//*********************************************************************************************<br /><br />void doLogMode(int mode,int val1,int val2){<br />unsigned long currentTime = millis();<br /> /*<br /> * Time, Controller Mode, Controller Target , Acc FWD Avg, Acc LeftRight Avg , Acc UP Avg,<br /> * Strut Name, FL Strut STatus, FL Target, FL Strut Actual <br /> * Strut Name, FR Strut STatus, FR Target, FR Strut Actual <br /> * Strut Name, RL Strut STatus, RL Target, RL Strut Actual <br /> * Strut Name, RR Strut STatus, RR Target, RR Strut Actual <br /> *<br /> */<br /><br /> switch (mode){<br /><br /> case LOG_TIME:<br /> Serial.print(currentTime);<br /> Serial.print(F(","));<br /> <br /> if(myController.mode() == MANUAL){<br /> Serial.print(F("MANUAL,"));<br /><br /> }else if(myController.mode() == AUTO){<br /> Serial.print(F("AUTO,"));<br /> }else{<br /> Serial.print(F("MODE?,"));<br /> }<br /> if (myController.strutMode() == HARD){<br /> Serial.print(F("HARD,"));<br /> }else if (myController.strutMode() == MEDIUM){<br /> Serial.print(F("MEDIUM,"));<br /> }else if (myController.strutMode() == SOFT){<br /> Serial.print(F("SOFT,"));<br /> }<br /><br /> break;<br /> <br /> case LOG_STRUTS_NAME:<br /> if (val1 == 0) Serial.print("FL,"); <br /> else if (val1 == 1) Serial.print("FR,"); <br /> else if (val1 == 2) Serial.print("RL,"); <br /> else if (val1 == 3) Serial.print("RR,"); <br /> break;<br /><br /> case LOG_ACCELEROMETER:<br /> readAccelerometerValues(); <br /><br /><br /> break;<br /> }<br />}<br /><br /><br /><br />//**************************************************************************<br />// this routine converts three seperate RGB values in to a correctly encoded RGB integer<br />//**************************************************************************<br />long rgbEncode(int red,int green,int blue){<br /> long rgb;<br /><br /> //if (red &gt; 255) red=255;<br /> //if (green &gt; 255) green=255;<br /> //if (blue &gt; 255) blue=255;<br /><br /> rgb = red*65536 + green*256 + blue;<br /><br /> return (rgb);<br /> <br />} <br /><br /><br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink one of the lights<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkLights(int lightaddress, int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay){<br /> <br /> setLights(lightaddress,LOW);<br /> <br /> for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br /> <br /> for (int count = numberBlinks; count &gt; 0; count--){<br /> delay(blinkDelay);<br /> setLights(lightaddress,HIGH);<br /> delay(blinkDelay);<br /> setLights(lightaddress,LOW);<br /> }<br /> delay(groupDelay);<br /> }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Blink Both of the lights at the same time<br />///////////////////////////////////////////////////////////////////////////////////////<br />void blinkBothLights(int numberBlinks,int blinkDelay,int numberGroupBlinks,int groupDelay,int doLed){<br /> <br /> // Turn off both Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> <br /> for (int groupcount = numberGroupBlinks; groupcount &gt; 0; groupcount--){<br /> <br /> for (int count = numberBlinks; count &gt; 0; count--){<br /> delay(blinkDelay);<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> if(doLed){<br /> setLED(STRUT_FL,D_WHITE);<br /> setLED(STRUT_FR,D_WHITE);<br /> }<br /> <br /> delay(blinkDelay);<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> if(doLed){<br /> setLED(STRUT_FL,D_OFF);<br /> setLED(STRUT_FR,D_OFF);<br /> }<br /> }<br /> delay(groupDelay);<br /> }<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to Set TourSport Lights during command loop<br />///////////////////////////////////////////////////////////////////////////////////////<br /><br />void setLoopTourSportLights(){<br /><br /> int strutMode;<br /> unsigned long currentTime = millis();<br /> <br /> if(currentTime &lt;= lightCycleStartTime + 30000){<br /> //<br /> // time to do Standard Lights Status<br /> //<br /><br /> if(myController.mode() == MANUAL) strutMode = myController.strutMode();<br /> else if (myController.mode() == AUTO) {<br /> strutMode = SOFT;<br /> for(int strut = 0; strut &lt; 4; strut++){<br /> if(myStruts[strut].desiredState() == MEDIUM &amp;&amp; strutMode == SOFT ) strutMode = MEDIUM;<br /> if(myStruts[strut].desiredState() == HARD &amp;&amp; ( strutMode == MEDIUM || strutMode == SOFT)) strutMode = HARD;<br /> }<br /> }<br /> switch (strutMode){<br /> case(HARD):<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> break;<br /><br /> case(MEDIUM):<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> break;<br /><br /> case(SOFT):<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> break;<br /> <br /> case(UNKNOWN):<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> break;<br /> } <br /><br /> signalWireFailureCycleStartTime = millis();<br /> <br /> }else if(lightCycleStartTime+30000 &lt; currentTime &amp;&amp; currentTime &lt;= lightCycleStartTime + 31500){<br /> //<br /> // time to do Signal Wire Status<br /> //<br /><br /> <br /> // if there was a problem start flashing shit.. to the do sport/tourlight error thing<br /> if (myStruts[STRUT_FL].errorStatus() == SIGNAL_WIRE_FAILURE || <br /> myStruts[STRUT_FR].errorStatus() == SIGNAL_WIRE_FAILURE ||<br /> myStruts[STRUT_RL].errorStatus() == SIGNAL_WIRE_FAILURE ||<br /> myStruts[STRUT_RR].errorStatus() == SIGNAL_WIRE_FAILURE ) {<br /> <br /><br /> if(currentTime &lt; signalWireFailureCycleStartTime + 250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 750 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 1000 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 1250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; signalWireFailureCycleStartTime + 1500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> }<br /> }<br /> commandFailureCycleStartTime = millis();<br /><br /> }else if(lightCycleStartTime+31500 &lt; currentTime &amp;&amp; currentTime &lt;= lightCycleStartTime + 33000){<br /> //<br /> // time to do Motor Command Status<br /> //<br /><br /> // if there was a problem start flashing shit.. to the do sport/tourlight error thing<br /> if (myStruts[STRUT_FL].errorStatus() == MOTOR_COMMAND_FAILURE || <br /> myStruts[STRUT_FR].errorStatus() == MOTOR_COMMAND_FAILURE ||<br /> myStruts[STRUT_RL].errorStatus() == MOTOR_COMMAND_FAILURE ||<br /> myStruts[STRUT_RR].errorStatus() == MOTOR_COMMAND_FAILURE ) {<br /> <br /><br /> if(currentTime &lt; commandFailureCycleStartTime + 250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 750 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 1000 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 1250 ){<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }else if(currentTime &lt; commandFailureCycleStartTime + 1500 ){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,HIGH);<br /> }<br /> }<br /> } else if (currentTime &gt; lightCycleStartTime + 33000) lightCycleStartTime = millis();<br /> <br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Motors<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeMotorIOPin(int pin){<br /><br /> // Set pin used for strut motor as output <br /> pinMode(pin, OUTPUT);<br /><br /> // Make sure Motor is off<br /> digitalWrite(pin,LOW);<br />}<br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to intialiaze IOPins for Strut Switches<br />///////////////////////////////////////////////////////////////////////////////////////<br />void initializeStrutIOPin(int pin){<br /><br /> // Set pin used for strut motor as output <br /> pinMode(pin, INPUT);<br /><br /> // Pull Pin HIGH <br /> digitalWrite(pin,HIGH);<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set an LED to a color by Address<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLED(int ledaddress, int color){<br /> //<br /> // rgbEncode(int red,int green,int blue)<br /> // red,green,blue = 0, 255<br /> //<br /><br /> switch(color){<br /> case D_HARD:<br /> setLedColors(ledaddress,ledHardValue);<br /> break; <br /> case D_MEDIUM:<br /> setLedColors(ledaddress,ledMediumValue);<br /> break; <br /> case D_SOFT:<br /> setLedColors(ledaddress,ledSoftValue);<br /> break; <br /> case D_RED:<br /> setLedColors(ledaddress,ledRedValue);<br /> break; <br /> case D_GREEN:<br /> setLedColors(ledaddress,rgbEncode(0,10,0));<br /> break; <br /> case D_BLUE:<br /> setLedColors(ledaddress,rgbEncode(0,0,25));<br /> break; <br /> case D_TURQUOIS:<br /> setLedColors(ledaddress,rgbEncode(0,40,64));<br /> break; <br /> case D_PURPLE:<br /> setLedColors(ledaddress,rgbEncode(10,0,10));<br /> break; <br /> case D_YELLOW:<br /> setLedColors(ledaddress,rgbEncode(15,15,0));<br /> break;<br /> case D_GREENYELLOW:<br /> setLedColors(ledaddress,rgbEncode(55,128,0));<br /> break;<br /> case D_STARTUP:<br /> setLedColors(ledaddress,rgbEncode(2,2,2));<br /> break; <br /> case D_WHITE:<br /> setLedColors(ledaddress,rgbEncode(15,15,15));<br /> break; <br /> case D_OFF:<br /> setLedColors(ledaddress,rgbEncode(0,0,0));<br /> break; <br /> case D_UNKNOWN:<br /> setLedColors(ledaddress,rgbEncode(53,18,0));<br /> break; <br /><br /> <br /> }<br /> // delayMicroseconds(600); //Wait for 200us to go into reset<br /> <br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to turn an LED off<br />//////////////////////////////////////////////////////////////////////////////////////<br />void offLED(int ledaddress){<br /> <br /> setLedColors(ledaddress,LEDOFF);<br />}<br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to set either the Tour Or Sport light to an HIGH/ON or LOW/OFF state<br />///////////////////////////////////////////////////////////////////////////////////////<br />void setLights(int light, int state){<br /> digitalWrite(light,state);<br />}<br /><br /><br />///////////////////////////////////////////////////////////////////////////////////////<br />// Routine to turn off all strut motors<br />///////////////////////////////////////////////////////////////////////////////////////<br />void motorsOff(){<br /> myStruts[0].motorOff();<br /> myStruts[1].motorOff();<br /> myStruts[2].motorOff();<br /> myStruts[3].motorOff();<br />}<br /><br />//****************************************************<br />//<br />// Convert Desired HEX color into rgb values and store in LED array<br />//<br />//****************************************************<br /><br />void setLedColors(int LED,long hexValue) {<br /> <br /> if( ledCurrentValue[LED] == hexValue ) return;<br /> ledCurrentValue[LED] = hexValue ;<br /> ledColors[LED].r = ((hexValue &gt;&gt; 16) &amp; 0xFF) ; // Extract the RR byte<br /> ledColors[LED].g = ((hexValue &gt;&gt; 8) &amp; 0xFF) ; // Extract the GG byte<br /> ledColors[LED].b = ((hexValue) &amp; 0xFF) ; // Extract the BB byte<br /><br /> ledColors[LED+4].r = ledColors[LED].r ;<br /> ledColors[LED+4].g = ledColors[LED].g ;<br /> ledColors[LED+4].b = ledColors[LED].b ;<br /><br /><br /> LEDS.show(); //PUSH the current color definitions of ALL the LEDs out<br />}<br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />//<br />//<br />// routine to read the switch pushes while setting LED brightness <br />//<br />//<br />//<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readPushButtonSwitch(){<br /> int numberPushes =0;<br /> unsigned long currentTime;<br /> currentTime = millis();<br /><br /> //<br /> // If no presses have occurred or &lt; 0.5 secs since push then return<br /> //<br /> if(pushCount == 0) return(NOCHANGE); <br /> if(currentTime &lt; (pushTime + 1500)) {<br /> /*<br /> Serial.print(F("PushCount &lt; 2000ms ="));<br /> Serial.println(pushCount);<br /> */<br /> return(NOCHANGE);<br /> }<br /><br /> /*<br /> Serial.print(F("PushCount &gt;2000 ="));<br /> Serial.println(pushCount);<br /> */<br /><br /><br /> //<br /> // Make sure pushCount is in valid range - if out of range set back to zero<br /> //<br /> if(pushCount &gt; 4){<br /> pushCount = 0;<br /> return (NOCHANGE); <br /> }<br /> numberPushes = pushCount;<br /> pushCount = 0;<br /> return(numberPushes);<br /><br /> /* <br /> * Alternate code for more complex return<br /> //<br /> // Ok button has been pressed one or more times and 0.5 seconds has passed since last push <br /> // now decide what new state we are in<br /> //<br /> switch (pushCount){<br /> case 1:<br /> pushCount = 0;<br /> return(1);<br /> break;<br /><br /> case 2: <br /> pushCount = 0;<br /> return(2);<br /> break;<br /> <br /> case 3: <br /> pushCount = 0;<br /> return(3);<br /> break;<br /> <br /> case 4: <br /> pushCount = 0;<br /> return(4);<br /> break;<br /> break;<br /><br /><br /> }<br /> */<br /><br />}<br /><br /><br /><br />/////////////////////////////////////////////////////////////////////////////////////<br />// Reference 5v = 1023<br />//<br />// Hard 0.0v / xxx<br />// Cutoff 0.415 / 85<br />// Medium 0.83v / xxx<br />// Cutoff 1.245 / 255<br />// Soft 1.66v / xxx<br />// Cutoff 2.075 / 424<br />// Diag 2.5 / xxx<br />// Cutoff 2.915 / 596<br />/////////////////////////////////////////////////////////////////////////////////////<br />int readControllerSwitch(){<br /> int voltage;<br /> int voltage2;<br /> float voltdiff;<br /><br /> unsigned long currentTime ;<br /><br /> // Based on the selector switch type do the appropriate read<br /> if( myController.selectorType() == ROTARY){<br /> <br /> voltage = analogRead(selectorSwitchPin)+1; // read the voltage <br /> delay(50); // debounce for 50 msec<br /> voltage2 = analogRead(selectorSwitchPin)+1;// read the voltage again<br /> voltdiff = abs(1.0-voltage/voltage2); // Calculate the percentage difference <br /><br /> //<br /> // as long as voltage difference &gt; 10% keep reading<br /> //<br /> while(voltdiff &gt; .1 ){<br /> voltage = analogRead(selectorSwitchPin)+1; // read the voltage <br /> delay(50); // debounce for 50 msec<br /> voltage2 = analogRead(selectorSwitchPin)+1;// read the voltage again<br /> voltdiff = abs(1.0-voltage/voltage2); // Calculate the percentage difference <br /> }<br /><br /> //<br /> // based on the converted voltage value and the range return the value<br /> //<br /> if(voltage &lt; 85){ <br /> // Selector is in HARD mode <br /> if (myController.strutMode() == HARD) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(MANUAL);<br /> myController.setControllerStrutMode(HARD);<br /> return(HARD);<br /> }<br /> <br /> }else if (voltage &gt;= 85 &amp;&amp; voltage &lt; 255){ <br /> // Selector is in MEDIUM mode <br /> if (myController.strutMode() == MEDIUM) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(MANUAL);<br /> myController.setControllerStrutMode(MEDIUM);<br /> return(MEDIUM);<br /> }<br /><br /> }else if (voltage &gt;= 255 &amp;&amp; voltage &lt; 424){<br /> // Selector is in SOFT mode <br /> if (myController.strutMode() == SOFT) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(MANUAL);<br /> myController.setControllerStrutMode(SOFT);<br /> return(SOFT);<br /> }<br /><br /> }else if(voltage &gt;= 424 &amp;&amp; voltage &lt;596){<br /> // Selector is in DIAG mode <br /> if (myController.mode() == DIAG) return (NOCHANGE) ;<br /> else {<br /> myController.setMode(DIAG);<br /> return(DIAG);<br /> }<br /><br /> }else{<br /> myController.setMode(UNKNOWN);<br /> return (UNKNOWN);<br /><br /> }<br /> }else if( myController.selectorType() == PUSHBUTTON){<br /> currentTime = millis();<br /> //<br /> // If no presses have occurred or &lt; 1.5 secs since last push then return<br /> //<br /> if(pushCount ==0) return(NOCHANGE); <br /> if(currentTime &lt; (pushTime + 1500)) {<br /> /*<br /> Serial.print(F("PushCount &lt; 2000ms ="));<br /> Serial.println(pushCount);<br /> */<br /> return(NOCHANGE);<br /><br /> }<br /> /*<br /> Serial.print(F("PushCount &gt;2000 ="));<br /> Serial.println(pushCount);<br /> */<br /> //<br /> // Make sure pushCount is in valid range - if out of range set back to zero<br /> //<br /> if(pushCount &gt; 9){<br /> pushCount = 0;<br /> return (NOCHANGE); <br /> }<br /><br /> //<br /> // Ok button has been pressed one or more times and 1.5 seconds has passed since last push <br /> // now decide what new state we are in<br /> //<br /> switch (pushCount){<br /> //<br /> // Changing to next strut mode using sequence H -&gt; S -&gt; M -&gt; H -&gt; S -&gt; M -&gt;....<br /> case 1:<br /><br /> pushCount = 0;<br /><br /> //<br /> // If in Diag or readDiag mode and single push occurs treat this as an abort to go back to manual mode<br /> //<br /> if(myController.mode() == DIAG || myController.mode() == READDIAG){<br /> myController.setMode(MANUAL);<br /> return(myController.strutMode());<br /> }<br /> <br /> <br /> switch(myController.strutMode()){<br /> case HARD:<br /> myController.setControllerStrutMode(SOFT);<br /> return(SOFT);<br /> break;<br /><br /> case MEDIUM:<br /> if(myController.mode() == AUTO) myController.setControllerStrutMode(SOFT);<br /> else myController.setControllerStrutMode(HARD);<br /> return(HARD);<br /> break;<br /><br /> case SOFT:<br /> myController.setControllerStrutMode(MEDIUM);<br /> return(MEDIUM);<br /> break;<br /> }<br /> break;<br /> //<br /> // Changing Controller mode from Auto to Manual or vice versa<br /> case 2: <br /> motorsOff();<br /><br /> pushCount = 0;<br /> switch(myController.mode()){<br /> case MANUAL:<br /><br /> myController.setMode(AUTO); // Change controller to Auto MODE<br /> myController.setControllerStrutMode(SOFT);<br /><br /> //<br /> // Blink both lights alternating(3 times) to indicate auto mode<br /> //<br /> for(int i=0;i&lt;2;i++){<br /> setLights(SportLight,HIGH);<br /> setLED(STRUT_FR,D_WHITE);<br /> <br /> setLights(TourLight,LOW);<br /> setLED(STRUT_FL,D_OFF);<br /> <br /> delay(250);<br /> setLights(SportLight,LOW);<br /> setLED(STRUT_FR,D_OFF);<br /> <br /> setLights(TourLight,HIGH);<br /> setLED(STRUT_FL,D_WHITE);<br /> delay(250);<br /> }<br /><br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> setLED(STRUT_FL,D_OFF);<br /> setLED(STRUT_FR,D_OFF);<br /> <br /> return(myController.strutMode());<br /> break;<br /><br /> case AUTO:<br /> case DIAG:<br /> case READDIAG:<br /> <br /> myController.setMode(MANUAL); // Change Controller to Manual MODE<br /><br /> //<br /> // Blink both lights at together (3 times) to indicate manual mode<br /> //<br /> blinkBothLights(3,250,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> return(myController.strutMode());<br /><br /> break;<br /><br /> }<br /> break;<br /><br /> //<br /> // Changing Controller LED Settings<br /> //<br /> case 3: <br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /> myController.setMode(SETLED); // Change Controller to change LED MODE<br /><br /> //<br /> // Blink both lights at together (3 times) to indicate LED mode<br /> //<br /> blinkBothLights(3,500,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> break; <br /><br /> //<br /> // Set default startup Mode<br /> //<br /> case 4: <br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /><br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //<br /> // Blink both lights at together (4 times) to indicate saving startup mode<br /> //<br /> blinkBothLights(4,500,1,0,1);<br /><br /> myController.setMode(SETSTARTUP); // Change Controller to change LED MODE<br /> break; <br /><br /> //<br /> // Changing Controller to Read Diagnostics Mode<br /> //<br /> case 5: <br /> motorsOff();<br /> myController.setMode(READDIAG);<br /> pushCount = 0;<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //<br /> // Blink both lights at together (5 times) to indicate read diag mode<br /> //<br /> blinkBothLights(5,500,1,0,1);<br /> delay(500);<br /><br /> return(READDIAG);<br /><br /> break;<br /> <br /> //<br /> // Changing Controller to RUN Diagnostics Mode<br /> //<br /> case 6: <br /> motorsOff();<br /> myController.setMode(DIAG);<br /> pushCount = 0;<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //<br /> // Blink both lights at together (6 times) to indicate saving startup mode<br /> //<br /> blinkBothLights(6,500,1,0,1);<br /> delay(500);<br /><br /> return(DIAG);<br /> break;<br /><br /> //<br /> // Changing Controller to Set Thresholds Mode<br /> //<br /> case 7: <br /><br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /> myController.setMode(SETTHRESHOLD); // Change Controller to change LED MODE<br /><br /> //<br /> // Blink both lights at together (7 times) to indicate Set Threshold mode<br /> //<br /> blinkBothLights(7,500,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> return(SETTHRESHOLD);<br /> break;<br /><br /><br /> //<br /> // Toggle Controller in out of logging mode<br /> //<br /> case 8: <br /> //<br /> // Blink both lights at together (9 times) to indicate logging mode on or Off<br /> //<br /> pushCount = 0;<br /> blinkBothLights(8,500,1,0,1);<br /> if(logMode == 0) {<br /> logMode =1;<br /> logSequence++;<br /> Serial.println(F("**********************************************************************"));<br /> Serial.print(F("LogSequence #:"));<br /> Serial.println(logSequence);<br /> Serial.println(F("******** Strut GForce Transition Thresholds ********"));<br /> Serial.print(F("Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> readPrintEpromValues();<br /> Serial.print(F("1:Time, 2:Controller Mode, 3:Controller Target , 4:AccFWD, 5:AccLeftRight,6:AccUP,"));<br /> Serial.print(F("7:Strut Name, 8:FL Strut STatus, 9:FL Target, 10:FL Strut Actual,")); <br /> Serial.print(F("11:Strut Name, 12:FR Strut STatus, 13:FR Target, 14:FR Strut Actual,")); <br /> Serial.print(F("15:Strut Name, 16:RL Strut STatus, 17:RL Target, 18:RL Strut Actual")); <br /> Serial.println(F("19:Strut Name, 20:RR Strut STatus, 21:RR Target, 22:RR Strut Actual "));<br /> <br /><br /> }<br /> else if (logMode ==1) logMode =0;<br /> break;<br /> <br /><br /> //<br /> // Changing Controller to Calibrate Accelerometer Mode<br /> //<br /> case 9: <br /><br /> motorsOff();<br /> pushCount = 0;<br /> previousMode = myController.mode();<br /> myController.setMode(CALIBRATEACCEL); // Change Controller to <br /><br /> //<br /> // Blink both lights at together (8 times) to indicate Calibration mode<br /> //<br /> blinkBothLights(9,500,1,0,1);<br /> <br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> return(CALIBRATEACCEL);<br /> break;<br /> }<br /><br /> }<br />}<br /><br />//*******************************************************************************************<br />//<br />// Read Diagnostic routine<br />//<br />//<br />//*******************************************************************************************<br />void readDiagnostics(){<br /><br /> while(readControllerSwitch()== NOCHANGE){ <br /><br /> // Turn Off All LEDS<br /> offLED(FL_LED);<br /> offLED(FR_LED);<br /> offLED(RL_LED);<br /> offLED(RR_LED);<br /><br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /><br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> <br /> //SET strut LED 1-&gt;4 to White to indicate testing Front Left Strut <br /> setLED(strut,D_WHITE);<br /> delay(1000);<br /> <br /> // Blink Sport Light number of times based on strut nuber as group 4 times To indicate strut<br /> blinkLights(SportLight, (strut+1),300,4,1000); <br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> <br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> // Display Final test results for this Strut<br /> if (myStruts[strut].errorStatus() == NO_FAILURES){<br /> setLED(strut,D_GREEN);<br /> setLights(TourLight,HIGH);<br /> setLights(SportLight,HIGH);<br /> delay(2500);<br /><br /> }else if (myStruts[strut].errorStatus() == MOTOR_COMMAND_FAILURE){<br /> blinkBothLights(3,300,3,1000,0);<br /> setLED(strut,D_RED);<br /><br /> }else if (myStruts[strut].errorStatus() == SIGNAL_WIRE_FAILURE){ <br /> setLED(strut,D_YELLOW);<br /><br /> for(int i=0;i&lt;8;i++){<br /> setLights(SportLight,HIGH);<br /> setLights(TourLight,LOW);<br /> delay(250);<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,HIGH);<br /> delay(250);<br /> }<br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /> }<br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /> <br /> delay(2000);<br /> }<br /> }<br />}<br /><br />//*******************************************************************************************<br />//<br />// Diagnostic routine<br />//<br />//<br />//*******************************************************************************************<br />void runDiagnostics(){<br /> <br /> while(readControllerSwitch()== NOCHANGE){ <br /><br /> // Turn Off All LEDS<br /> offLED(FL_LED);<br /> offLED(FR_LED);<br /> offLED(RL_LED);<br /> offLED(RR_LED);<br /><br /><br /><br /> for(int strut = 0; strut &lt; 4; strut++){<br /> // Turn off Lights<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> //SET strut LED 1-&gt;4 to White to indicate testing Front Left Strut <br /> setLED(strut,D_WHITE);<br /> <br /> delay(1000);<br /> <br /> // Blink Sport Light number of times based on strut nuber as group 4 times To indicate strut<br /> blinkLights(SportLight, (strut+1),300,4,1000); <br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> //Attempt to set Strut HARD MODE<br /> setStrutDirectMode( strut, HARD, CLEAR);<br /> delay(1000);<br /> <br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> //Attempt to set Front Left Strut Medium MODE<br /> setStrutDirectMode( strut, MEDIUM, DONT_CLEAR);<br /> delay(1000);<br /> <br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /> <br /> //Attempt to set Front Left Strut Soft MODE<br /> setStrutDirectMode( strut, SOFT, DONT_CLEAR);<br /> delay(1000);<br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /><br /> // Display Final test results for this Strut<br /> if (myStruts[strut].errorStatus() == NO_FAILURES){<br /> setLED(strut,D_GREEN);<br /> setLights(TourLight,HIGH);<br /> setLights(SportLight,HIGH);<br /> delay(2500);<br /> }else{<br /> blinkBothLights(3,300,3,1000,0);<br /> setLED(FL_LED,D_RED);<br /> }<br /><br /> // See if abort desired<br /> if(readControllerSwitch()!= NOCHANGE) return; <br /> <br /> delay(2000);<br /> }<br /> }<br />}<br /><br />/*************************************************************<br /> *<br /> *<br /> * Routine to read int from EProm<br /> *<br /> *<br /> *************************************************************/<br />int eepromReadInt(int address){<br /> int value = 0x0000;<br /> value = value | (EEPROM.read(address) &lt;&lt; 8);<br /> value = value | EEPROM.read(address+1);<br /> return value;<br />}<br /><br />/*************************************************************<br /> *<br /> *<br /> * Routine to Write int to EProm<br /> *<br /> *<br /> *************************************************************/<br />void eepromWriteInt(int address, int value){<br /> EEPROM.write(address, (value &gt;&gt; 8) &amp; 0xFF );<br /> EEPROM.write(address+1, value &amp; 0xFF);<br />}<br /><br /><br />//******************************************************************************************<br />//<br />//LSM303DLHC I2C Device Routines<br />//<br />//******************************************************************************************<br /> <br />// Send register address and the byte value you want to write the accelerometer and <br />// loads the destination register with the value you send<br /><br />void WriteDevRegister(int devAddress, byte data, int regaddress) {<br /> Wire.beginTransmission(devAddress); // device Address<br /> Wire.write(regaddress);<br /> Wire.write(data); <br /> Wire.endTransmission(); <br />}<br /><br />//<br />//Send register address to this function and it returns byte value<br />//for the accelerometer register's contents <br />byte ReadDevRegister(int devAddress, int regaddress) {<br /><br /> byte data;<br /> Wire.beginTransmission(devAddress); // device Address <br /> Wire.write(regaddress);<br /> Wire.endTransmission();<br /> <br /> delayMicroseconds(100);<br /><br /> Wire.requestFrom(devAddress,1); // device Address <br /> data = Wire.read();<br /> Wire.endTransmission(); <br /><br /> delayMicroseconds(100);<br /><br /> return data; <br />} <br /><br />//**************************************************************************<br />//<br />// Accelerometer Initialization Routine<br />//<br />//**************************************************************************<br /><br />void initAccelerometer(void) {<br /><br /> WriteDevRegister(LSM303_ADDRESS_ACC,0x67,0x20); // Enable accelerometer, 200Hz data output<br /><br /> WriteDevRegister(LSM303_ADDRESS_MAG,0x9c,0x00); // Enable temperature sensor, 220Hz data output<br /> WriteDevRegister(LSM303_ADDRESS_MAG,0x20,0x01); // set gain to +/-1.3Gauss<br /> WriteDevRegister(LSM303_ADDRESS_MAG,0x00,0x02); // Enable magnetometer constant conversions<br />}<br /><br />//**************************************************************************<br />//<br />// Read the X,Y,Z axis values from the accelerometer <br />//<br />//**************************************************************************<br />void readAccelerometerValues() {<br /><br /> float Acc[3];<br /> //<br /> // ForwardAxis axis<br /> // ForwardSign +Acc = Acceleration<br /><br /> // LeftRightAxis axis<br /> // LeftRightSign +Acc = Left Turn<br /><br /> // UpDownAxis axis<br /> // UpDownSign +Acc = Up <br /><br /> //<br /> // reinitialize globals to prepare for new averages<br /> //<br /> ForwardAcceleration = 0.0;<br /> LeftRightAcceleration = 0.0;<br /> UpDownAcceleration = 0.0;<br /><br /> //<br /> // Shift all the previous readings down in the array &amp; Start summing the existing valid entries for the average calculation<br /> //<br /> for ( int i = accelDataPoints -1; i &gt; 0 ; i--){<br /> ForwardAccelerationArray[i] = ForwardAccelerationArray[i-1];<br /> ForwardAcceleration = ForwardAcceleration + ForwardAccelerationArray[i]; <br /><br /> LeftRightAccelerationArray[i] = LeftRightAccelerationArray[i-1]; <br /> LeftRightAcceleration = LeftRightAcceleration + LeftRightAccelerationArray[i]; <br /><br /> UpDownAccelerationArray[i] = UpDownAccelerationArray[i-1];<br /> UpDownAcceleration = UpDownAcceleration + UpDownAccelerationArray[i]; <br /><br /> }<br /><br /> //<br /> // get the raw Accelerometer current values off the I2C device<br /> // <br /> getAccelerometer(Acc);<br /><br /> //<br /> // Alias and Assign the current reading to the correct mappings in the normilized array [0]<br /> //<br /> ForwardAccelerationArray[0] = float (ForwardSign * Acc[ForwardAxis]);<br /> LeftRightAccelerationArray[0] = float (LeftRightSign * Acc[LeftRightAxis]); <br /> UpDownAccelerationArray[0] = float (UpDownSign * Acc[UpDownAxis]); <br /><br /><br /> //<br /> // add the current acceleration reading to the global value and finish average calculation<br /> //<br /> ForwardAcceleration = (ForwardAcceleration + ForwardAccelerationArray[0]) / accelDataPoints;<br /> LeftRightAcceleration = (LeftRightAcceleration + LeftRightAccelerationArray[0]) / accelDataPoints;<br /> UpDownAcceleration = (UpDownAcceleration + UpDownAccelerationArray[0]) / accelDataPoints;<br /> <br /> if(logMode) {<br /> Serial.print(ForwardAcceleration);<br /> Serial.print(F(","));<br /> Serial.print(LeftRightAcceleration);<br /> Serial.print(F(","));<br /> Serial.print(UpDownAcceleration);<br /> Serial.print(F(","));<br /> }<br /><br />} <br /><br />// ******************************************************************************k<br />//Readsthe X,Y,Z axis values from the accelerometer and sends the values to the <br />// ******************************************************************************k<br />void getAccelerometer(float *Acc) {<br /><br /> // accelerometer values<br /> byte xh = ReadDevRegister(LSM303_ADDRESS_ACC,0x29);<br /> byte xl = ReadDevRegister(LSM303_ADDRESS_ACC,0x28);<br /> byte yh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2B);<br /> byte yl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2A);<br /> byte zh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2D);<br /> byte zl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2C);<br /> <br /> // need to convert the register contents into a righ-justified 16 bit value<br /> Acc[0] = (xh&lt;&lt;8|xl); <br /> Acc[1] = (yh&lt;&lt;8|yl); <br /> Acc[2] = (zh&lt;&lt;8|zl); <br /><br /> //<br /> // Convert raw Accelerometer readings to g's using calibration values <br /> //<br /> if( Acc[0] &gt;= 0) Acc[0] = float( Acc[0] / XAxisPositive1gCalibration) ;<br /> else Acc[0] = float(Acc[0] / XAxisNegative1gCalibration ); <br /><br /> if( Acc[1] &gt;= 0) Acc[1] = float( Acc[1] / YAxisPositive1gCalibration) ;<br /> else Acc[1] = float(Acc[1] / YAxisNegative1gCalibration ); <br /><br /> if( Acc[2] &gt;= 0) Acc[2] = float( Acc[2] / ZAxisPositive1gCalibration) ;<br /> else Acc[2] = float(Acc[2] / ZAxisNegative1gCalibration ); <br /><br />} <br />// ******************************************************************************k<br />//Readsthe X,Y,Z axis values from the accelerometer and sends the values to the <br />// ******************************************************************************k<br />void getRawAccelerometer(float *Acc) {<br /><br /> // accelerometer values<br /> byte xh = ReadDevRegister(LSM303_ADDRESS_ACC,0x29);<br /> byte xl = ReadDevRegister(LSM303_ADDRESS_ACC,0x28);<br /> byte yh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2B);<br /> byte yl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2A);<br /> byte zh = ReadDevRegister(LSM303_ADDRESS_ACC,0x2D);<br /> byte zl = ReadDevRegister(LSM303_ADDRESS_ACC,0x2C);<br /> <br /> // need to convert the register contents into a righ-justified 16 bit value<br /> Acc[0] = (xh&lt;&lt;8|xl); <br /> Acc[1] = (yh&lt;&lt;8|yl); <br /> Acc[2] = (zh&lt;&lt;8|zl); <br /><br />} <br /><br />//******************************************************************<br />//Read &amp; Print Eprom Calibration Values<br />//******************************************************************<br />void readPrintEpromValues(){<br /> //<br /> // Now Read Back all the Values<br /> //<br /> XAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_X_NEG_1G); <br /> YAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Y_NEG_1G); <br /> ZAxisNegative1gCalibration= eepromReadInt(EPROM_ACC_Z_NEG_1G); <br /> XAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_X_POS_1G); <br /> YAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Y_POS_1G); <br /> ZAxisPositive1gCalibration= eepromReadInt(EPROM_ACC_Z_POS_1G); <br /><br /> <br /> Serial.println("**************** Accelerometer Calibration Values *********************"); <br /> Serial.print(F("EProm X Neg Axis 1g Calibration: "));<br /> Serial.println(XAxisNegative1gCalibration);<br /> Serial.print(F("EProm Y Neg Axis 1g Calibration: "));<br /> Serial.println(YAxisNegative1gCalibration);<br /> Serial.print(F("EProm Z Neg Axis 1g Calibration: "));<br /> Serial.println(ZAxisNegative1gCalibration);<br /> Serial.print(F("EProm X Pos Axis 1g Calibration: "));<br /> Serial.println(XAxisPositive1gCalibration);<br /> Serial.print(F("EProm Y Pos Axis 1g Calibration: "));<br /> Serial.println(YAxisPositive1gCalibration);<br /> Serial.print(F("EProm Z Pos Axis 1g Calibration: "));<br /> Serial.println(ZAxisPositive1gCalibration);<br /> Serial.println("*************************************************************************"); <br />}<br />//*************************************************************************************<br />//<br />//<br />// calibrateAccelerometer()<br />//<br />//<br />//************************************************************************************<br />void calibrateAccelerometer(){<br /><br /> float Acc[3];<br /> float prevAcc[3];<br /> <br /> <br /> int XAxis;<br /> int YAxis;<br /> int ZAxis;<br /><br /> int button;<br /><br /> //<br /> // Min Max accumulation variables<br /> //<br /> int XAxis_min = 0;<br /> int XAxis_max = 0;<br /> int YAxis_min = 0;<br /> int YAxis_max = 0;<br /> int ZAxis_min = 0;<br /> int ZAxis_max = 0; <br /> //<br /> // Initialize Min Max accumulation variables<br /> //<br /> XAxis_min = 0;<br /> XAxis_max = 0;<br /> YAxis_min = 0;<br /> YAxis_max = 0;<br /> ZAxis_min = 0;<br /> ZAxis_max = 0;<br /><br /> /*<br /> * Intialize to Zero<br /> */ <br /> for(int i=0; i&lt;3 ; i++){<br /> Acc[i] = 0;<br /> }<br /> <br /> // Turn off Lights while setting LEDs<br /> setLights(SportLight,LOW);<br /> setLights(TourLight,LOW);<br /><br /> Serial.println(F("********************* Calibration Routine ****************************"));<br /> Serial.println(" o Press Once to see EEprom Calibration &amp; Threshold values "); <br /> Serial.println(" o Press Twice To Exit Calibration Mode "); <br /> Serial.println(" o Press Three Times To Begin Calibration Mode "); <br /> Serial.println(" During Calibration - Press Once to Save Calibration Values &amp; Exit Calibration Routine"); <br /> Serial.println(F("**********************************************************************"));<br /> <br /> /*<br /> * Stay in this loop until button is pressed multple times to indicate save<br /> */ <br /> while(1){ <br /> <br /> //read the selector PUSHBUTTON or Rotary and see if they have it in DIAGMODE<br /> button = readPushButtonSwitch();<br /> switch(button){<br /><br /> case NOCHANGE:<br /> break;<br /><br /> case 1:<br /> //<br /> // Ok they want to print current calibration &amp; threshold values<br /> //<br /> //<br /> Serial.println(F("************************ Thresholds **********************************"));<br /> Serial.print(F("Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> Serial.println(F("**********************************************************************"));<br /> readPrintEpromValues();<br /> Serial.println(" o Press Once to see EEprom Calibration values "); <br /> Serial.println(" o Press Twice To Exit Calibration Mode "); <br /> Serial.println(" o Press Three Times To Begin Calibration Mode "); <br /> Serial.println(" During Calibration - Press Once to Save Calibration Values &amp; Exit Calibration Routine"); <br /><br /> break;<br /><br /><br /><br /> case 2:<br /> //<br /> // Ok they want to Exit <br /> //<br /> <br /> Serial.println(F("********************* Exiting Calibration Routine ****************************"));<br /> Serial.println(F("********************* NO Save Performed ****************************"));<br /> //<br /> // Blink both lights at together (3 times) to indicate exiting calibration<br /> //<br /> blinkBothLights(3,500,1,0,1);<br /> <br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /> break;<br /><br /> case 3:<br /> //<br /> // Ok they want to Calibrate Accelerometer <br /> //<br /> <br /> while(1){<br /> <br /> button = readPushButtonSwitch();<br /> if (button != NOCHANGE) break;<br /> <br /> for(int i=0; i&lt;3 ; i++){<br /> prevAcc[i] = Acc[i];<br /> }<br /> delay(100);<br /> getRawAccelerometer(Acc);<br /><br /> XAxis =Acc[0];<br /> YAxis =Acc[1];<br /> ZAxis =Acc[2];<br /> //<br /> // Check to make sure previous value = current value<br /> // to weed out noise from acc.<br /> //<br /> if(prevAcc[0] == Acc[0]){ <br /> if(Acc[0] &lt; XAxis_min) XAxis_min=XAxis;<br /> if(Acc[0] &gt; XAxis_max) XAxis_max=XAxis;<br /> }<br /><br /> if(prevAcc[1] == Acc[1]){ <br /> if(Acc[1] &lt; YAxis_min) YAxis_min=YAxis;<br /> if(Acc[1] &gt; YAxis_max) YAxis_max=YAxis;<br /> }<br /><br /> if(prevAcc[2] == Acc[2]){ <br /> if(Acc[2] &lt; ZAxis_min) ZAxis_min=ZAxis;<br /> if(Acc[2] &gt; ZAxis_max) ZAxis_max=ZAxis;<br /> }<br /><br />/*<br /> Serial.print(" X ");<br /> Serial.print(XAxis ); <br /><br /> Serial.print(" Y ");<br /> Serial.print(YAxis ); <br /><br /> Serial.print(" Z ");<br /> Serial.println(ZAxis ); <br />*/<br /> Serial.print("MIN X=");<br /> Serial.print(XAxis_min); <br /> Serial.print(" ");<br /> Serial.print("Y=");<br /> Serial.print(YAxis_min); <br /> Serial.print(" ");<br /> Serial.print("Z=");<br /> Serial.print(ZAxis_min); <br /><br /> Serial.print(" MAX X=");<br /> Serial.print(XAxis_max); <br /> Serial.print(" ");<br /> Serial.print("Y=");<br /> Serial.print(YAxis_max); <br /> Serial.print(" ");<br /> Serial.print("Z=");<br /> Serial.println(ZAxis_max); <br /><br /> }<br /><br /> // <br /> // ok their done calibrtaing save values<br /> //<br /> XAxis_min = abs(XAxis_min) ;<br /> eepromWriteInt(EPROM_ACC_X_NEG_1G , XAxis_min);<br /><br /> YAxis_min = abs(YAxis_min) ;<br /> eepromWriteInt(EPROM_ACC_Y_NEG_1G , YAxis_min);<br /><br /> ZAxis_min = abs(ZAxis_min) ;<br /> eepromWriteInt(EPROM_ACC_Z_NEG_1G , ZAxis_min);<br /><br /> eepromWriteInt(EPROM_ACC_X_POS_1G , XAxis_max);<br /> eepromWriteInt(EPROM_ACC_Y_POS_1G , YAxis_max);<br /> eepromWriteInt(EPROM_ACC_Z_POS_1G , ZAxis_max);<br /><br /><br /><br /> //<br /> // Now Read Back &amp; Print all the Values<br /> //<br /> Serial.println(F("************************** Thresholds *****************************"));<br /> Serial.print(F("Medium Threshold: "));<br /> Serial.println( mediumThreshold);<br /> Serial.print(F("Hard Threshold: "));<br /> Serial.println( hardThreshold );<br /> Serial.println(F("**********************************************************************"));<br /><br /> readPrintEpromValues(); <br /><br /> Serial.println(F("New Threshold Values have been saved - exiting calibration routine"));<br /><br /> //<br /> // Blink both lights at together (8 times) to indicate saving calibrations &amp; exiting.<br /> //<br /> blinkBothLights(8,500,1,0,1);<br /><br /> // Restore the previous state of the controller<br /> //<br /> myController.setMode(previousMode);<br /> return;<br /> break;<br /> <br /><br /> }<br /> }<br />}<br /><br /></pre>