Advertisement
Guest User

versao com estado

a guest
Jan 8th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1.  
  2. {
  3.   pinMode(13,OUTPUT); // LED output
  4.   pinMode(2,INPUT); // Button input
  5. }
  6.  
  7. void loop()
  8. {
  9.   static unsigned char ledState = LOW;
  10.   static unsigned char buttonState = LOW;
  11.   static unsigned char lastButtonState = LOW;
  12.   static unsigned long ledCameOn = 0;
  13.  
  14.   // If the LED has been on for at least 5 seconds then turn it off.
  15.   if(ledState == HIGH)
  16.   {
  17.     if(millis()-ledCameOn > 5000)
  18.     {
  19.       digitalWrite(13,LOW);
  20.       ledState = LOW;
  21.     }
  22.   }
  23.  
  24.   // If the button's state has changed, then turn the LED on IF it is not on already.
  25.   buttonState = digitalRead(2);
  26.   if(buttonState != lastButtonState)
  27.   {
  28.     lastButtonState = buttonState;
  29.     if((buttonState == HIGH) && (ledState == LOW))
  30.     {
  31.       digitalWrite(13,HIGH);
  32.       ledState = HIGH;
  33.       ledCameOne = millis();
  34.     }
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement