Ciao a tutti, sto realizzando un timer con i display a sette segmenti e contemporaneamente accende e spegne un led.
Tutto questo è reso possibile dalla funzione millis solo che il timer va troppo veloce.
Il problema è che nel ciclo for del timer si è soliti a utilizzare il delay per temporizzare il tutto.
C’è un modo per ottenere lo stesso risultato usando il millis?.
CODICE
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
const int ledPin = 12;
int ledState = LOW;
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 2000
unsigned long previousMillis = 0;
const long interval = 100;
unsigned long previousMillis2 = 0;
const long interval2 = 1000;
const uint8_t SEG_DONE[] = {
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_C | SEG_E | SEG_G, // n
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
};
TM1637Display display(CLK, DIO);
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
int k;
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
display.setBrightness(0x0f);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Show decimal numbers with/without leading zeros
bool lz = false;
for (uint8_t z = 0; z < 2; z++) {
for(k = 60; k>= 0 ; k--) {
display.showNumberDec(k, lz);
}
lz = true;
}
}
if (currentMillis - previousMillis2 >= interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}