programming help

found this code on the web…loaded the program and it works and then goes off.

i want it to repeat
continuously.

just a knightrider effect and all off then all on…

thanks…

#include <Shifter.h>//header program

// THESE VARIABLES CHANGE.
// Set the number of leds your using and the number of shift registers used.
// The Shifter library assumes one register = 8 bits.  With this in mind,  
// NUM_LED can never be greater that NUM_REG x 8 bits.
const int NUM_LED = 32;      // number of leds
const int NUM_REG = 4;       // number of shift registers


// Set variables - Global variables, except for the Arduino
// pins, are CAPITALIZED.  This makes identification of local and 
// global variables easy to understand.  The compilier is case sensitive
// so it understands capitalized and lower case letters.

// These variables are too make things a little easier for humans to read.
// The "const" sets the variable so it cannot be changed
// later in the program (ie. make them constants).  The standard
// constants "true" and "false" function just as well as these.
const bool OFF = 0;
const bool ON = 1;


const int latchPin = 11;
                                   
const int clockPin = 12;

const int dataPin = 8;


Shifter shifter(dataPin, latchPin, clockPin, NUM_REG); 


// Set up the array of leds that is used throughout the program
bool LED[NUM_LED];        


/**************************************************************
Function: setup
Purpose:  set up Arduino
Args:     none
Returns:  nothing
Notes:    This function is required by the Arduino
***************************************************************/
void setup() {

  // Set up the output pins.  
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  
  // Set up the shifter output
  shifter.clear();
  shifter.write();
  
  // turn off all the LEDS, just in case
  allOff();
  writeBytes();
 
  randomSeed(analogRead(0));
  

}  // End of Arduino Setup 




void loop() {
  

  int loopDelay = 2000;  
  
  // Flash all leds, to let us know its on
  
  flashAll(100, 4);//effect one
  
  delay(loopDelay);
  
  knightRider(50, 5, 3);//effect two
  
  
  
 
}  // End of Arduino Loop



/**************************************************************
Function: flashAll
Purpose:  Flashes all leds
Args:     int duration - how long an led is on 
          int count - how many times to flash
Returns:  nothing
Notes:    
***************************************************************/
void flashAll(int duration, int count)
{
  for(int i = 0; i < count; i++)
  {
    allOn();  
    writeBytes();  
    delay(duration);
    allOff();
    writeBytes();  
    delay(duration);
  }  
 
}  




/**************************************************************
Function: knightRider
Purpose:  "Knight Rider" effect from the TV show
Args:     int duration - how long an led is on 
          int count - how many times to cycle through effect
          int numleds - how many leds are on at a time
Returns:  nothing
Notes:    This is not a perfect implementaion of the effect.
          A perfect effect would include a dimming of the 
          trainling leds (using PWM). Its still cool! 
**************************************************************/
void knightRider(int duration, int count, int numleds)
{
  // Clear the array and turns off any leds still on 
  allOff();
  writeBytes();
  
  duration = duration / numleds;
  int trailingleds = numleds - 1;
  int ledcounter = NUM_LED + trailingleds;
  
  for(int i = 0; i < count; i++)
  {
    // cycle through one direction
    for(int j = 0; j < ledcounter -1 ; j++)
    {
      LED[j] = ON;
      writeBytes();
      delay(duration);
      LED[j + 1] = ON;
      LED[j - trailingleds] = OFF;
      writeBytes();
      // Skip the extra delay() commands as the 
      // trailing leds catch up.
      if(j < NUM_LED)
        delay(duration);    
    }
    // cycle back in the oppisite direction
    for(int j = ledcounter; j > -trailingleds; j--)
    {
      LED[j] = ON;
      writeBytes();
      // Skip the initial delay() commands as the 
      // trailing leds start back.
      if(j < NUM_LED -1)
        delay(duration);
      LED[j - 1] = ON;
      LED[j + trailingleds] = OFF;
      writeBytes();
      delay(duration);    
    }
  }
  
 // Turn the last led off 
 allOff(); 
 writeBytes();
}
/**************************************************************
Function: allOn
Purpose:  Turns ON all leds
Args:     none
Returns:  none
Notes:    
**************************************************************/  
void allOn()
{
   for (int i = 0; i < NUM_LED; i++)
   {
     LED[i] = ON;
   }
}


void allOff()
{
   for (int i = 0; i < NUM_LED; i++)
   {
     LED[i] = OFF;
   }
}



/**************************************************************
Function: writeBits
Purpose:  Write the array with Most Significant Bit first (MSFBFIRST)
Args:     none
Returns:  nothing
Notes:    This function is a wrapper function for outBytes()
***************************************************************/
void writeBytes()
{
  outBytes(MSBFIRST);
}


/**************************************************************
Function: writeBytesRev
Purpose:  Write the array with Least Significant Bit first (MSFBFIRST)
Args:     none
Returns:  nothing
Notes:    This function is a wrapper function for outBytes()
***************************************************************/
void writeBytesRev()
{
  outBytes(LSBFIRST);
}

/**************************************************************
Function: outBytes
Purpose:  Send array data to shift register
Args:     int dir - First or Least significant bit first
Returns:  nothing
Notes:    This is where data is actually sent out, but is not 
          intended to be used directly.  writeBytes() and
          writeBytesRev() should be used instead.
***************************************************************/
void outBytes(int dir)
{
   // set latch pin low so data can be sent 
   digitalWrite(latchPin, LOW);
   // Clears previous data
   shifter.clear();
   
   // Set each pin according to what LED[] currently has.
   
   // most significant bit first
   if(dir == MSBFIRST)
   {
     for(int i = 0; i < sizeof(LED);i++)
     {
        if(LED[i] == ON)
         shifter.setPin(i, HIGH);
       else
         shifter.setPin(i, LOW);
     } 
   }
   // least significant bit first - reverses what is in LED[]
   else if(dir == LSBFIRST)
   {
     int j = 0;
     for(int i = sizeof(LED) - 1; i > -1;i--)
     {
       if(LED[i] == ON){
         shifter.setPin(j, HIGH);
       }  
       else{
         shifter.setPin(j, LOW);
       }        
       j++;  
     } 
   }   
   
   // Write the data to the registers
   shifter.write();
   // set the latch high to turn on the output
   digitalWrite(latchPin, HIGH);

}

//  End of program

in the section labelled "void loop()" delete all but the knightrider line and the closing tags.

void loop() {
  
  knightRider(50, 5, 3);//effect two
  

}  // End of Arduino Loop

This should loop the knight rider effect continuously.

can you be more specific..i want both the effects to run one after another..

the program you posted initially should repeat continuously.

Strange that it only happens once. You might need to add some debug print statements, or more fully describe what the leds actually do right now.

it does light all leds on den all leds of den the knight rider effect starts and after that it just goes off, it does not repeat the process again..

can someone please help..

I cannot see anything wrong with the program, and it should repeat to my eye.

The only thing I can think of is near the end of the first time through the loop, it is crashing for some reason.

You will have to use serial prints to debug this.

See how to do this here:

https://www.arduino.cc/en/Serial/Print

i just tried to load one effect at a time and realized that two effect seems to be crashing…

the knight rider effect and the chase effect…

You can’t do this:

bool LED[NUM_LED];     
...
  int ledcounter = NUM_LED + trailingleds;
...

and then do this:

    for(int j = 0; j < ledcounter -1 ; j++)
    {
...
      LED[j + 1] = ON;
...

I suggest that you check very carefully that you are not writing outside the bounds of the LED array.

This effect runs from left to right but then it stops.Can some one please help me as to what could be the problem.i tried but cant understand.

this effect lights 3 leds from left to right and then right to left. and then goes back to effect one.

void chase(int duration, int count, int numleds, bool reverse)
{
  // Clear the array and turns off any leds still on 
  allOff();
  writeBytes();
  
  duration = duration / numleds;
  int trailingleds = numleds - 1;
  int ledcounter = NUM_LED + trailingleds;
  
  for(int i = 0; i < count; i++)
  {
    for(int j = 0; j < NUM_LED; j++)
    {
      LED[j] = ON;
      if (reverse == false)
        writeBytes();
      else
        writeBytesRev();
      delay(duration);
      LED[j + 1] = ON;
      LED[j - trailingleds] = OFF;
    }    
  } 
  
  // turn off the last led
  delay(duration);
  allOff(); 
  writeBytes();
  
}

see post #8. You are outside your array bounds.

im trying my best to modify the code but cant really get it working..

domdomgin:
im trying my best to modify the code but cant really get it working…

Here’s a hint: you cannot do this

int LEDS[10];

LEDS[15] = 1;

because the array index is out of bounds (i.e. 15 > 9).

You can only use array indexes from 0 to the size of the array - 1.

see:
https://www.arduino.cc/en/Reference/Array

can you please please modify the code so i can learn from it as im really new to programming..thanks..

domdomgin: can you please please modify the code so i can learn from it as im really new to programming..thanks..

this is pretty basic stuff so it's an opportunity for you to learn and understand it. Read the link I posted.

can someone please tell which programming all this comes under.i really am interested in learning.thanks,,

domdomgin: can someone please tell which programming all this comes under.i really am interested in learning.thanks,,

It is common to all programming. Did you read the link? Maybe this program is too complicated for you at the moment. Start with smaller programs and build up your knowledge. Arrays and indexes out-of-bounds is pretty basic stuff, and common to all languages.

i finally found out the problem in the code with trial and error..thank you anyways..

This site should be more explanatory and more helpful rather than a DIY method...