I am trying shift registers for the first time, and would like to be able to make patterns for how the leds would be turned on and off.
Right now I got this test code running, which is just a boring sweep...
int dataPin = 2; //Define which pins will be used for the Shift Register control
int latchPin = 3;
int clockPin = 4;
int seq[14] = {1,2,4,8,16,32,64,128,64,32,16,8,4,2}; //The byte sequence
void setup()
{
pinMode(dataPin, OUTPUT); //Configure each IO Pin
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
for (int n = 0; n < 14; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, MSBFIRST, seq[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(75);
}
}
But how would I do it if I would have multiple pins enabled? like 00011000 then 00100100, followed by 01000010...
Here's code I wrote a long time ago to make patterns. See if you can figure out how to make your from this. FYI my LEDs were in a circle.
// 595 tester
//Pin Definitions
//The 74HC595 using a protocol called SPI (for more details http://www.arduino.cc/en/Tutorial/ShiftOut)
//Which has three pins
#define DATA_PIN 10
#define CLOCK_PIN 9
#define LATCH_PIN 8
#define MAX_LED 8 // the number of LEDs we want to flash, should be a number >= 2
#define MAX_REPEAT 2 // the maximum number of times each sequence will repeat
#define PAUSE_DELAY 100 // delay between each LED animation on/off
#define DEBUG 1 // Enable Debugging code if required
//Used for single LED manipulation
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
void setup()
{
// We set the three control pins to outputs
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
#if DEBUG
Serial.begin(57600);
Serial.println("[LED Sequence Flasher]");
#endif
}
void updateLEDs(byte value, boolean bReverse, int nPause)
// Handle the interface to the 74HC595 to write the 8 bits of the byte
// Optionally reversethe bit string and then delay at the end
{
if (bReverse) value = ~value;
digitalWrite(LATCH_PIN, LOW); //Pulls the chips latch low
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value); //Shifts out the 8 bits to the shift register
digitalWrite(LATCH_PIN, HIGH); //Pulls the latch high displaying the data
if (nPause != 0) delay(nPause);
}
void Sequence01(boolean bReverse)
// Flash each LED in sequence
{
#if DEBUG
Serial.println("Seq 01");
#endif
for (byte i=0; i<MAX_LED; i++)
{
updateLEDs((1 << i), bReverse, PAUSE_DELAY);
}
}
void Sequence02(boolean bReverse)
// Scan back and forth
{
#if DEBUG
Serial.println("Seq 02");
#endif
for (byte i=0; i<MAX_LED; i++)
{
updateLEDs((1 << i), bReverse, PAUSE_DELAY);
}
for (byte i=MAX_LED-1; i>0; i--)
{
updateLEDs((1 << i), bReverse, PAUSE_DELAY);
}
}
void Sequence03(boolean bReverse)
// Start at each end towards the middle
{
#if DEBUG
Serial.println("Seq 03");
#endif
for (byte i=0; i<MAX_LED/2; i++)
{
updateLEDs((1 << i) | (1 << MAX_LED-i-1), bReverse, PAUSE_DELAY);
}
}
void Sequence04(boolean bReverse)
// Start in the middle towards each end
{
#if DEBUG
Serial.println("Seq 04 ");
#endif
for (int i=MAX_LED/2-1; i>=0; i--)
{
updateLEDs((1 << i) | (1 << MAX_LED-i-1), bReverse, PAUSE_DELAY);
}
}
void Sequence05(boolean bReverse)
// Start at each end towards the middle, cross over and keep going
// Looks like they bounce off each other!
{
#if DEBUG
Serial.println("Seq 05");
#endif
for (byte i=0; i<MAX_LED; i++)
{
updateLEDs((1 << i) | (1 << MAX_LED-i-1), bReverse, ((i==MAX_LED/2) ? PAUSE_DELAY/2 : PAUSE_DELAY));
}
}
void Sequence06(boolean bReverse)
// Bouncing ball, each bounce is one less LED
{
byte j;
#if DEBUG
Serial.println("Seq 06");
#endif
for (byte i=0; i<MAX_LED; i++)
{
for (j=0; j<MAX_LED-i-1; j++)
{
updateLEDs((1 << j), bReverse, PAUSE_DELAY);
}
for ( /* wherever j is from before */; j>0; --j)
{
updateLEDs((1 << j), bReverse, PAUSE_DELAY);
}
}
}
void Sequence07(boolean bReverse)
// Binary counter
{
int maxCount = (1<<MAX_LED)-1; // use up all the bits in the number
#if DEBUG
Serial.println("Seq 07");
#endif
for (int i=0; i<maxCount; i++)
{
updateLEDs(i, bReverse, PAUSE_DELAY/4);
}
}
void loop()
{
static int n = 0;
static boolean bReverse = false;
switch (n/MAX_REPEAT) // repeat each sequence MAX_REPEAT times
{
case 0: Sequence01(bReverse); break;
case 1: Sequence02(bReverse); break;
case 2: Sequence03(bReverse); break;
case 3: Sequence04(bReverse); break;
case 4: Sequence05(bReverse); break;
case 5: Sequence06(bReverse); break;
case 6: Sequence07(bReverse); break;
default:
n=-1; // reset the loop counter - it gets incremeted again at the end of loop()
bReverse = ~bReverse;
}
n++; // increment the loop counter for next time
}