LED - Lampeggia senza ritardo

a) Codice Arduino - Con Delay()

// constants won't change:
const int LED_PIN = 3;     // the number of the LED pin
const int BUTTON_PIN = 7;  // the number of the button pin
const long BLINK_INTERVAL = 1000;   // interval at which to blink LED (milliseconds)
// Variables will change:
int ledState = LOW;   // ledState used to set the LED
int previousButtonState = HIGH; // will store last time button was updated
void setup() {
  Serial.begin(9600);
  // set the digital pin as output:
  pinMode(LED_PIN, OUTPUT);
  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
  // if the LED is off turn it on and vice-versa:
  ledState = (ledState == LOW) ? HIGH : LOW;
  // set the LED with the ledState of the variable:
  digitalWrite(LED_PIN, ledState);
  delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect
  int currentButtonState = digitalRead(BUTTON_PIN);
  if(currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);
    // save the last state of button
    previousButtonState = currentButtonState;
  }
  // DO OTHER WORKS HERE
}

b) Codice Arduino - Senza Delay()

// constants won't change:
const int LED_PIN = 3;     // the number of the LED pin
const int BUTTON_PIN = 7;  // the number of the button pin
const long BLINK_INTERVAL = 1000;   // interval at which to blink LED (milliseconds)
// Variables will change:
int ledState = LOW;   // ledState used to set the LED
int previousButtonState = HIGH; // will store last time button was updated
unsigned long previousMillis = 0;   // will store last time LED was updated
void setup() {
  Serial.begin(9600);
  // set the digital pin as output:
  pinMode(LED_PIN, OUTPUT);
  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
  /* check to see if it's time to blink the LED; that is, if the difference between the current time and last time you blinked the LED is bigger than the interval at which you want to blink the LED.*/
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= BLINK_INTERVAL) {
    // if the LED is off turn it on and vice-versa:
    ledState = (ledState == LOW) ? HIGH : LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN, ledState);
    // save the last time you blinked the LED
    previousMillis = currentMillis;
  }
  // check button state's change
  int currentButtonState = digitalRead(BUTTON_PIN);
  if(currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);
    // save the last state of button
    previousButtonState = currentButtonState;
  }
  // DO OTHER WORKS HERE
}

c) Aggiunta di più attività

// constants won't change:
const int LED_PIN_1 = 3;            // the number of the LED 1 pin
const int LED_PIN_2 = LED_BUILTIN;  // the number of the LED 2 pin
const int BUTTON_PIN = 7;           // the number of the button pin
const long BLINK_INTERVAL_1 = 1000;  // interval at which to blink LED 1 (milliseconds)
const long BLINK_INTERVAL_2 = 500;   // interval at which to blink LED 2 (milliseconds)
// Variables will change:
int ledState_1 = LOW;   // ledState used to set the LED 1
int ledState_2 = LOW;   // ledState used to set the LED 2
int previousButtonState = HIGH; // will store last time button was updated
unsigned long previousMillis_1 = 0;   // will store last time LED 1 was updated
unsigned long previousMillis_2 = 0;   // will store last time LED 2 was updated
void setup() {
  Serial.begin(9600);
  // set the digital pin as output:
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
  unsigned long currentMillis = millis();
  // check to see if it's time to blink the LED 1
  if (currentMillis - previousMillis_1 >= BLINK_INTERVAL_1) {
    // if the LED is off turn it on and vice-versa:
    ledState_1 = (ledState_1 == LOW) ? HIGH : LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN_1, ledState_1);
    // save the last time you blinked the LED
    previousMillis_1 = currentMillis;
  }
  // check to see if it's time to blink the LED 2
  if (currentMillis - previousMillis_2 >= BLINK_INTERVAL_2) {
    // if the LED is off turn it on and vice-versa:
    ledState_2 = (ledState_2 == LOW) ? HIGH : LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(LED_PIN_2, ledState_2);
    // save the last time you blinked the LED
    previousMillis_2 = currentMillis;
  }
  // check button state's change
  int currentButtonState = digitalRead(BUTTON_PIN);
  if(currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);
    // save the last state of button
    previousButtonState = currentButtonState;
  }
  // DO OTHER WORKS HERE
}