I can't make a "while" loop working

I am new to C programming and asking for help with this little project I have using 5 LEDs, 5 relays, a 5 position rotary switch and an Arduino UNO board.

I want the LEDs to randomly blink for 3 seconds, then pause for x number of seconds depending on the rotary switch position and then repeat the process. Here are the seconds it should pause when the switch is in the following positions:

1 = 3 seconds pause and turn LED on
2 = 6 seconds pause and turn LED on
3 = 10 seconds pause and turn LED on
4 = step through all LEDs, one at a time, turn LED on and wait for 2 second, turn it off and go on to the next LED and repeat the process for all 5 LEDs, then do this all over and never step until the switch position is changed
5 = Pause indefinitely and turn LED on

The relays are connected to pins 8, 9, 10, 11 and 12 and one LED is connected to each relay. The positions on the rotary switch are connected to pins 2, 3, 4, 5 and 6. 3.5v are supplied to the common terminal of the rotary switch. So, I am checking for “HIGH” and “LOW” on a pin to determine how long the software should pause/delay execution.

When I run the software everything seems to work OK, but when pin 4 is “HIGH” it seems to get ignored. It does pick up the “pauseTime = 2000;” and go into blink/pause loop with a 2 seconds delay. It should step through all 5 LEDs, 2seocnds on and 2 seconds off and do that forever, but instead it goes into a random LED selection mode. I hope my description makes sense.

As a side note, there are other components connected to the relays, but I wanted to keep the description simple.

The code is listed below. I would appreciate it very much if someone could look at it and help me out.

// Multi targets V2 4.17.2015
// 5 targets
// random selection energizes LEDs & sensors
// 5 options:
// 1. switch on 1 = 3 secs pause
// 2. switch on 2 = 6 seconds pause
// 3. switch on 3 = 10 seconds pause
// 4. switch on 4 = step through all sensors & LEDs 2 secs on, 2 secs off
// 5. switch on 5 = random LED/sensor always on

int ranNum = 3; // default is LED 3
int blinkTime = 40; // default
int pauseTime = 3000; // default

int ledPin1 = 8; // LED connected to digital pin 8
int ledPin2 = 9; // LED connected to digital pin 9
int ledPin3 = 10; // LED connected to digital pin 10
int ledPin4 = 11; // LED connected to digital pin 11
int ledPin5 = 12; // LED connected to digital pin 12

int switchPin1 = 2; // 3 seconds delay
int switchPin2 = 3; // 6 seconds delay
int switchPin3 = 4; // 10 seconds delay
int switchPin4 = 5; // step-by-step LEDs 1 through 5
int switchPin5 = 6; // endless loop delay

// array to define 5 output ports
#define PIN_COUNT 5
int ledPin[ PIN_COUNT ] = {
8, 10, 12, 9, 11
};
int i;

void setup () {
// Check which pin is set
if (digitalRead(2) == HIGH)
pauseTime = 3000; // 3 secs
if (digitalRead(3) == HIGH)
pauseTime = 6000; // 6 secs
if (digitalRead(4) == HIGH)
pauseTime = 10000; // 10 secs
if (digitalRead(6) == HIGH)
pauseTime = 999999999; // stay on as long as possible
// don't worry about remaining code

// ********** this is where I have problems with the code
// Do this as long as switchPin4 is HIGH
if (digitalRead(switchPin4) == HIGH){
pauseTime = 2000; // ~2 secs
while(switchPin4 == HIGH )
{
delay(pauseTime);
digitalWrite(ledPin[2], HIGH);
delay(pauseTime);
digitalWrite(ledPin[2], LOW);
delay(pauseTime);
digitalWrite(ledPin[3], HIGH);
delay(pauseTime);
digitalWrite(ledPin[3], LOW);
delay(pauseTime);
digitalWrite(ledPin[4], HIGH);
delay(pauseTime);
digitalWrite(ledPin[4], LOW);
delay(pauseTime);
digitalWrite(ledPin[5], HIGH);
delay(pauseTime);
digitalWrite(ledPin[5], LOW);

}
}

// looks like switchPin4 went to LOW if it comes to this point

// create random output pin
randomSeed( analogRead( 0 ) );

for ( int i=0 ; i < PIN_COUNT ; i++ ) {
pinMode( ledPin[ i ], OUTPUT );
}
}
void loop () {

for ( int i=75 ; i ; i-- ) { // 75 = ~3 secs
ranNum = random( PIN_COUNT );

for ( int pin=0 ; pin < PIN_COUNT ; pin++ ) {
if ( pin == ranNum ) {
digitalWrite( ledPin[ pin ], LOW );
} else {
digitalWrite( ledPin[ pin ], HIGH );
}
}
delay( 20 + random( blinkTime ) );
}
delay( pauseTime );
}

First question:

