Code Improvement - LED blink (pin 13)

Is there anything more to improve for accelerating an LED (pin 13) from 0 to 1000ms of delay and decelerating from 1000ms down to 0, looped?

LED without a current limit resistor is a bad idea.

Posting an image of code is a bad idea.

Please read the forum guidelines to see how to properly post code.

oh yes im new to this community and it was like my 3rd day of learning from Simon Monk's "Programming Arduino"

im currently using a virtual arduino simulator - TinkerCAD well ye cuz my stuff havent arrived,
aside from the current (led exceeded 20mA and reached less than 50mA)
is the code that bad that it needs improvement
how can you rate it for me who is learning for 5 hours

I would test the code for you but I am not going to type it all in to my IDE. If you post the code in code tags I can just copy and paste it.

What does

accelerating an LED (pin 13)

mean?

If that's the way you are doing it, that's the way to do it. Looks entirely plausible. If it works, congrats!

We could be sure if you had included your code in a more convenient manner. Then anyone can cut and paste and try it for herself. No one is going to re-type even a small program!

See the forum instructions pinned at the top for future reference.

But now you want to learn how to do it another way, in you Arduino IDE select

File menu File/Examples/02.Digital/BlinkWithoutDelay

and give "blink without delay" a bit of attention. Then challenge yourself to see if you can make it do the same kind of nice accelerating/decelerating behaviour!

HTH and good luck.

a7

int ledPin = 13;
int delayPeriod = 0;
int i = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  while (delayPeriod < 1000)
  {
  digitalWrite(ledPin, HIGH);
  delay(delayPeriod);
  digitalWrite(ledPin, LOW);
  delay(delayPeriod);
  delayPeriod = delayPeriod + 50;
  i += 50;
  }
  delay(2000);
  while (i > 0)
  {
    digitalWrite(ledPin, HIGH);
    delay(delayPeriod);
    digitalWrite(ledPin, LOW);
    delay(delayPeriod);
    delayPeriod = delayPeriod - 50;
    i -= 50;
  }  
  delay(2000);
}

Here is the code for everyone to copy and paste;

Loop er, loops. It's generally best not to add while loops inside it too.

In your case here, it doesn't matter, but on another project, particularly if you want to respond to a button press in a timely manner, a while loop may mess things up.

verniumen:
Here is the code for everyone to copy and paste;

Thanks. Works good. As has been pointed out, this is very much a beginner's approach, but hey! you nailed it, so.

I like it better without the delay(2000), or at least a very shorter delay, between acceleration and decelerations. I didn't notice those until I was running you program, I thought I would be looking for a logic error that caused the gap where the LED is off for quite some time.

But it was just those delays.

On to "blink without delay"! Srsly, it's time.

a7

All I would add is try to be consistent:

  delayPeriod = delayPeriod + 50;
  i += 50;

One way or the other.

For counting, 'for' loops are generally easier to understand than 'while' loops:

void loop()
{
  for (delayPeriod = 1; delayPeriod < 1000; delayPeriod += 50)
  {
    digitalWrite(ledPin, HIGH);
    delay(delayPeriod);
    digitalWrite(ledPin, LOW);
    delay(delayPeriod);
  }
  
  delay(2000);
  
  for (delayPeriod = 1000; delayPeriod > 0; delayPeriod -= 50)
  {
    digitalWrite(ledPin, HIGH);
    delay(delayPeriod);
    digitalWrite(ledPin, LOW);
    delay(delayPeriod);
  }


  delay(2000);

}

A for loop is just a while loop in a posh frock.

Neither should be encouraged in this situation.

Think about this approach (code is untested):

#define LED_PIN 13
#define DELAY_LOW 0
#define DELAY_HIGH 1000
#define INVERT_DELAY 2000

int blink_delay = 0;
int delay_step = 10;

void invert_direction()
{
  delay_step = -delay_step;
  delay(INVERT_DELAY);
}

void setup()
{
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop()
{
  digitalWrite(LED_PIN, HIGH);
  delay(blink_delay);
  digitalWrite(LED_PIN, LOW);
  delay(blink_delay);

  blink_delay += delay_step;
  if (blink_delay >= DELAY_HIGH)
  {
    blink_delay = DELAY_HIGH;
    invert_direction();
  }
  else if (blink_delay <= DELAY_LOW)
  {
    blink_delay = DELAY_LOW;
    invert_direction();
  }
}