Finding first HIGH signal in a row of pins

Hi. I'm new to Arduino (and to programming in general, for that matter). After some blinking and buzzing I decided to try and write something on my own. I chose to write a sketch that would "move" HIGH signal along a row of pins.
Like, we have LOW--LOW--HIGH--LOW--LOW pins in a row and can move "HIGH" section left and right by pressing one button or another ("click -- one move -- click -- one move" etc.). If we move "HIGH" past the edge, in returns on the opposite side.
The resulting sketch is (with 3 output pins):

// Assigning pins for ASCending and DEScending pin switching buttons.
const int ASC_BUTTON = 3;
const int DES_BUTTON = 2;

// Booleans used for debouncing the buttons.
bool previous_ASC_button_state = false;
bool previous_DES_button_state = false;

// Set of digital pins we're going to use as outputs.
const int first_OUT = 10;
const int last_OUT = 12;

// These variables allow us to turn on current chosen pin and turn off all the others.
int current_OUT, previous_ASC_OUT, previous_DES_OUT;

void setup()
{
  // Setting button pins to be in HIGH state by default.
  pinMode(ASC_BUTTON, INPUT_PULLUP);
  pinMode(DES_BUTTON, INPUT_PULLUP);

  // Setting output pins.
  for (int output_pins = first_OUT; output_pins <= last_OUT; output_pins++) {
    pinMode(output_pins, OUTPUT);
  }

  // 11th pin will always be the first one to turn on upon boot.
  current_OUT = 11;
  // It's like closing the door behind yourself. When a new pin becomes ON, previous one becomes OFF.
  // Therefore we must define what "previous" is.
  previous_ASC_OUT = current_OUT - 1;
  previous_DES_OUT = current_OUT + 1;
}

void loop()
{
  // Turning on current pin and turning off previous pins.
  digitalWrite(current_OUT, HIGH);
  digitalWrite(previous_ASC_OUT, LOW);
  digitalWrite(previous_DES_OUT, LOW);

  // Finding current pin – CURRENT VARIANT.
  for (int output_pins = first_OUT; output_pins <= last_OUT; output_pins++) {
    if (digitalRead(output_pins) == HIGH) {
    current_OUT = output_pins;
    }
  }

  /* Finding current pin – OLDER AND DUMBER VARIANT.
  if (digitalRead(12) == HIGH && digitalRead(11) == LOW && digitalRead(10) == LOW) {
    current_OUT = 12;
  }
  if (digitalRead(12) == LOW && digitalRead(11) == HIGH && digitalRead(10) == LOW) {
    current_OUT = 11;
  }
  if (digitalRead(12) == LOW && digitalRead(11) == LOW && digitalRead(10) == HIGH) {
    current_OUT = 10;
  }
  */

  // ASCending pins switching.
  bool current_ASC_button_state = !digitalRead(ASC_BUTTON); // When the button is pressed, current button state is TRUE.
  if (current_ASC_button_state && current_ASC_button_state != previous_ASC_button_state) {
    delay(20); // Debounce.
    current_ASC_button_state = !digitalRead(ASC_BUTTON); // Debounce.
      if (current_ASC_button_state) {
        current_OUT++;
        previous_ASC_OUT = current_OUT - 1;
        delay(10);
      }
  }

  previous_ASC_button_state = current_ASC_button_state; // This line prevents switching by keeping the button pressed.

  // ASCending count interrupt.
  if (current_OUT >= 13) {
    digitalWrite(current_OUT, LOW);
    digitalWrite(previous_ASC_OUT, LOW);      
    current_OUT = first_OUT;
    previous_ASC_OUT = current_OUT - 1;
  }

  // DEScending pins switching.
  bool current_DES_button_state = !digitalRead(DES_BUTTON); // When the button is pressed, current button state is TRUE.
  if (current_DES_button_state && current_DES_button_state != previous_DES_button_state) {
    delay(20); // Debounce.
    current_DES_button_state = !digitalRead(DES_BUTTON); // Debounce.
      if (current_DES_button_state) {
        current_OUT--;
        previous_DES_OUT = current_OUT + 1;
        delay(10);
      }
  }

  previous_DES_button_state = current_DES_button_state; // This line prevents switching by keeping the button pressed.

  // DEScending count interrupt.
  if (current_OUT <= 9) {
    digitalWrite(current_OUT, LOW);
    digitalWrite(previous_DES_OUT, LOW);
    current_OUT = last_OUT;
    previous_DES_OUT = current_OUT + 1;
  }
}

The code works. I attached thee differently colored LEDs with 200 Ohm resistors between GND and respective output pins and enjoyed switching the LEDs by changing current ON pin.
As you can see, in order to move current ON pin the code first has to find out which pin is currently ON. First I did it in a dumb and straightforward way:

if (digitalRead(12) == HIGH && digitalRead(11) == LOW && digitalRead(10) == LOW) {
    current_OUT = 12;
  }
  if (digitalRead(12) == LOW && digitalRead(11) == HIGH && digitalRead(10) == LOW) {
    current_OUT = 11;
  }
  if (digitalRead(12) == LOW && digitalRead(11) == LOW && digitalRead(10) == HIGH) {
    current_OUT = 10;

Then I thought I want some more elegant solution and wrote this:

for (int output_pins = first_OUT; output_pins <= last_OUT; output_pins++) {
    if (digitalRead(output_pins) == HIGH) {
    current_OUT = output_pins;
    }

My problem is I don't quite understand how this last part works. When I wrote it, it was more like a guesswork, an instinctive coding. I wanted "for" statement to run through the pins 10, 11 and 12 and as soon as it finds HIGH signal I wanted it to assign HIGH pin as current_OUT. Is that how "for" actually works? I mean, it comes through all the variants (in my case 10, 11, 12) and triggers the "if" statement as soon as it finds the proper signal?

Sorry for being hectic, it's my first time when I'm asking a question about coding.

It sounds like you are badly in need of learning to use arrays

Put the LED pin numbers in an array and change the number of the array index variable using the buttons to access the current LED

Way overly complicated. Here is a less complicated way:

// Assigning pins for ASCending and DEScending pin switching buttons.
const int ASC_BUTTON = 3;
const int DES_BUTTON = 2;

// Set of digital pins we're going to use as outputs.
const int first_OUT = 10;
const int last_OUT = 12;

byte previous_ASC_button_state;
byte previous_DES_button_state;

// These variables allow us to turn on current chosen pin and turn off all the others.
int current_OUT = first_OUT;

void setup()
{
  // Setting button pins to be in HIGH state by default.
  pinMode(ASC_BUTTON, INPUT_PULLUP);
  pinMode(DES_BUTTON, INPUT_PULLUP);
  previous_ASC_button_state = digitalRead(ASC_BUTTON);
  previous_DES_button_state = digitalRead(DES_BUTTON);

  // Setting output pins.
  for (int output_pins = first_OUT; output_pins <= last_OUT; output_pins++) {
    pinMode(output_pins, OUTPUT);
    digitalWrite(output_pins, LOW);
  }

  digitalWrite(current_OUT, HIGH);
}

void loop()
{
  byte current_ASC_button_state = digitalRead(ASC_BUTTON);
  if (current_ASC_button_state != previous_ASC_button_state) {
    delay(50); // Debounce.
    if (current_ASC_button_state == LOW)  
    {
      digitalWrite(current_OUT, LOW);
      current_OUT++;
      if (current_OUT > last_OUT) current_OUT = first_OUT;
      digitalWrite(current_OUT, HIGH);
    }
    previous_ASC_button_state = current_ASC_button_state;
  }

  byte current_DES_button_state = digitalRead(DES_BUTTON);
  if (current_DES_button_state != previous_DES_button_state) {
    delay(50); // Debounce.
    if (current_DES_button_state == LOW) {
      digitalWrite(current_OUT, LOW);
      current_OUT--;
      if (current_OUT < first_OUT) current_OUT = last_OUT;
      digitalWrite(current_OUT, HIGH);
    }
    previous_DES_button_state = current_DES_button_state;
  }
}

A more general purpose solution

// Assigning pins for ASCending and DEScending pin switching buttons.
const int ASC_BUTTON = A2;
const int DES_BUTTON = A3;

// Set of digital pins we're going to use as outputs.
const byte ledPins[] = {3, 5, 6};
const byte NUMBER_OF_LEDS = sizeof(ledPins);
int currentLed = 0;

byte previous_ASC_button_state;
byte previous_DES_button_state;

void setup()
{
  // Setting button pins to be in HIGH state by default.
  pinMode(ASC_BUTTON, INPUT_PULLUP);
  pinMode(DES_BUTTON, INPUT_PULLUP);
  previous_ASC_button_state = digitalRead(ASC_BUTTON);
  previous_DES_button_state = digitalRead(DES_BUTTON);
  // Setting output pins.
  for (int led = 0; led < NUMBER_OF_LEDS; led++)
  {
    pinMode(ledPins[led], OUTPUT);
    digitalWrite(ledPins[led], LOW);
  }
  digitalWrite(ledPins[currentLed], HIGH);
}

void loop()
{
  byte current_ASC_button_state = digitalRead(ASC_BUTTON);
  if (current_ASC_button_state != previous_ASC_button_state)
  {
    delay(50); // Debounce.
    if (current_ASC_button_state == LOW)
    {
      digitalWrite(ledPins[currentLed], LOW);
      currentLed++;
      currentLed = currentLed > NUMBER_OF_LEDS - 1 ? 0 : currentLed;
    }
    previous_ASC_button_state = current_ASC_button_state;
  }
  byte current_DES_button_state = digitalRead(DES_BUTTON);
  if (current_DES_button_state != previous_DES_button_state)
  {
    delay(50); // Debounce.
    if (current_DES_button_state == LOW)
    {
      digitalWrite(ledPins[currentLed], LOW);
      currentLed--;
      currentLed = currentLed < 0 ? 2 : currentLed;
    }
    previous_DES_button_state = current_DES_button_state;
  }
  digitalWrite(ledPins[currentLed], HIGH);
}

Any number of LEDS on any combination of pins

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.