/*
Lezione 21: Blinking led senza delay
Impiego della funzione millis() per fare lampeggiare un led
Creato 20 Mar 2020
da Andrea Primavera
*/
int pinLed = 9; // Pin LED
int stateLed = LOW; // Stato del LED (LOW/HIGH)
int puls1 = 2;
int pulsante1_stato = HIGH;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0; // Utilizzata per memorizzare il tempo del ciclo precedente
const long interval = 500; // Tempo in millisecondi utilizzato per fare lampeggiare il led
bool stato_puls1 = false;
void setup() {
pinMode(pinLed, OUTPUT);
pinMode(puls1, INPUT);
Serial.begin(9600);
int pulsante1_stato = digitalRead(puls1);
Serial.println(pulsante1_stato);
}
void loop() {
pulsante1_stato = digitalRead(puls1);
delay(100);
if (pulsante1_stato == LOW) {
// switch is pressed - pullup keeps pin high normally
// delay to debounce switch
stato_puls1 = !stato_puls1;
stateLed = stato_puls1;
if (stateLed == LOW) {
stateLed = HIGH;
} else {
stateLed = LOW;
}
digitalWrite(pinLed, stateLed); // toggle running variable
// digitalWrite(pinLed, stato_puls1); // indicate via LED
}
}
/*if (pulsante1_stato == 0 ){
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.println(previousMillis);
Serial.println(pulsante1_stato);
if (stateLed == LOW) {
stateLed = HIGH;
} else {
stateLed = LOW;
}
digitalWrite(pinLed, stateLed);
}
}
currentMillis = millis();
int delayMillis = currentMillis + 1000;
Serial.println("123");
if (currentMillis - delayMillis >=1000){
if (stateLed == LOW) {
stateLed = HIGH;
} else {
stateLed = LOW;
}
digitalWrite(pinLed, stateLed);
}
/* if (pulsante1_stato == 1){
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.println(previousMillis);
Serial.println(pulsante1_stato);
if (stateLed == LOW) {
stateLed = HIGH;
} else {
stateLed = LOW;
}
digitalWrite(pinLed, stateLed);
}
}
/*
// switch is pressed - pullup keeps pin high normally
delay(100); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(pinLed, running); // indicate via LED
}
}
*/