ESP32_8LEDS_DANH — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by danh-pham2526.

Components

Sketch code

// ESP32 Blink LED
// Blinks the built-in LED (GPIO2) and an external LED (GPIO4)
// Requires arduino-esp32 2.0.17 (IDF 4.4.x) — see docs/ESP32_EMULATION.md

#define LED_D32 32
#define LED_D33 33
#define LED_D25 25
#define LED_D26 26

#define LED_D4  4
#define LED_D18 18
#define LED_D19 19
#define LED_D21 21

const int leds[] = {LED_D32, LED_D33, LED_D25, LED_D26, LED_D4, LED_D18, LED_D19, LED_D21};
const int ledNum = 8;

void ledTask(void *parameter){
  int step = 0;
  
  while(1){
    for (int i = 0; i < ledNum; ++i){
      digitalWrite(leds[i], LOW);
    }

    switch(step){
      // HIỆU ỨNG 1: ĐI THEO NGƯỢC VÀ CÙNG CHIỀU KIM ĐỒNG HỒ
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        digitalWrite(leds[step], HIGH); 
        break;
      case 8: 
        break;
      case 9: 
      case 10: 
      case 11: 
      case 12: 
      case 13: 
      case 14: 
      case 15: 
      case 16:
        digitalWrite(leds[16 - step], HIGH); 
        break;
      case 17: 
        break;

      // HIỆU ỨNG 2: SÁNG 2 BÊN ĐỐI DIỆN CÙNG LÚC 
      case 18:
        digitalWrite(leds[3], HIGH); 
        digitalWrite(leds[4], HIGH); 
        break;
      case 19:
        digitalWrite(leds[2], HIGH); 
        digitalWrite(leds[5], HIGH); 
        break;
      case 20:
        digitalWrite(leds[1], HIGH); 
        digitalWrite(leds[6], HIGH);
        break;
      case 21:
        digitalWrite(leds[0], HIGH); 
        digitalWrite(leds[7], HIGH); 
        break;
      case 22:
        digitalWrite(leds[1], HIGH); 
        digitalWrite(leds[6], HIGH); 
        break;
      case 23:
        digitalWrite(leds[2], HIGH); 
        digitalWrite(leds[5], HIGH); 
        break;
      case 24:
        digitalWrite(leds[3], HIGH); 
        digitalWrite(leds[4], HIGH); 
        break;
      case 25:
        break;

      // HIỆU ỨNG 3: SÁNG TOÀN BỘ ĐÈN 2 LẦN
      case 26:
      case 28:
        for (int i = 0; i < ledNum; ++i){
          digitalWrite(leds[i], HIGH);
        }
        break;
      case 27:
      case 29:
        break;
    }

    step++;
    if(step > 29){
      step = 0; 
    }

    vTaskDelay(pdMS_TO_TICKS(500));
  }
}

void setup() {
  for (int i = 0; i < ledNum; ++i){
    pinMode(leds[i], OUTPUT);
  }

  xTaskCreate(ledTask, "8leds", 2048, NULL, 1, NULL);
}

void loop() {
  // Trống
}