/******************************************************************************* * * HB 04 - Três leds e um botão * Sugerido por Raphael * Autor: Angelo Luis Ferreira * http://squids.com.br/arduino * *******************************************************************************/ const byte led[] = {3, 5, 7}; const byte button = 9; uint32_t startPress; uint32_t lastPress; uint32_t serialTimer; boolean control = 0; boolean state; void setup(void) { for(byte i=0; i<3; i++) { pinMode(led[i], OUTPUT); } pinMode(button, INPUT_PULLUP); Serial.begin(9600); } // end setup void loop(){ state = digitalRead(button); // lê o estado do botão // verifica se o botão foi pressionado com todos leds apagados if (!state && control) { startPress = millis(); control = !control; } //verifica se o botão permanece pressionado if (!state && !control) { lastPress = millis() - startPress; digitalWrite(led[0], HIGH); if (lastPress >= 3000) digitalWrite(led[1], HIGH); if (lastPress >=5000) digitalWrite(led[2], HIGH); } else { for (byte i=0; i<3; i++) digitalWrite(led[i], LOW); startPress = millis(); lastPress = 0, control = 0; } // mostra valores no monitor serial (opcional) if (millis() - serialTimer >= 500) { Serial.print(state); Serial.print(" | "); Serial.print(control); Serial.print(" | "); Serial.println(lastPress); serialTimer = millis(); } } // end loop