본문 바로가기
푸닥거리

아두이노 플랫폼을 활용한 코딩 기초(릴리패드)-4

by ┌(  ̄∇ ̄)┘™ 2022. 4. 2.
728x90

아두이노 릴리패드(RallaPad) 코딩 기초 시리즈 4번째 편입니다. 이번에는 아두이노 프로그램의 기본 골격디지털 출력의 핵심 함수들을 정리합니다. 아두이노 스케치는 반드시 setup()loop() 두 함수로 이루어집니다.

1. 프로그램의 두 축 — setup() vs loop()

  • setup() — 리셋하거나 전원을 넣을 때 딱 한 번만 실행. 주로 핀 모드 설정 같은 하드웨어 초기 세팅에 사용
  • loop() — setup 이후 무한 반복 실행. 실제 동작 로직(알고리즘)을 여기에 작성
  • 두 함수 모두 안에서 지역 변수를 선언할 수 있고, 함수 바깥에 선언한 것은 전역 변수

2. 디지털 출력 핵심 함수

LED·부저처럼 켜고 끄는 부품을 제어하는 세 함수입니다. 디지털은 0(끄다, LOW)과 1(켜다, HIGH) 두 값만 다룹니다.

  • pinMode(핀, OUTPUT/INPUT) — 핀을 출력/입력 모드로 설정 (setup에서)
  • digitalWrite(핀, HIGH/LOW) — 출력 핀에 켜기(HIGH)/끄기(LOW) 신호
  • digitalRead(핀) — 입력 핀의 값을 읽기

3. 기본 예제 — LED 점멸

13번 핀의 LED를 0.1초 간격으로 깜빡이는 예제입니다. 릴리패드에서는 핀 번호만 2번 등으로 바꾸면 다른 LED를 제어할 수 있습니다.

// setup(): 리셋/전원 인가 시 한 번만 실행
void setup() {
  pinMode(13, OUTPUT);   // 13번 핀을 출력 모드로 설정
}

// loop(): 무한 반복 실행
void loop() {
  digitalWrite(13, HIGH);   // LED 켜기 (HIGH = 전압 공급)
  delay(100);               // 0.1초 대기
  digitalWrite(13, LOW);    // LED 끄기 (LOW)
  delay(100);               // 0.1초 대기
}

4. delay() — 시간 제어

delay(ms)는 지정한 밀리초(ms)만큼 프로그램을 멈춥니다. 1초 = 1000ms입니다.

  • delay(1000) = 1초
  • delay(100) = 0.1초
  • delay 값을 조절해 깜빡임 속도나 신호 지속 시간을 바꿉니다.

참고 — 더 자세한 문법은 아두이노 공식 레퍼런스에서 확인할 수 있습니다.

 

Arduino Reference - Arduino Reference

Language Reference Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure.

www.arduino.cc

 

pinMode() - Arduino Reference

Example Code The code makes the digital pin 13 OUTPUT and Toggles it HIGH and LOW void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a sec

www.arduino.cc

 

digitalWrite() - Arduino Reference

Example Code The code makes the digital pin 13 an OUTPUT and toggles it by alternating between HIGH and LOW at one second pace. void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the dig

www.arduino.cc

 

delay() - Arduino Reference

Description Pauses the program for the amount of time (in milliseconds) specified as parameter. (There are 1000 milliseconds in a second.) Syntax Parameters ms: the number of milliseconds to pause. Allowed data types: unsigned long. Returns

www.arduino.cc

728x90

댓글