[HELP] Controlling Led with Potentiometer

Hi guys, I got a problem here and i am also a beginner and learned some of few basic codes. I wanted to control my led using potentiometer and i know its range is from 0 - 1023. While the analogWrite is 0 - 255. I wanted to light up my 4 led until its range at max 1023 will light up all led but with some fading style. I managed to do it successfully with digitalWrite but with analogWrite when i spool up my potentiometer it resets the led again. Here is the default code i used its digitalWrite.

int potpin = A0; // my analog A0 pin //
int sensorValue = 0; // Stores Sensor Value and Print later At Serial //
#define ledpin7 7
#define ledpin8 8 // defines all led //
#define ledpin9 9
#define ledpin10 10

void setup()
{
Serial.begin(9600);
pinMode(ledpin7, OUTPUT);
pinMode(ledpin8, OUTPUT); // i set all led to output //
pinMode(ledpin9, OUTPUT);
pinMode(ledpin10, OUTPUT);

}

void loop()
{

sensorValue = map(sensorValue , 0, 1023 , 0 , 255);
sensorValue = analogRead(potpin);
Serial.print("You are writing a value of "); // prints the value //
Serial.println(sensorValue);

if (sensorValue >= 250) {
digitalWrite(ledpin7, HIGH);
} else { // if the sensorValue reach >= 250 led will light up else no //
digitalWrite(ledpin7, LOW);
}

if (sensorValue >= 500) {
digitalWrite(ledpin8, HIGH);
} else { // same //
digitalWrite(ledpin8, LOW);
}

if (sensorValue >= 750) {
digitalWrite(ledpin9, HIGH);
} else {
digitalWrite(ledpin9, LOW);
}
// same
if (sensorValue >= 1000) {
digitalWrite(ledpin10, HIGH);
} else {
digitalWrite(ledpin10, LOW);
}

}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.
CCing @232 because I know they love code tags:

232:

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please post your code using analogWrite().

Carl_Anthony:
I wanted to control my led using potentiometer and i know its range is from 0 - 1023. While the analogWrite is 0 - 255. I wanted to light up my 4 led until its range at max 1023 will light up all led but with some fading style.

Try this (untested) sketch for four LEDs.
Leo..

// pot fades four LEDs after each other
const byte ledPin[] = {3, 9, 10, 11}; // four PWM LED pins (Uno)
int rawValue; // holds pot value

void setup() {
  for (byte i = 0; i < 4; i++) pinMode(ledPin[i], OUTPUT);
}

void loop() {
  rawValue = analogRead(A0); // potPin
  analogWrite(ledPin[0], constrain(rawValue, 0, 255));
  analogWrite(ledPin[1], constrain(rawValue, 256, 511) - 256);
  analogWrite(ledPin[2], constrain(rawValue, 512, 767) - 512);
  analogWrite(ledPin[3], constrain(rawValue, 768, 1023) - 768);
  delay(100);
}

pert:
Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.
CCing @232 because I know they love code tags:
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please post your code using analogWrite().

Sorry about that i will use it next time

Wawa:
Try this (untested) sketch for four LEDs.
Leo..

// pot fades four LEDs after each other

const byte ledPin[] = {3, 9, 10, 11}; // four PWM LED pins (Uno)
int rawValue; // holds pot value

void setup() {
  for (byte i = 0; i < 4; i++) pinMode(ledPin[i], OUTPUT);
}

void loop() {
  rawValue = analogRead(A0); // potPin
  analogWrite(ledPin[0], constrain(rawValue, 0, 255));
  analogWrite(ledPin[1], constrain(rawValue, 256, 511) - 256);
  analogWrite(ledPin[2], constrain(rawValue, 512, 767) - 512);
  analogWrite(ledPin[3], constrain(rawValue, 768, 1023) - 768);
  delay(100);
}

Thanks for the code it works. At first test it didn't work cause my pin is at the wrong one it is not plugged at pwm pins and i modified it a little to print the value of the pot.