Fading LED, for() loop with potentiometer

I am very new to Arduino but I am learning all the time. (at this point, 4 days)
I took a look at the already put together programs in the programming environment,
then chose the one with Pulse Width Modulation for fading an LED. (it is in OPEN, BASICS, FADE.)
I wanted to make it my own so I made an adjustment where in the for() loop, the test/condition is determined by the analog input of a potentiometer .

my code looks like this :

const int ledPin = 9;    // LED connected to digital pin 9-
const int pot = A0;

void setup()  { 
pinMode( ledPin,OUTPUT);
  pinMode (pot,INPUT);
Serial.begin(9600);
} 

void loop()  { 
 
int value = analogRead(pot);
  

  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= value; fadeValue += 5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = value ; fadeValue >= 0; fadeValue -= 5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
  Serial.println(value);
  delay(1000);
}

It works, to a degree..

The problems are that the serial port will not print anything. The communications and also, instead of fading on and off, it goes something like this :

fade from 0 to high 3 times one right after another , then fade from high to 0 three times, and repeat,
instead of fading from off to on and back again.

here are some pictures of wiring.

I will keep working, If you can find whats wrong or give advice, THANK YOU.

1228141412[1].jpg

1228141412b[1].jpg

Im sorry, in problems, that is supposed to say that the communications light was not even on instead of (the comminications)

Hi EskymoCho

Don't think you need this for analog input.

pinMode (pot,INPUT);

This will give a value between 0 and 1023. You need to scale the value to 0 to 255 for the analogWrite.

int value = analogRead(pot);

Try this:

int value = analogRead(pot) / 4;

the serial port will not print anything

Do you have the speed in serial monitor set to 9600, to match the speed set in your program?

Regards

Ray

I like to use the map() function whilst reading analog values. Then I can set min/max within useful range. In your case 0-255. You may also want to look into the constrain() function.

thanks guy so much. It works now! hackscribble's comment helped.

hey knystol, I looked on this site'spage about map at map() - Arduino Reference this says that all the analog values over 255will be omitted. Hackscribble's way works by using a proportion instead. if there is something I am missing please let me know.

Map will take a range let's say from 0-1023 and map it to let's say 0-255.

You can have a pot that should map let's say 4 different levels, then use map() and map from 0-1023 (analog read will always give you this initial range, at least on the Uno), then map from 1-4, this is how great the map()-function is!