Skip to content

Instantly share code, notes, and snippets.

@huytd
Created June 1, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/ee07fab2e55dc81875d0 to your computer and use it in GitHub Desktop.
Save huytd/ee07fab2e55dc81875d0 to your computer and use it in GitHub Desktop.
// khai báo pin 13
const int ledPin = 13;
// Các giá trị sẽ thay đổi
int ledState = LOW; // ledState - trạng thái của đèn LED
long previousMillis = 0; // thời gian của lần vừa xử lý
long interval = 1000; // biên độ chênh lệch thời gian - đây sẽ là thời gian cần delay
void setup() {
// Thiết lập chế độ output cho pin 13
pinMode(ledPin, OUTPUT);
}
void loop()
{
// lấy thời gian hiện tại
unsigned long currentMillis = millis();
// tính khoảng chênh lệch giữa thời gian hiện tại và thời gian của lần xử lý trước đó
if(currentMillis - previousMillis > interval) {
// lưu lại thời gian của vòng lặp vừa mới xử lý
previousMillis = currentMillis;
// bật tắt đèn ngược với trạng thái hiện tại của đèn
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// ghi tín hiệu ra pin 13
digitalWrite(ledPin, ledState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment