Go Down

Topic: OP1 Flash once, OP2 Flash twice etc... etc ... (Read 668 times) previous topic - next topic

The-Owl

May 29, 2012, 10:17 pm Last Edit: May 29, 2012, 10:57 pm by The Owl Reason: 1
Hi,

For my first post, here is a piece of code I wrote to flash an LED on IO-0 once, one on IO-1 Twice etc... etc... up to IO-13 that will flash 14 times. This will be very handy for anyone that has had to regularly identify which core is which on a long multicore cable.

I know this is very heavilly commented, but the old adage of " it was hard for me to write, so it must be hard for someone else to Understand " really isn't in the Arduino spirit of things.

Code as copy & Pasted from arduino Software.

All the Best,

The Owl.

Code: [Select]


/*

Copyright (C) 2012 The Owl.

Program to flash LED's so that Pin 0 will flash once, Pin 1 will flash twice, etc ...

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

An example use for this could be as a test instrument to help you identify core on a multi-core cable. ( A device that could
have saved me a lot of work back when I was working in Heavy Industry in an Electrical Engineer Role. )

Created 28th May 2012 : Original Code produced.
Modified 29th May 2012 : Code cleaned up, comments added, plus Integers & Array renamed from x,y & z to easier to understand names.

Please be aware this is one of my first attempts at programming in C ( having only recently got an Arduino to play with ),
so no comments about how dirty this code is! ( Although I have previously used Basic < BBC Electron >, Pascal < Turbo Pascal >,
Javascript and Visual Basic < Internet Explorer HTML, MS Excell embedded code in spreadsheets > ).

This code is free to use or distrubute for any non-commercial purposes, provided this is supplied " As Is " complete with these
original comments. If anyone wishes to add a translation to these comments, please add a short note to this header to give yourself
credit, but please also remember to leave my original comments and this header.

For Ryan...

*/

int counterArray[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };   /*   Set an array to store what stage in the sequence we are in for each of the 14 outputs.
                                                                            ( Note: for ease of coding I have not used counterArray[0] )                            */
int count = 0;  int opflag = 0;

void setup()
{   for ( count=0; count<=13; count++ )  { pinMode(count, OUTPUT); }  }  /*  Configure Digital I.O. 0 to 13 to all be Outputs  */

void loop()  /*  repeat continuously . . .  */

{
   for ( count = 1; count <= 14; count++ )                     /*  For each of the 14 outputs ...  */
     {
      opflag = 1;                                              /*  Set a flag ( used to hold the required state for the output ) to "on"         */
     
      if ((counterArray[count]%2)!=1) { opflag = 0; };         /*  If the stage number is an even number, then set the flag to "Off", so output goes
                                                                   on-off-on-off etc... as the stage number steps through 1-2-3-4 etc...               */
                                                                   
      if (counterArray[count] >= (count*2)) { opflag = 0; } ;  /*  If output has already flashed the required number of times then set flag to "Off".  */
     
      digitalWrite((count-1), opflag);                         /*  Write to the Output! Notice how I am writing Output 1 to 14 to IO Pins 0 to 13. This was done to
                                                                   avoid using counterArray[0] ( that would have made this code much more difficult ro read. )        */
                                                                   
      counterArray[count]++;                                   /*  Set the stage counter to the next stage ( ready for next time round )  */
     
      if ( counterArray[count] > (( count * 2 ) + 6 ) ) { counterArray[count] = 1; };  /*  When we have flashed enough times, and had enough of a delay
                                                                                           ( 6 stage delay ), jump back to stage 1.                         */  
     }  
   delay(200);    /*  Now that each of the outputs has been set, wait 200ms before repeating the above with the next step for each output  */                    
}


Go Up