//Global Variablesg
/*
const byte BUTTON=2; // our button pin
const byte LED=13; // LED (built-in on Uno)
unsigned long interval1=5000; // the time we need to wait
unsigned long interval2=4000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
unsigned long currentMillis=0;
bool ledState = false; // state variable for the LED
int i =0;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.begin(115200);
}
void loop() {
// get the time at the start of this loop()
ledState = digitalRead(BUTTON);
if (ledState == false){
previousMillis = millis();
while(!ledState){
ledState = digitalRead(BUTTON);
delay(1);
}
currentMillis =millis();
Serial.print("Waktu anda menekan: ");
Serial.println(currentMillis -previousMillis);
if(currentMillis -previousMillis >=interval1){
for (int i = 0; i<=5;i++){
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}
}
else if(currentMillis -previousMillis <=interval2 && 1000<=currentMillis -previousMillis ){
for (int i = 0; i<=2;i++){
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
}
}
}
*/
//====================================================================
/*
//Global Variables
const byte BUTTON=2; // our button pin
const byte LED=13; // LED (built-in on Uno)
unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 2500; // wait to turn on LED
unsigned long turnOffDelay = 5000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}
void loop() {
// get the time at the start of this loop()
unsigned long currentMillis = millis();
// check the button
if (digitalRead(BUTTON) == LOW) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (ledReady) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(LED, HIGH);
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
// wait for next button press
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
ledState = false;
digitalWrite(LED, LOW);
}
}
}
*/
//==============================================
// Press and hold button for 100 ms to turn LED ON.
// Press and hold button for 1000 ms to turn LED OFF.
#include <Bounce2.h>
byte buttonState = 0;
const byte ledPin = 13;
const byte buttonPin = 2;
const unsigned long debouncerInterval = 50;
unsigned long buttonPressedTime = 0;
unsigned long currentMillis = 0;
Bounce debouncer = Bounce();
void setup(){
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
debouncer.attach(buttonPin, INPUT);
debouncer.interval(debouncerInterval);
}
void loop(){
Serial.println("test serial");
Serial.println(buttonState);
//delay(500);
// Get the current time.
currentMillis = millis();
// Check if button was pressed or released.
if(debouncer.update()){
// Pressed.
if(debouncer.read() == 0){
if(buttonState == 1){
buttonState = 0;
buttonPressedTime = currentMillis;
}
}
// Released.
else{
buttonState = 1;
}
}
// Turn the LED ON or OFF.
if(buttonState == 1){
if(currentMillis - buttonPressedTime >= 3500){
digitalWrite(ledPin, LOW);
buttonState = 0;
}
else if(currentMillis - buttonPressedTime >= 3000){
digitalWrite(ledPin, HIGH);
//lampu();
}
}
}
void lampu(){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}