SHARE
TWEET

Untitled

a guest Oct 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    If button is pressed for 3 seconds, one LED should light up.
  3.    If button is pressed for 6 seconds, another LED should light up.
  4.    If a button is pressed and held for 6 seconds and a second button is pressed, make the LED do some special behavior.
  5.    Explore combining behaviors.
  6.    How many different LED behaviors can you get out of just two switches and measuring how long they're pressed?
  7. */
  8.  
  9. #define LED1 5
  10. #define LED2 6
  11. #define BTN1 2
  12. #define BTN2 3
  13.  
  14. long time;
  15. bool ledState = false;
  16.  
  17. void setup() {
  18.   // pin definitions
  19.   pinMode(LED1, OUTPUT);
  20.   pinMode(LED2, OUTPUT);
  21.   pinMode(BTN1, INPUT_PULLUP);
  22.   pinMode(BTN2, INPUT_PULLUP);
  23.  
  24.   digitalWrite(LED1, LOW);
  25.   digitalWrite(LED2, LOW);
  26.  
  27.   time = millis();
  28.  
  29.   Serial.begin(115200);
  30. }
  31.  
  32. void loop() {
  33.   if (digitalRead(BTN1) == 1) {
  34.     Serial.println("BTN1 is not pressed");
  35.     time = millis();
  36.     digitalWrite(LED1, LOW);
  37.     digitalWrite(LED2, LOW);
  38.   } else {
  39.     Serial.println("BTN1 is pressed");
  40.   }
  41.  
  42.   if (millis() - time > 3000) {
  43.     Serial.println("BTN1 is pressed for 3s");
  44.     digitalWrite(LED1, HIGH);
  45.   }
  46.  
  47.   if (millis() - time > 6000) {
  48.     Serial.println("BTN1 is pressed for 6s");
  49.     digitalWrite(LED2, HIGH);
  50.     if (digitalRead(BTN2) == 0) {
  51.       Serial.println("BTN2 is pressed");
  52.       blinky();
  53.     }
  54.   }
  55. }
  56.  
  57. void blinky(){
  58.   if(ledState){
  59.     digitalWrite(LED1, HIGH);
  60.     digitalWrite(LED2, LOW);
  61.     delay(500);
  62.   } else {
  63.     digitalWrite(LED2, LOW);
  64.     digitalWrite(LED2, HIGH);
  65.     delay(500);
  66.   }
  67.   ledState = !ledState;
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top