Rotary switch to choose loop

Im hoping for some help here. Im trying to learn but I haven't written code since I learned Basic in 1992.

I an creating a simple lighting display for a regional Burning Man Event. I have an Arduino Uno R3 and an 8 channel relay board. I have written a simple program to flash led lights connected to each of the 8 relay channels, that seemed simple.

What I'm struggling with is integrating a 4 position rotary switch to select from 4 different flashing programs. In basic it would have been 4 loops and if then statements.

Switch select 1 (Relays always closed, lights on) Switch Select 2 (Slow Flashing) Switch Select 3 (Fast Flashing) Switch Select 4 (Obnoxious strobe)

I can write the patterns to control the relays, I'm struggling with the code to select the loop or program segment.

Any help?

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components. Posting images:

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Connect that switch to 4 inputs (or 3 where all off is the fourth position). And just use a simple if, else if in the loop to select the right pattern.

But do note! If you are designing the pattern with delay() the response to the switch will NEVER be lag-free. If you want direct response you have to use a state machine with millis() for timing. That way the loop() can run thousands of times per second and thus respond to the switch immediately.

State machine.

In your loop() function read the switch, set the state based on it, and run that specific code. Keep in mind that you may have to set some relays once upon state change to have a known starting position.

Im hoping for some help here. Im trying to learn but I haven't written code since I learned Basic in 1992.

I an creating a simple lighting display for a regional Burning Man Event. I have an Arduino Uno R3 and an 8 channel relay board. I have written a simple program to flash led lights connected to each of the 8 relay channels, that seemed simple.

What I'm struggling with is integrating a 4 position rotary switch to select from 4 different flashing programs. In basic it would have been 4 loops and if then statements.

Switch select 1 (Relays always closed, lights on) Switch Select 2 (Slow Flashing) Switch Select 3 (Fast Flashing) Switch Select 4 (Obnoxious strobe)

I can write the patterns to control the relays, I'm struggling with the code to select the loop or program segment.

Any help?

For speedy results it is best to paste up your existing code. Modifying and correcting existing code to fix your problem is faster than creating something from scratch.

Are you using four separate inputs to detect the switch position or a single Analog input.

void setup(){}
void loop(){
  static int LASTSW;
  int SWREAD = whatSW();

  if (SWREAD != LASTSW){
    // DOSOMETHING
    if (SWREAD == 1){}
    if (SWREAD == 2){}
    if (SWREAD == 3){}
    if (SWREAD == 4){}
    LASTSW =  SWREAD;    
  }
}
int whatSW(){
  if(digitalRead(sw1_Pin)){return 1;}
  if(digitalRead(sw2_Pin)){return 2;}
  if(digitalRead(sw3_Pin)){return 3;}
  if(digitalRead(sw4_Pin)){return 4;}
  return 0;
}

Not all rotary switches are created equal but in general there should be one common an four contacts so it should be pretty straightfirward,
as shown by previous post.

In fact, it would be just as easy with four pushbuttons.

There is a sort of inherent logic with most rotary switches, they only give one selection at a time, whereas using separate pushbuttons, you could push one or more. at the same time. But equally, you may not want to pass through positions two and three, to get from one to four using a rotary switch. The coding may need to be different, and also current limiting resistors.

Even something relatively simple, can need quite a lengthy specification, to avoid ambiguity, and so on.

Try a switch case statement, which will also prioritize the switches for you. Example from another section: https://www.arduino.cc/en/Reference.SwitchCase Example: switch (var) { case 1: //do something when var equals 1 break; case 2: //do something when var equals 2 break; default: // if nothing else matches, do the default // default is optional break; } Good Luck and Have Fun! Gil