Created
July 6, 2016 19:58
-
-
Save dirkjanfaber/2d074802879b1f09b415b5b67800c9c0 to your computer and use it in GitHub Desktop.
blink a led on either an arduino or an esp8266 (with platform.io)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Blink | |
Turns on an LED on for one second, then off for one second, repeatedly. | |
Most Arduinos have an on-board LED you can control. On the Uno and | |
Leonardo, it is attached to digital pin 13. If you're unsure what | |
pin the on-board LED is connected to on your Arduino model, check | |
the documentation at http://arduino.cc | |
This example code is in the public domain. | |
modified 8 May 2014 | |
by Scott Fitzgerald | |
*/ | |
// constants won't change. Used here to set a pin number : | |
#ifdef ARDUINO_ARCH_AVR | |
const int ledPin = 3; // the number of the LED pin | |
#endif | |
#ifdef ARDUINO_ARCH_ESP8266 | |
const int ledPin = D3; | |
#endif | |
// Variables will change : | |
int ledState = LOW; // ledState used to set the LED | |
// Generally, you should use "unsigned long" for variables that hold time | |
// The value will quickly become too large for an int to store | |
unsigned long previousMillis = 0; // will store last time LED was updated | |
// constants won't change : | |
const long interval = 500; // interval at which to blink (milliseconds) | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// initialize digital pin 13 as an output. | |
pinMode(ledPin, OUTPUT); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= interval) { | |
// save the last time you blinked the LED | |
previousMillis = currentMillis; | |
// if the LED is off turn it on and vice-versa: | |
if (ledState == LOW) { | |
ledState = HIGH; | |
} else { | |
ledState = LOW; | |
} | |
// set the LED with the ledState of the variable: | |
digitalWrite(ledPin, ledState); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment