Tuesday, March 8, 2016

Modification of SIK Experiment #14 - Shift Register IC

To get more confident with modifying Arduino code, I chose to rework the Shift Register IC Experiment #14 that came with the Sparkfun Inventor's Kit. I omitted the binary counting function, and then altered the delay timing and blink patterns of the other functions (oneAfterAnother, pingPong, randomLED, and marquee). I also created an additional random function called randomLED1.

RandomLED1 function had a delay time of 40 milliseconds and three different random indexes. In contrast, the RandomLED function had a delay time of 100 milliseconds and one random index of  3.

SIK Experiment #14 Mod

Here's is my code modification:

int datapin = 2; 
int clockpin = 3;
int latchpin = 4;

byte data = 0;

void setup()
{
  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT);  
  pinMode(latchpin, OUTPUT);
}

void loop()
{

  oneAfterAnother();      // All on, all off with altered sequence of HIGH LOW
  
  randomLED1();          // Blink random LEDs with delay of 40 milliseconds 

  oneOnAtATime();       // Scroll down the line

  pingPong();           // Like above, but back and forth with altered sequence of HIGH LOW

  randomLED();          // Blink random LEDs with delay of 100 milliseconds 

  marquee();

}

void shiftWrite(int desiredPin, boolean desiredState)

{

  bitWrite(data,desiredPin,desiredState);
  shiftOut(datapin, clockpin, MSBFIRST, data);
  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin, LOW);
}


void randomLED1()
{
  int index;
  int delayTime = 40; // time (milliseconds) to pause between LEDs
                      
  index = random(1);    // pick a random number between 0 and 7
  index = random(2);
  index = random(3);
  shiftWrite(index, HIGH);  // turn LED on
  delay(delayTime);     // pause to slow down the sequence
  shiftWrite(index, LOW);   // turn LED off
}


void oneAfterAnother()
{
  int index;
  int delayTime = 100; // Time (milliseconds) to pause between LEDs

  for(index = 0; index <= 7; index++)
  {
    shiftWrite(index, HIGH);
    delay(delayTime);                
  }

  for(index = 7; index >= 0; index--)
  {
    shiftWrite(index, LOW);
    delay(delayTime);
  }
}


void oneOnAtATime()
{
  int index;
  int delayTime = 50; // Time (milliseconds) to pause between LEDs

  for(index = 0; index <= 7; index++)
  {
    shiftWrite(index, HIGH);    // turn LED on
    delay(delayTime);       // pause to slow down the sequence
    shiftWrite(index, LOW); // turn LED off
  }
}


void pingPong()
{
  int index;
  int delayTime = 70; // time (milliseconds) to pause between LEDs

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)
  {
    shiftWrite(index, HIGH);    // turn LED on
    delay(delayTime);       // pause to slow down the sequence
    shiftWrite(index, LOW); // turn LED off
  }

  // step through the LEDs, from 7 to 0

  for(index = 7; index >= 0; index--)
  {
    shiftWrite(index, HIGH);    // turn LED on
    delay(delayTime);       // pause to slow down the sequence
    shiftWrite(index, LOW); // turn LED off
  }
}



void randomLED()
{
  int index;
  int delayTime = 100; // time (milliseconds) to pause between LEDs

  index = random(3);    // pick a random number between 0 and 7

  shiftWrite(index, HIGH);  // turn LED on
  delay(delayTime);     // pause to slow down the sequence
  shiftWrite(index, LOW);   // turn LED off
}


void marquee()
{
  int index;
  int delayTime = 100; // Time (milliseconds) to pause between LEDs

  // Step through the first four LEDs
  // (We'll light up one in the lower 4 and one in the upper 4)

  for(index = 0; index <= 4; index++)
  {
    shiftWrite(index, HIGH);    // Turn a LED on
    shiftWrite(index+5, HIGH);  // Skip four, and turn that LED on
    delay(delayTime);       // Pause to slow down the sequence
    shiftWrite(index, LOW); // Turn both LEDs off
    shiftWrite(index+1, LOW);
  }

                
  shiftOut(datapin, clockpin, MSBFIRST, data);

  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin, LOW);

  data++;

  delay(delayTime);
}

No comments:

Post a Comment