if (digitalRead(2) == HIGH)
if (digitalRead(switchPin4) == HIGH){

Why the mix of pin numbers and names?

Second:
Just how is the switch wired? If it's a 5 position rotary switch, do you have external resistors? A schematic is going to be required.

oh, that is probably a leftover when I tried different versions of the "if" statement. I should clean that up to make it consistent. From my little understanding it should not make a difference...or does it?

No, I do not have a resistor at the switch. Here is a rude schematic....no laughing please, it's drawn for me to understand.

Thank you.

See:
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

int switchPin4 = 5;                // step-by-step LEDs 1 through 5

if (digitalRead(switchPin4) == HIGH)
{
      pauseTime = 2000;                  // ~2 secs
      while(switchPin4 == HIGH )  //<-- change to while(digitalRead(switchPin4) == HIGH)
      {

digitalRead(switchPin4) == HIGH

switchPin4 == 5

First a code issue:

// Do this as long as switchPin4 is HIGH                                         
   if (digitalRead(switchPin4) == HIGH){
        pauseTime = 2000;                  // ~2 secs
      while(switchPin4 == HIGH )
      {
   delay(pauseTime);                             
        digitalWrite(ledPin[2], HIGH);
   delay(pauseTime);

What does "while(switchPin4 == HIGH )" do, do you think? "switchPin4" is a constant (the number of the inputpin) not the current value of the digitalRead of the pin ...

Secondly, you have 8 of these delay calls, each 2 seconds, so NOTHING gets checked for those 16 seconds. That requires a major rewrite to fix, so just be aware of it for now.

That is a very nice schematic! It is not the right way to hook up switches. When the switch is opencircuit the input pin is "floating", there is no defined potential on its input. You do not define your pins as either input or output with the pinMode() call. Might work without, bad practice to not do it. I suggest you use the pinMode(inputpin,INPUT_PULLUP) and then take the common pin to GROUND insetad of 3V. Now any pin that is "floating" is via a resistor tied to the 5V rail, and thus firmly in HIGH. the one that the selector is connected to goes firmly to 0V or LOW. There is a small currentdrain as the 5V goes through the pullup, your switch and to ground, but it is very small.

      while(switchPin4 == HIGH )  //<-- change to while(digitalRead(switchPin4 == HIGH)

The suggested change wouldn't compile.

PaulS:

      while(switchPin4 == HIGH )  //<-- change to while(digitalRead(switchPin4 == HIGH)

The suggested change wouldn't compile.

How about if I didn't omit the close parenthesis?
change to while(digitalRead(switchPin4) == HIGH)

Thanks all for your help. I will get to it right now and make changes as suggested. I was not aware of how the input pins work and the link provided by Larry is of great help.

Yes, I changed some of the pin connections that are not reflected in this older schematic.

If ok, I will post my results later.

How about if I didn't omit the close parenthesis?

Just making sure you were advising putting it in the right place.

MSQUARE: ""...so NOTHING gets checked for those 16 seconds. ..."

Yes, I thought that the pin would remain constant on "HIGH" until it is changed. It works on all other switch setting even when delaying for 6 or 10 seconds. I experimented with various delays.

The only function I can not get to work is when physical pin 5 should be on "HIGH".

I changed the code to define the input pins. Changed to Ground and added a 3K resistor between the ground and the input pins. However, it seems to not wanting to run that part of the code, but rather takes the default values and the value defined under the "if (digitalRead(switchPin4) == HIGH){" statement. It's like it ignores the statements defined under "while and executes the random blinking part of the code. oh before I forget, when first powered up it sits there for ~25 seconds and does nothing..at least nothing that I can see.

What I can not understand is when I added some test code to determine what part of code this thing is executing it did not seem to reach that test code.

I hope it is OK to show my modified code here again, for completeness.

// Multi targets  V2 4.17.2015
// 5 targets 
// random selection energizes LEDs & sensors
// 5 options:
//    1. switch on 1 = 3 secs pause
//    2. switch on 2 = 6 seconds pause
//    3. switch on 3 = 10 seconds pause
//    4. switch on 4 = step through all sensors & LEDs 2 secs on, 2 secs off
//    5. switch on 5 = random LED/sensor always on

int ranNum = 3;                 // default is LED 3
int blinkTime = 40;             // default
int pauseTime = 3000;        // default          

int ledPin1 = 8;                // LED connected to digital pin 8
int ledPin2 = 9;                // LED connected to digital pin 9
int ledPin3 = 10;               // LED connected to digital pin 10
int ledPin4 = 11;               // LED connected to digital pin 11
int ledPin5 = 12;               // LED connected to digital pin 12

int switchPin1 = 2;             // 3 seconds delay
int switchPin2 = 3;             // 6 seconds delay
int switchPin3 = 4;             // 10 seconds delay
int switchPin4 = 5;             // steb-by-step LEDs 1 through 5
int switchPin5 = 6;             // endless loop delay

//  array to define 5 output ports
#define PIN_COUNT   5
int ledPin[ PIN_COUNT ] = {
    8, 10, 12, 9, 11  
};

int i;

void setup () {
 // define input pins 
  pinMode(switchPin1, INPUT);             // pin 2 on board
  pinMode(switchPin2, INPUT);             // pin 3
  pinMode(switchPin3, INPUT);             // pin 4
  pinMode(switchPin4, INPUT);             // pin 5
  pinMode(switchPin5, INPUT);             // pin 6
  
  // Check which pin is set
  if (digitalRead(switchPin1) == HIGH)
     pauseTime = 3000;                     // 3 secs
  if (digitalRead(switchPin2) == HIGH)
     pauseTime = 6000;                     // 6 secs
  if (digitalRead(switchPin3) == HIGH)
     pauseTime = 10000;                    // 10 secs
  if (digitalRead(switchPin5) == HIGH)
     pauseTime = 999999999;                // stay on as long as possible
                                                      // don't worry about remaining code  
                                          
// Do this as long as switchPin4 is HIGH                                         
     if (digitalRead(switchPin4) == HIGH){
        pauseTime = 1000;                    // 1000 = ~1 sec      
      while(digitalRead(switchPin4) == HIGH)
      {
	delay(pauseTime);                             
        digitalWrite(ledPin[2], HIGH);
 	delay(pauseTime);
	digitalWrite(ledPin[2], LOW);
	delay(pauseTime);                      
        digitalWrite(ledPin[3], HIGH);
	delay(pauseTime);
	digitalWrite(ledPin[3], LOW);
        delay(pauseTime);                          
        digitalWrite(ledPin[4], HIGH);
	delay(pauseTime);
	digitalWrite(ledPin[4], LOW);
	delay(pauseTime);
	digitalWrite(ledPin[5], HIGH);
        delay(pauseTime);
	digitalWrite(ledPin[5], LOW);

      }
 }

// looks like switchPin4 went to LOW if it comes to this point

//the following is test code to figure out why while code does not work
	pauseTime = 3000;
        delay(pauseTime);                             
        digitalWrite(ledPin[2], HIGH);
 	delay(pauseTime);
	digitalWrite(ledPin[2], LOW);
	delay(pauseTime);                      
        digitalWrite(ledPin[2], HIGH);
	delay(pauseTime);
	digitalWrite(ledPin[2], LOW);
        delay(pauseTime);                          
        digitalWrite(ledPin[2], HIGH);
	delay(pauseTime);
	digitalWrite(ledPin[2], LOW);
	delay(pauseTime);
	digitalWrite(ledPin[2], HIGH);
        delay(pauseTime);
	digitalWrite(ledPin[2], LOW);
// end of test

// create random output pin
    randomSeed( analogRead( 0 ) );

    for ( int i=0 ; i < PIN_COUNT ; i++ ) {
        pinMode( ledPin[ i ], OUTPUT );
    }
}
void loop () {
  
  for ( int i=75 ; i ; i-- ) {             // 75 = ~3 secs
        ranNum = random( PIN_COUNT );
       
        for ( int pin=0 ; pin < PIN_COUNT ; pin++ ) {
            if ( pin == ranNum ) {
                digitalWrite( ledPin[ pin ], LOW );
            } else {
                digitalWrite( ledPin[ pin ], HIGH );
            }
        }
        delay( 20 + random( blinkTime ) );
    }
    delay( pauseTime );
}

Moderator edit: CODE TAGS. Please, can someone (anyone) tell me why such a simple thing is so difficult?

After writing to a bunch of input pins, make them output pins. What a great idea. Perhaps in the other order...

Paul, at almost 70 my brain is not so good anymore. I don't understand what you mean. My eyes are spinning from looking at the code over and over. What is wrong with my pin assignments? I do not see where I write to input pins. I have dedicated input and output pins.

kurtwm:
Paul, at almost 70 my brain is not so good anymore. I don't understand what you mean. My eyes are spinning from looking at the code over and over. What is wrong with my pin assignments? I do not see where I write to input pins. I have dedicated input and output pins.

Look at your code. Where, in setup(), do you set the mode for the pins whose numbers are in the ledPin array? What is the default condition of a pin when the Arduino starts up? What is the first thing you do with the pins whose numbers are in the ledPin array?

And the answers are:
At the end.
INPUT
Write to them.

Writing to an INPUT pin does something, but it is NOT what you want to do.

Thanks all for your comments and help. I pretty much change and cleaned up my code to reflect the suggestions made and it works now just the way I want to.

At first my primary goal was to build this targeting thing. As I went along and got more involved my primary goal changed it is now gaining a better understanding how circuits work and do's and don'ts (I blew up three Arduino boards...I know some of the don'ts now).

I do appreciate the way this forum works. I never expected someone to write the code for me, but by pointing me in the right directions and pointing out my mistakes it made me gain an understanding of what I am doing when I make this or that change.

Thanks

Next challenge is to replace the rotary switch with a remote control.

I blew up three Arduino boards...I know some of the don'ts now

That's funny.