HI. Complete newbie here - about 7 days playing with the kit. I'm needing some help scripting an IF statement to allow me to use a rotary switch to change between sections of code. I've just figured out how shift registers work so now have a couple of rows of LEDS's I can light up and off in different patterns etc. All good so far I think.
I'm keen to create very specific lighting sequences/timing in the code and have invested in a rotary switch to change between each sequence. That's where I'm struggling to write the code as nearly all the google resources I can find related to rotary encoders. This is definitely an analog rotary switch - basically 1 pole and 3 position.
I've created a resistor ladder around the pins using 2 x 1K resistors, have 5v going in on position 1 and ground out on position 3 and the ' analog' sensor input lead on the common. I get 0 resistance at position 1, 998 at position 2 and 1.998 at position 3 so the switch and resistors appear to be working.
From googling I believe i can use the resistance readings and an Analog input pin to tell arduino which piece of the script to run. I have attached my script for running the 2 shift registers as well as the additional ( what I think I need - Second and Third swithc positions ) script for the extra LED patterns. It's how to tie these and the switch together that's causing me pain. Thanks for any advice or indeed free bit of script to 'borrow..
Ged
int dataPin = 2; //Define which pins will be used for the Shift Register control
int latchPin = 3;
int clockPin = 4;
int seq[16] = {128,64,32,16,8,4,2,1,128,64,32,16,8,4,2,1};
int seq2[16]= {128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1};
void setup()
{
pinMode(dataPin, OUTPUT); //Configure each IO Pin
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
for (int n = 0; n <16; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, seq[n]); //Send the data
shiftOut(dataPin, clockPin, LSBFIRST, seq2[n]);
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(200);
}
}
//Void Second Switch Position
// int seq3[8]={1,2,4,8,1,2,4,8}
// int seq4[8]=[2,8,2,8,2,8,2,8}
for (int n = 0; n <8; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST, seq[n]); //Send the data
shiftOut(dataPin, clockPin, LSBFIRST, seq2[n]);
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(200);
// Void Third Switch Position
// Void Fourth Switch Position etc etc