아두이노 - 코드 구조 | Arduino - Code Structure

준비물

1×Arduino Uno Amazon
1×USB 2.0 cable type A/B 쿠팡 | Amazon
1×(Optional) 9V Power Adapter for Arduino Amazon
1×(Recommended) Screw Terminal Block Shield for Arduino Uno 쿠팡 | Amazon
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

기본 구조

아두이노 코드(아두이노 스케치라고도 함)는 주로 설정 코드와 반복 코드 두 가지 주요 부분으로 구성됩니다.

설정 코드

  • setup() 함수 내의 코드입니다.
  • 전원이 켜진 직후나 리셋된 직후 실행됩니다.
  • 단 한 번만 실행됩니다.
  • 변수 초기화, 핀 모드 설정, 라이브러리 사용 시작 등을 위해 사용됩니다.

루프 코드

  • loop() 함수 안의 코드입니다.
  • 설정 코드 바로 후에 실행됩니다.
  • 반복적으로 (무한히) 실행됩니다.
  • 애플리케이션의 주요 작업을 수행하는 데 사용됩니다.

예시

void setup() { // 여기에 설정 코드를 넣으세요, 한 번만 실행됩니다: Serial.begin(9600); Serial.println("This is setup code"); } void loop() { // 여기에 메인 코드를 넣으세요, 반복하여 실행됩니다: Serial.println("This is loop code"); delay(1000); }

사용 방법

  • 위의 코드를 복사하고 Arduino IDE로 열기
  • Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino에 업로드하기
  • 시리얼 모니터 열기
  • 시리얼 모니터에서 출력 확인하기
COM6
Send
This is setup code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

보시다시피, “이것은 설정 코드입니다”는 한 번 출력되지만, “이것은 반복 코드입니다”는 여러 번 출력됩니다. 이는 설정 코드가 한 번 실행되고, 반복 코드는 반복해서 실행된다는 것을 의미합니다. 설정 코드가 먼저 실행됩니다.

※ NOTE THAT:

setup()loop() 함수는 Arduino 코드에 있어야 합니다. 그렇지 않으면 오류가 발생합니다.

선택적 부품

설정 및 루프 코드 외에도, Arduino 스케치에는 다음과 같은 부분들이 포함될 수 있습니다:

  • Block comment: usually used to write some information about the author, the wiring instruction, the license ... Arduino will ignore this part.
  • Libraries inclusion: is used to include libraries into the sketch.
  • Constant definition: used to define constant
  • Global variables declaration
/* * 이 Arduino 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-code-structure */ #include <Servo.h> #include <LiquidCrystal.h> #define MAX_COUNT 180 Servo servo; LiquidCrystal lcd(3, 4, 5, 6, 7, 8); int loop_count = 0; void setup() { Serial.begin(9600); lcd.begin(16, 2); servo.attach(9); Serial.println("This is setup code"); // "이것은 설정 코드입니다" } void loop() { loop_count++; Serial.print("This is loop code, count: "); // "이것은 루프 코드, 카운트: " Serial.println(loop_count); lcd.print("Hello World!"); servo.write(loop_count); if(loop_count >= MAX_COUNT) loop_count = 0; delay(1000); }

사용 방법

  • 위의 코드를 복사하고 Arduino IDE로 열기
  • Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino에 업로드하기
  • 시리얼 모니터 열기
  • 시리얼 모니터에서 출력을 확인하기
COM6
Send
This is setup code This is loop code, count: 1 This is loop code, count: 2 This is loop code, count: 3 This is loop code, count: 4 This is loop code, count: 5 This is loop code, count: 6 This is loop code, count: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

우리는 지금 코드를 줄마다 이해할 필요가 없습니다. 우리는 단지 코드 구조에 대해 알아야 합니다. 줄마다의 코드는 다음 튜토리얼에서 설명될 것입니다.

코멘트

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!