How to do this code with no for loop?

void setup() {
pinMode(LED, OUTPUT); //set pin 11 as OUTPUT
}

void loop() {
for (int i = 0; i < 255; i++){ //if i is less than 255 then increase i with 1
analogWrite(LED, i); //write the i value to pin 11
delay(5); //wait 5 ms then do the for loop again
}
for (int i = 255; i > 0; i--){ //descrease i with 1
analogWrite(LED, i);
delay(5);
}
}

Perhaps you could use the fact that loop() already loops, and just use a counter to keep track of how many times its looped?

(edit: and I meant to say: and use millis() to keep track of the time to see if an increment is due or not, ala blink without delay().)

Why do you want to do this? Are you trying to implement a state machine for this.
If so make your loop index i a static int variable set to zero. Next line do the analogWrite, then increment i and check that it is less than 255. If it is then set the return time to the delay value and exit the function. Do not renter the function until that time has passed.

Repeat the process for the fade down section.

i have mission to to this with no for loop tnx guys i will try it

complete useless but anyhow:

const byte pwmPin = 11;  // pins 3, 5, 6, 9, 10, and 11
const byte intervall = 5;

void setup() {
  pinMode(pwmPin, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  static bool direction = true;
  static uint32_t previousMillis = 0;
  static int16_t i = 0;

  if (millis() - previousMillis > intervall)
  {
    if (i < 1) direction = true;
    if (i > 254) direction = false;
    if (direction) i++; else i--;
    analogWrite(pwmPin, i);
    Serial.println(i);
    previousMillis = millis();
  }
}

noiasca:
complete useless

Why do you say that?

noiasca:

  if (millis() - previousMillis > intervall)

Should that not be

  if (millis() - previousMillis >= intervall)

?

sayHovis:
Why do you say that?

Because I dislike that kind of exercises "do this ... without that ...".
Why should someone avoid the usage of for loops?

@Paul: it's up to your preference, you can use >= also. I guess you know what you are doing.

noiasca:
complete useless but anyhow:

Can I suggest a simplification?

const byte pwmPin = 11;  // pins 3, 5, 6, 9, 10, and 11
const byte intervall = 5;

void setup() {
  pinMode(pwmPin, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  static int16_t direction = 1;
  static uint32_t previousMillis = 0;
  static int16_t i = 0;

  if (millis() - previousMillis >= intervall)
  {
    if (i < 1 || i > 254) direction = -direction;
    i += direction;
    analogWrite(pwmPin, i);
    Serial.println(i);
    previousMillis = millis();
  }
}

noiasca:
Why should someone avoid the usage of for loops?

There might be some other stuff that the sketch needs to do, like respond to a button or do a similar but differently interval-ed fade on another pin.

Why should someone avoid the usage of for loops?

  1. because they want to implement a state machine

  2. it is a homework question given to test the student’s knowledge about coding. This is what I believe was stated here when the OP said

i have mission to to this

Well, if

Grumpy_Mike:
it is a homework question given to test the student’s knowledge about coding.

... s/he won't have learned much by just having been given a turnkey sketch, although the ...

KiKI7:
mission

... is accomplished :wink:

There might be some other stuff that the sketch needs to do

... then the requirement should be formulated: write non-blocking code to enable the sketch do something else in parallel. But asking for B but targeting A is strange.

  1. because they want to implement a state machine

The same story. Then the requirement should be "write a state machine"

@PaulRB: simpler in code for shure, but I don't think that this is easier to understand for the TO.

Was there any particular reason why you have chosen a static int16_t direction instead of an 8 bit integer?

To me, its easier to understand. For example direction=true or false does not, by itself, tell you what should happen to "i". Incrementing is no more "true" than decrementing from a common sense point of view. But direction=1 or -1 is more indicative.

I chose int16_t to match the definition of "i". They could both be int8_t. This would be slightly more efficient on AVR based Arduino. But on SAMD21 or ESP8266 based Arduino for example, it would make no difference.

PaulRB:
They could both be int8_t.

Oops, sorry. "i" cannot be int8_t. It needs to go up to 255 but int8_t only goes to 127. But it could be uint8_t.