Thanks to the good folks on this board I now have 3 different LED light "patterns" that can be accessed or switched to by using a rotary switch on the digital inputs. All is working well. It's for a model and the next stage is to have a few more LED's flashing in a fairly simple HIGH/LOW at the same time one of the main patterns is selected, i.e Position 3 on the switch. The problem I'm seeing is the delay in the LED blink is causing pattern 3 to pause, start, pause, start.
I think I need to run the extra code concurrently which I think means taking it out of the Void Loop() part of the code ? I'm thinking the article on here - few posts down about "blinking in different configurations "e is the way to go but I need to reread it as didn't understand it first couple of times around. Can someone confirm that is indeed what I need to be focussing or are there other ways to do this ?
My code is attached below. It basically sets up some variables, three byte arrays and then determines what position the rotary switch is in before running the code. Its outputted to two shift registers and onto 16 LED's. The last part of the code - Position 3 is the one causing me grief.
Any advice appreciated as always.
Ged
const int firstRotaryPin = 8; // Defines first rotary pin position 1
const int lastRotaryPin = 10; // Defines last rotary pin position 3
int dataPin = 2; //Define which pins will be used for the Shift Register control
int latchPin = 3;
int clockPin = 4;
int ledPin = 13;
byte seq1[8] = {1,2,4,8,16,32,64,128};// The array for storing the seq#1 values
byte seq2[8] = {128,64,32,16,8,4,2,1}; // The array for storing the seq#2 values
byte seq3[8] = {128,1,64,2,32,4,16,8}; // The array for storing the seq#3 values
void setup() {
for( int i=firstRotaryPin; i<= lastRotaryPin; i++)
{pinMode( i, INPUT_PULLUP); } // Sets internal pull ups for digital inputs
pinMode(dataPin, OUTPUT); // Configure Digital Pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(ledPin, OUTPUT); } // Sets pin for EXTRA LED when switch is in position 3
//Find the position of the rotary switch, 1-3
int getRotaryValue() {
for( int i=firstRotaryPin; i<= lastRotaryPin; i++) {
int val = digitalRead( i ); // look at a rotary switch input
if( val == LOW ) { // it's being pulled to ground and is selected
return (i - firstRotaryPin + 1); // return a value that ranges 1 - 3
}}}
void loop() {
int rotaryPos = getRotaryValue();
if( rotaryPos == 1 )
{
for (int x = 0; x< 8; x++) //Array count cycle
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, seq1[x]); //Send the data byte 1
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay (50);
}}
else if ( rotaryPos ==2 )
{ for (int x = 0; x< 8; x++) //Array count cycle
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, seq2[x]); //Send the data byte 1
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay (50);
}}
else if ( rotaryPos ==3 )
{ for (int x = 0; x< 8; x++) //Array count cycle
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, seq3[x]); //Send the data byte 1
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay (50);}
blinkLED();
}}
void blinkLED(){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(500); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(500);
}