I don't understand why this code is not working

void setup() {
  // put your setup code here, to run once:
const int REDPIN = 3;
const int GREENPIN = 5;
const int BLUEPIN = 6;
int KNOB1 = 0;
int KNOB2 = 0;
int RED = 0;
int GREEN = 0;
int BLUE = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
KNOB1 = analogRead(A0);
KNOB2 = analogRead(A1);
RED = map(KNOB1, 1, 1023, 1, 255);
GREEN = map(KNOB2, 1, 1023, 1, 255);
BLUE = 255;
analogWrite(REDPIN, RED);
analogWrite(GREENPIN, GREEN);
analogWrite(BLUEPIN, BLUE);
}

The error message I keep getting is:

Arduino: 1.8.4 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\rahul\Documents\Home\Coding\Arduino\RGB_LED_POTENTIOMETER\RGB_LED_POTENTIOMETER.ino: In function 'void loop()':

RGB_LED_POTENTIOMETER:15: error: 'KNOB1' was not declared in this scope

 KNOB1 = analogRead(A0);

 ^

RGB_LED_POTENTIOMETER:16: error: 'KNOB2' was not declared in this scope

 KNOB2 = analogRead(A1);

 ^

RGB_LED_POTENTIOMETER:17: error: 'RED' was not declared in this scope

 RED = map(KNOB1, 1, 1023, 1, 255);

 ^

RGB_LED_POTENTIOMETER:18: error: 'GREEN' was not declared in this scope

 GREEN = map(KNOB2, 1, 1023, 1, 255);

 ^

RGB_LED_POTENTIOMETER:19: error: 'BLUE' was not declared in this scope

 BLUE = 255;

 ^

RGB_LED_POTENTIOMETER:20: error: 'REDPIN' was not declared in this scope

 analogWrite(REDPIN, RED);

             ^

RGB_LED_POTENTIOMETER:21: error: 'GREENPIN' was not declared in this scope

 analogWrite(GREENPIN, GREEN);

             ^

RGB_LED_POTENTIOMETER:22: error: 'BLUEPIN' was not declared in this scope

 analogWrite(BLUEPIN, BLUE);

             ^

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

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

If anyone could make me understand how to fix the code I would be really grateful.

Read about Scope.

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

'KNOB1' was not declared in this scopeThe error message says it all.

You have a number of variables defined within the setup() function. They do not exist outside that function.

If you want variables to be accessible anywhere in your program then define them before setup().

...R

Ok thanks for helping me understand!