int readtime=250;//delay 250 millisecond to execute next loop
const int LedPin=9;
const int PotOut=A2;
unsigned int PotVal;//output of potentiometer in range of 0-1023
float Ledval;
float BrightPercentage;
void setup() {
// put your setup code here, to run once:
pinMode(PotOut, INPUT);//declaring the input pin
Serial.begin(9600);//start the serial communication
}
void loop() {
// put your main code here, to run repeatedly:
PotVal=analogRead(PotOut);//read the output of potentiometer
Ledval=(255./1023.)*PotVal; // calibrate the value of led according to potentiometer
analogWrite(LedPin,Ledval);//using pwm pin to write analog data
BrightPercentage=(100./255.)*Ledval;
Serial.print("The brightness in percentage is: ");
Serial.println(BrightPercentage);
delay(readtime);//delay the execution of next loop
}