★ 아두이노

아두이노 호환 보드 & MAX7219CNG #3

ToolBOX01 2025. 9. 22. 08:01
반응형

▣ 8개의 행이 함께 순차적으로 이동하면서 깜빡이기

#include "LedControl.h"

// LedControl 객체 생성: (DIN 핀, CLK 핀, CS 핀, 디바이스 수)
LedControl lc = LedControl(12, 11, 10, 1);

// 행 번호 배열
const int rows[] = {0, 1, 2, 3, 4, 5, 6, 7};
const int num_rows = sizeof(rows) / sizeof(rows[0]);

// 각 행의 상태를 저장할 배열
int prev_cols[num_rows] = {0};
int current_cols[num_rows] = {0};

void setup() {
  lc.shutdown(0, false);  // 디스플레이 켜기
  lc.setIntensity(0, 8);  // 밝기 설정 (0~15, 8은 중간 밝기)
  lc.clearDisplay(0);       // 화면 지우기
}

void loop() {
  for (int i = 0; i < num_rows; i++) {
    // 이전 LED 끄기
    lc.setLed(0, rows[i], prev_cols[i], false);

    // 현재 LED 켜기
    lc.setLed(0, rows[i], current_cols[i], true);

    // 상태 업데이트
    prev_cols[i] = current_cols[i];
    current_cols[i] = (current_cols[i] + 1) % 8; // 8열을 순환
  }

  delay(200); // 200ms 대기
}

 

▣ 8개의 행이  순차적으로 이동하면서 깜빡이기

#include "LedControl.h"

// LedControl 객체 생성: (DIN 핀, CLK 핀, CS 핀, 디바이스 수)
LedControl lc = LedControl(12, 11, 10, 1);

// 행 번호
const int num_rows = 8;
const int rows[num_rows] = {0, 1, 2, 3, 4, 5, 6, 7};

// 각 행의 상태를 저장할 배열
int prev_cols[num_rows] = {0};
int current_cols[num_rows] = {0};

void setup() {
  lc.shutdown(0, false);  // 디스플레이 켜기
  lc.setIntensity(0, 8);  // 밝기 설정 (0~15, 8은 중간 밝기)
  lc.clearDisplay(0);       // 화면 지우기
}

void loop() {
  // 현재 활성화된 행을 추적
  static int active_row = 0;
  static unsigned long last_blink_time = 0;
  const unsigned long blink_interval = 200; // 200ms마다 깜박임

  unsigned long current_time = millis();

  if (current_time - last_blink_time >= blink_interval) {
    // 이전 LED 끄기
    lc.setLed(0, rows[active_row], prev_cols[active_row], false);

    // 상태 업데이트
    prev_cols[active_row] = current_cols[active_row];
    current_cols[active_row] = (current_cols[active_row] + 1) % 8; // 8열을 순환

    // 다음 행으로 이동 (다음 깜박임 시작)
    active_row = (active_row + 1) % num_rows;
    
    // 다음 LED 켜기 (새로운 행의 첫 번째 LED)
    lc.setLed(0, rows[active_row], current_cols[active_row], true);

    last_blink_time = current_time;
  }
}

 

▣ 대각선 방향으로, 8개의 행이  순차적으로 이동하면서 깜빡이기

#include "LedControl.h"

// LedControl 객체 생성: (DIN 핀, CLK 핀, CS 핀, 디바이스 수)
LedControl lc = LedControl(12, 11, 10, 1);

// 열 번호
const int num_cols = 8;
const int cols[num_cols] = {0, 1, 2, 3, 4, 5, 6, 7};

// 각 열의 현재 행 위치
int current_rows[num_cols] = {0};

void setup() {
  lc.shutdown(0, false);  // 디스플레이 켜기
  lc.setIntensity(0, 8);  // 밝기 설정 (0~15, 8은 중간 밝기)
  lc.clearDisplay(0);       // 화면 지우기
}

void loop() {
  static unsigned long last_blink_time = 0;
  const unsigned long blink_interval = 200; // 200ms마다 깜박임

  unsigned long current_time = millis();

  if (current_time - last_blink_time >= blink_interval) {
    // 이전 LED 끄기
    for (int i = 0; i < num_cols; i++) {
      lc.setLed(0, current_rows[i], cols[i], false);
    }
    
    // 1열의 깜박임 위치 업데이트
    current_rows[0] = (current_rows[0] + 1) % 8;

    // 1열의 현재 위치에 따라 다른 열들의 위치 설정
    for (int i = 1; i < num_cols; i++) {
      current_rows[i] = (current_rows[0] + i) % 8;
    }
    
    // 현재 LED 켜기
    for (int i = 0; i < num_cols; i++) {
      lc.setLed(0, current_rows[i], cols[i], true);
    }

    last_blink_time = current_time;
  }
}

반응형