Code to increse frequency of blinking works, but not to decrease, please review

I mean it reads weird in terms of natural language (in this case English) grammar. Compare

  • if direction
  • if faster
  • if decreasing

Having variables named faster or decreasing reads OK; as do other boolean-ese words, like active or expired. But not direction.

Instead of bool direction true and false, byte deltaT could be 50 or -50 and then add deltaT to timer and check limits to reverse. That removes one if() and IIRC Arduino bool is 16 bits, hope I'm wrong!

  1. if faster else slower

(post deleted by author)

Hi @emilyrosewater

I think you are trying to do to many thinks at once.

@ Victoria,

I'm a dummy, but i did guess you were born in 2015.

Mine is 1942, bombs away.

Good luck........

I have a brother your age (1/20/42) who is doing fine.

Processor dependent.
8 bits on an Uno R3.

Fundamentaly the wrong approach.
"Delays are evil"

see How to code Timers and Delays in Arduino
and
Simple Multitasking Arduino on any board without using an RTOS

A lot of interesting things about blinking an LED have been discussed here, if you'd like to have a look.

Based on the idea discussed there, you could also do something like this. Since you're just learning programming, you might find the following code interesting to look at.

Of course, you don't have to do it this way — it's just an idea. :slightly_smiling_face:

#include <math.h>

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

void loop() {
  // Time since startup in seconds
  double t = millis() / 1000.0;

  // One complete slow -> fast -> slow cycle takes 10 seconds
  const double cycle = 10.0;

  // Minimum and maximum blink frequency
  const double fMin = 0.5;
  const double fMax = 1.0 / 0.3;

  double fMean = (fMin + fMax) / 2.0;
  double fAmp  = (fMax - fMin) / 2.0;
  double omega = 2.0 * PI / cycle;

  // Calculate a continuous phase from the changing frequency
  double phase = fMean * t - (fAmp / omega) * sin(omega * t);

  // Switch the LED state every half period
  digitalWrite(LED_BUILTIN, ((unsigned long)(phase * 2.0)) & 1);
}

This makes the LED blink continuously faster and then slower again, without using delay(), previousMillis, a direction variable, or a stored LED state.

The interesting part is that the LED state is calculated directly from millis().

You don't need to understand all the math behind it yet. I just thought it might be interesting to see what can be derived directly from millis().