# Newbie: Need Simple variable?

See my working code below:
I would like to know how to make a sketch for loop that assigns a variable for the 0-255 and starting over again when finished (loop). So i assume there needs to be a variable assigned and some command to increment it through each loopthrough? Please advise...be kind, I am totally new to Arduino and coding. Thanks,
Jeff

int ledPin = 13; // LED connected to digital pin 13

void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
}

void loop()
{

analogWrite(ledPin,5); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
delay(1000);
analogWrite(ledPin,255); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
delay(1000);
}

This is an XY Problem.

``````for (int i = 0; i < 255; i = i + 25)
{
analogWrite(ledPin, i);
delay(100);
analogWrite(ledPin, i);
}
``````

As pin 13 is not a PWM pin then all the LED will do is turn on and off.
Try this

``````const byte ledPin = 5;      // LED connected to a PWM pin
unsigned long startTime = 0;
const unsigned long period = 10;
byte counter = 0;

void setup()
{
pinMode(ledPin, OUTPUT);   // sets the pin as output
}

void loop()
{
if (millis() - startTime >= period)
{
counter++;
startTime = millis();
}
analogWrite(ledPin, counter); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
``````

Shpaget:

``````for (int i = 0; i < 255; i = i + 25)
``````

{
analogWrite(ledPin, i);
delay(100);
analogWrite(ledPin, i);
}

Why are there two analogWrite() statements in the loop that do exactly the same thing?

PaulS:
Why are there two analogWrite() statements in the loop that do exactly the same thing?

Lazy copy pasting. :-[
(Explanation, not an excuse)

(Explanation, not an excuse)

That was my assumption.