이젠 중간고사 문제까지...

중간고사 5번 문제 블로그 미션 11월 6일까지, +10점 (보너스 최대 5점)

제목이 내용 그 자체네.. 내용엔 23시 59분까지 추가됨

3번 : 가변조항으로 숫자 조절

4번 : 버튼으로 카운터 동작 제어


우선! 중간고사 때 풀었던 3번

// 순서 지켜줄 것! (Graphics가 Matrix보다 나중에 만들어짐)
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"

#include <TaskScheduler.h>

ArduinoLEDMatrix matrix;
char buf[4];	// 인터넷에서 찾은 예제 그대로 썼던;;

const int LED2 =  9;
const int LED1 = 10;
const int LED0 = 11;
int n = 0;

void led_blink();
Scheduler runner;
Task rdBitLED(950, TASK_FOREVER, &led_blink);
// 맨 뒤에 True가 없으므로

void setup() {
  pinMode( LED2, OUTPUT );
  pinMode( LED1, OUTPUT );
  pinMode( LED0, OUTPUT );
  matrix.begin();
  matrix.beginDraw();
  matrix.stroke(0xFFFFFFFF);
  matrix.textFont(Font_5x7);
  matrix.beginText(-1, 1, 0xFFFFFF);
  // 이렇게 써줘야..
  runner.addTask(rdBitLED);
  rdBitLED.enable();
}


void loop() {
  n = analogRead(A1);
  n = map(n, 0, 1023, 0, 7);
  led_blink();
  //runner.execute();
  // 하지만 이 문제에선 필요치 않았다.
}

// 이 함수는 예전 실습 코드에서 따온 부분!
void drawInt(int n){
    sprintf(buf, "%2d", n);
    matrix.println(buf);
    matrix.endText(); 
}

void led_blink() {
  drawInt(n);
  digitalWrite( LED2, n & 4 );
  digitalWrite( LED1, n & 2 );
  digitalWrite( LED0, n & 1 );
  n = (n+1) % 8;
}

그리고 4번(은 못 풀어서 나중에 풀음)

#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include <TaskScheduler.h>

ArduinoLEDMatrix matrix;
char buf[2];

const int LED2 =  9;
const int LED1 = 10;
const int LED0 = 11;
const int but = 2;
int n = 0;

// Toggle 구현에 필요한 변수! 
bool act = false;
bool cur_but;
bool las_but = LOW;

void led_blink();
Scheduler runner;
Task rdBitLED(1000, TASK_FOREVER, &led_blink);
// STOP은 Task 필요X (중간고사 때 썼어서)

void setup() {
  pinMode( LED2, OUTPUT );
  pinMode( LED1, OUTPUT );
  pinMode( LED0, OUTPUT );
  pinMode(but, INPUT_PULLUP);
  matrix.begin();
  matrix.beginDraw();
  matrix.stroke(0xFFFFFFFF);
  matrix.textFont(Font_5x7);
  matrix.beginText(-1, 1, 0xFFFFFF);
  
  runner.addTask(rdBitLED);
  rdBitLED.enable();
}


void loop() {
  // Toggle Switch
  cur_but = digitalRead(but);
  if (cur_but == LOW && las_but == HIGH) { 
    act = (act) ? false : true;
    if(act){
      rdBitLED.disable();
    }
    else if(!act){
      rdBitLED.enable();
    }
  }
  las_but = cur_but;
  delay(100);         // Debouncing

  runner.execute();
}

void drawInt(int n){
    sprintf(buf, "%2d", n);
    matrix.println(buf);
    matrix.endText(); 
}

void led_blink() {
  drawInt(n);
  digitalWrite( LED2, n & 4 );
  digitalWrite( LED1, n & 2 );
  digitalWrite( LED0, n & 1 );
  n = (n+1) % 8;
}

4번 코드에 3번 코드 일부를 얹히면 되는 줄 알았는데..

#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include <TaskScheduler.h>

ArduinoLEDMatrix matrix;
char buf[2];

const int LED2 =  9;
const int LED1 = 10;
const int LED0 = 11;
const int but = 2;
int n = 0;

bool act = false;
bool cur_but;
bool las_but = LOW;

void led_blink();
Scheduler runner;
Task rdBitLED(1000, TASK_FOREVER, &led_blink);


void setup() {
  pinMode( LED2, OUTPUT );
  pinMode( LED1, OUTPUT );
  pinMode( LED0, OUTPUT );
  pinMode(but, INPUT_PULLUP);
  matrix.begin();
  matrix.beginDraw();
  matrix.stroke(0xFFFFFFFF);
  matrix.textFont(Font_5x7);
  matrix.beginText(-1, 1, 0xFFFFFF);
  
  runner.addTask(rdBitLED);
  rdBitLED.enable();
}


void loop() {
  // Toggle Switch
  cur_but = digitalRead(but);
  if (cur_but == LOW && las_but == HIGH) { 
    act = (act) ? false : true;
    if(act){
      rdBitLED.disable();
      // 이렇게!라고 해봤자 별거 아니긴 하다;;
      n = analogRead(A1);
      n = map(n, 0, 1023, 0, 7);
      led_blink();    
    }
    else if(!act){
      rdBitLED.enable();
    }
  }
  las_but = cur_but;
  delay(100);         // Debouncing

  runner.execute();
}

void drawInt(int n){
    sprintf(buf, "%2d", n);
    matrix.println(buf);
    matrix.endText(); 
}

void led_blink() {
  drawInt(n);
  digitalWrite( LED2, n & 4 );
  digitalWrite( LED1, n & 2 );
  digitalWrite( LED0, n & 1 );
  n = (n+1) % 8;
}

이거... (act) 가변저항 값이 바뀌는 게 안 보인다;;
    (!act) 버튼을 누르면 그냥 멈췄던 수부터 다시 시작한다..

(act) 다시 버튼을 누르면 전에 바꿨던 값이 나오고 [바뀌는 건 또 안 보임]
(!act) 그리고 다시 버튼을 누르면 바뀐 수의 다음 수부터 시작됨;;;;


             < 최종 코드 >

그럼 수정 + 최적화(?!) 시켜볼까

#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include <TaskScheduler.h>

#define TIME 1000	// 코드에 숫자는 NO! (타인은 왜 그 수를 썼는지 모르기에!)

ArduinoLEDMatrix matrix;
char buf[2];

const int LED2 =  9;
const int LED1 = 10;
const int LED0 = 11;
const int but = 2;

int n = 0;

bool act = false;
bool cur_but;
bool las_but = LOW;

void led_blink();
Scheduler runner;
// 이렇게 해주면
Task rdBitLED(TIME, TASK_FOREVER, led_blink, &runner, true);

void setup() {
  pinMode( LED2, OUTPUT );
  pinMode( LED1, OUTPUT );
  pinMode( LED0, OUTPUT );
  pinMode(but, INPUT_PULLUP);
  matrix.begin();
  matrix.beginDraw();
  matrix.stroke(0xFFFFFFFF);
  matrix.textFont(Font_5x7);
  matrix.beginText(-1, 1, 0xFFFFFF);
  // addTask와 enable이 필요없어짐!!
}


void loop() {

  cur_but = digitalRead(but);
  if (cur_but == LOW && las_but == HIGH) { 
    act = (act) ? false : true;
    if(act){
      rdBitLED.disable();
    }
    else if(!act){
      // TIME 쉬고 rdBitLED 시작!
      rdBitLED.restartDelayed(TIME);
    }
  }
  las_but = cur_but;
  delay(TIME * 0.1);

  // act일 때 계속 보여줘야 하기 때문에 위의 if문과 독립된 if문으로!
  if(act){
    n = analogRead(A1);
    n = map(n, 0, 1023, 0, 7);
    led_blink();
  }

  runner.execute();
}

void drawInt(int n){
    sprintf(buf, "%2d", n);
    matrix.println(buf);
    matrix.endText(); 
}

void led_blink() {
  drawInt(n);
  digitalWrite( LED2, n & 4 );
  digitalWrite( LED1, n & 2 );
  digitalWrite( LED0, n & 1 );
  n = (n+1) % 8;
}

        구현 영상

    ▲ 위 이미지를 클릭하세요!

0개의 댓글