0% found this document useful (0 votes)
32 views

Code 2

The document declares arrays for LED pins and button pins. It initializes LED states and intervals for blinking LEDs. It sets pin modes in setup and checks button states in loop to toggle or turn on LEDs.

Uploaded by

Chúc Văn Kiên
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Code 2

The document declares arrays for LED pins and button pins. It initializes LED states and intervals for blinking LEDs. It sets pin modes in setup and checks button states in loop to toggle or turn on LEDs.

Uploaded by

Chúc Văn Kiên
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Khai báo các chân kết nối đèn LED và nút nhấn

int ledPins[] = {10, 11, 12};


int buttonPins[] = {2, 7};

int ledStates[] = {HIGH, HIGH, HIGH};

// Thời gian chu kỳ các đèn


const unsigned long intervals[] = {10000, 5000, 12000};
unsigned long previousMillis[3];

void setup() {
// Đặt chế độ pin
for (int i = 0; i < sizeof(ledPins)/sizeof(ledPins[0]); i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], HIGH);
}

for (int i = 0; i < sizeof(buttonPins)/sizeof(buttonPins[0]); i++) {


pinMode(buttonPins[i], INPUT_PULLUP);
}
}

void loop() {
int buttonState1 = digitalRead(buttonPins[0]);
int buttonState2 = digitalRead(buttonPins[1]);

if (buttonState1 == LOW) {
unsigned long currentMillis = millis();

for (int i = 0; i < sizeof(ledPins)/sizeof(ledPins[0]); i++) {


if (currentMillis - previousMillis[i] >= intervals[i]) {
previousMillis[i] = currentMillis;

ledStates[i] = !ledStates[i];
digitalWrite(ledPins[i], ledStates[i]);
}
}
}

if (buttonState2 == LOW) {
for (int i = 0; i < sizeof(ledPins)/sizeof(ledPins[0]); i++) {
ledStates[i] = HIGH;
digitalWrite(ledPins[i], ledStates[i]);
}
}
}

You might also like