Variable not declared in this scope

I'm a beginner and from germany. So sorry for possible bad english.
I always get this error message:

exit status 1
'stufe' was not declared in this scope

I'm programming a RGB- controller programm, that creates 6 PWM signals on an Uno. The programm isn't complete and this wrote this code just to test the structure of the program. The way the program works and what sense the variables habe aren't relevant and so i'm not gonna explain that.
I just don't understand, why this error appears. I declared the variable 'stufe' in the 'setup()'.

Thanks in advance

void setup() {
  pinMode(2,INPUT);
  pinMode(4,INPUT);
  //PINS
  int red1 = 3;
  int green1 = 5;
  int blue1 = 6;
  int red2 = 9;
  int green2 = 10;
  int blue2 = 11;
  //OPVARS
  boolean cancel = false;
  boolean onepressed = false;
  boolean twopressed = false;
  int mode = 1;
  int stufe = 1; // HERE is 'stufe' declared
}

void render(int r, int g, int b) {
  switch(stufe) {
    case 1:
    analogWrite(red1,r);
    analogWrite(green1,g);
    analogWrite(blue1,b);
    break;
    case 2:
    analogWrite(red2,r);
    analogWrite(green2,g);
    analogWrite(blue2,b);
    break;
  }
}

void check(){
  if ((digitalRead(2)==HIGH)||(digitalRead(4)==HIGH)) {
    if ((digitalRead(2)==HIGH){ onepressed = true; }
    if ((digitalRead(4)==HIGH){ twopressed = true; }
  cancel = true;
  }
}

void manager() {
  cancel = false;
  if (onepressed==true) {
  switch(mode) {
    case 1:
    mode = 2;
    break;
    case 2:
    mode = 3;
    break;
    case 3:
    mode = 1;
    break;
    default:
    break;
  }
  }
  
  if (twopressed==true) {
  switch(mode) {
    case 1:
    break;
    case 2:
    break;
    case 3:
    break;
    default:
    break;
  }
  stufe++;
  if (stufe==3) { stufe = 1; }
  }

}

void loop() {

  check();
  if (cancel==true) { manager(); }

  if (mode==1) {
    switch(stufe) {         //HERE it throws the error
      case 1:
      render(150,0,0);
      break;
      case 2:
      render(0,250,0);
      break;
      case 3:
      render(0,0,250);
      break;
    }
  }

  if (mode==2) {
    analogWrite(red1,150);
    analogWrite(green1,250);
    analogWrite(blue1,250);
    analogWrite(red2,150);
    analogWrite(green2,250);
    analogWrite(blue2,250);
  }
  
}

Check out any tutorial on variable scope.

The variable is declared in the setup() so the visibility of stufe ends after the closing } of the setup(), and loop cannot see it.

See "SCOPE"

Mark

I put the variables outside, of any void. Now it works. Thanks!

hallolo:
I put the variables outside, of any void. Now it works. Thanks!

The important thing is: Do you understand why it works? Making a variable global to fix a bug is rarely a good idea.