Advertisement
mikroavr

blink

May 26th, 2023
1,489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // constants won't change. Used here to set a pin number:
  2. const int ledPin =  13;// the number of the LED pin
  3.  
  4. // Variables will change:
  5. int ledState = LOW;             // ledState used to set the LED
  6.  
  7. // Generally, you should use "unsigned long" for variables that hold time
  8. // The value will quickly become too large for an int to store
  9. unsigned long previousMillis = 0;        // will store last time LED was updated
  10.  
  11. // constants won't change:
  12. const long interval = 1000;           // interval at which to blink (milliseconds)
  13.  
  14. void setup() {
  15.   // set the digital pin as output:
  16.   pinMode(ledPin, OUTPUT);
  17. }
  18.  
  19. void loop() {
  20.   // here is where you'd put code that needs to be running all the time.
  21.  
  22.   // check to see if it's time to blink the LED; that is, if the difference
  23.   // between the current time and last time you blinked the LED is bigger than
  24.   // the interval at which you want to blink the LED.
  25.   unsigned long currentMillis = millis();
  26.  
  27.   if (currentMillis - previousMillis >= interval) {
  28.     // save the last time you blinked the LED
  29.     previousMillis = currentMillis;
  30.  
  31.     // if the LED is off turn it on and vice-versa:
  32.     if (ledState == LOW) {
  33.       ledState = HIGH;
  34.     } else {
  35.       ledState = LOW;
  36.     }
  37.  
  38.     // set the LED with the ledState of the variable:
  39.     digitalWrite(ledPin, ledState);
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement