Scope Question - Problem assigning value to variable inside For Loop

I am trying to scroll through the numbers 1-4 by using a button in the setup portion of the code. The number is displayed on an LCD and the user presses a second button to select the value and move onto the next part of the code. I want the variable rowcount to be equal to the loop counter. I know the for loop, buttons and LCD work how I want, but I can't use rowcount in the main loop of my code. I receive a 'rowcount' was not declared in this scope error message if I try to use the variable in the main loop.

How can I assign this variable so I can use it in the loop portion of my code?

Here is the for loop:

for (int i = 1; i >0; i = i){
    lcd.setCursor(15,0);
    lcd.print(i);
    buttononestate = digitalRead(buttononepin);
    buttontwostate = digitalRead(buttontwopin);

    if (buttononestate == HIGH) {
      i = i + 1;
      rowcount = rowcount + 1; 
      if (i == 5 ) {
        i = 1;
        rowcount = 1;
      }
      delay(200);
    }
    else if (buttontwostate == HIGH) {
      delay(100);
      break;
    }
    else {
      i = i;
    }
  }

If you declare rowcount as a global variable [at the top of your code, outside of any functions like setup() or loop()] then it will work.

Only posting a portion of your code and not the complete sketch makes it difficult to know since it is never declared

snippets R us... we don’t know where rowcount is declared and where this piece of code lives

That’s weird construct for (int i = 1; i >0; i = i){or also

else {
      i = i;
    }

What are you really trying to achieve ??

I was declaring it inside of the setup. The code works once I put it before. Thank you.

Below is the rest of the code for reference. Sorry for not posting it all before.

//#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
//char rx_byte = 0;
//String rx_str = "";
//boolean not_number = false;
//char input;
//int result;

int lightpin = 2;
int receiverpin = 3;
int buttononepin = 4;
int buttontwopin = 5;
int receiverstate = 0;
int lastreceiverstate = 0;
int buttononestate = 0;
int buttontwostate = 0;
int count = 0;
int rowcount = 1;
// the setup function runs once when you press reset or power the board

void setup() {
  Serial.begin(9600);
  //lcd.begin(20,4);
  //lcd.backlight();
  //lcd.setCursor(0,0);
  pinMode(lightpin, OUTPUT);
  pinMode(buttononepin, INPUT);
  pinMode(buttontwopin, INPUT);
  pinMode(receiverpin, INPUT);
  digitalWrite(lightpin, HIGH);
  //lcd.setCursor(0,0);
  //lcd.clear();
  int rowcount = 1;
  //lcd.print("Number of Rows: ");
  //lcd.setCursor(0,4);
  //lcd.print ("Enter");

  for (int i = 1; i >0; i = i){
    //lcd.setCursor(15,0);
    //lcd.print(i);
    buttononestate = digitalRead(buttononepin);
    buttontwostate = digitalRead(buttontwopin);

    if (buttononestate == HIGH) {
      i = i + 1;
      rowcount = rowcount++; 
      if (i == 5 ) {
        i = 1;
        rowcount = 1;
      }
      delay(200);
    }
    else if (buttontwostate == HIGH) {
      delay(100);
      break;
    }
    else {
      i = i;
    }
  }
  //lcd.clear();
  delay(200);
  buttononestate = digitalRead(buttononepin);
  while (buttononestate == LOW) {
    //lcd.setCursor(0,0);
    //lcd.print("Begin Count?");
    buttononestate = digitalRead(buttononepin);
    if (buttononestate == HIGH) {
      break;
    }
  }
 
  delay(1000);
  //lcd.clear();
  //lcd.setCursor(0,0);
  //lcd.print("Count: ");
  //lcd.setCursor(0,4);
  //lcd.print("Reset Count");

}

// the loop function runs over and over again forever
void loop() {
  receiverstate = digitalRead(receiverpin);
  buttononestate = digitalRead(buttononepin);
  buttontwostate = digitalRead(buttontwopin);

  if (receiverstate != lastreceiverstate) {
  
    if (receiverstate == HIGH) {
    count = count + rowcount;
    digitalWrite(LED_BUILTIN, HIGH);  
    }
    else {
    digitalWrite(LED_BUILTIN, LOW);
    }
    delay(50);
  }
  
  if (buttontwostate == HIGH) {
    count = 0;
  }
  //lcd.setCursor(7,0);
  //lcd.print(count);  
  //Serial.println(count);
  lastreceiverstate = receiverstate;
}

That’s also “crappy” rowcount = rowcount++;

I know. I already noticed that and changed it. Thanks though.

J-M-L:
That’s also “crappy” rowcount = rowcount++;

And undefined. Broke a lot of code around the 1.5 era when the behavior changed.

Delta_G: And undefined. Broke a lot of code around the 1.5 era when the behavior changed.

I had never heard this before about it being undefined. So is the example code in the ++ Arduino Language reference page bad? https://www.arduino.cc/reference/en/language/structure/compound-operators/increment/

y = ++x;  // x now contains 3, y contains 3
y = x++;  // x contains 4, but y still contains 3

Nothing wrong with the example, they are not assigning back to the same variable.

Ah, I understand now. Thanks!