TIOBE Index Dongyang Mirae University 5 LOGO prepared
프로그래밍 언어 순위 : TIOBE Index Dongyang Mirae University 센서활용프로그래밍 5 LOGO prepared by Choon Woo Kwon
프로그래밍 개발 LOGO § 프로그램 개발 과정 ü 개발 과정 5단계 • 프로그램 구상 ⇒ 소스 편집 ⇒ 컴파일(compile) ⇒ 링크(link) ⇒ 실행(execute) 소스 편집 (source edit) #include <stdio. h> 컴파일 (compile) int main() { … } 프로그램 구상 컴파일 (compile) int sub 1() { … } int sub 2() { … } 소스파일 sub 2. c Dongyang Mirae University 라이브러리 파일 (library file) 01111000 10111101 10100101 + 링크 (link) 컴파일 (compile) 010101101 10001101 v 빌드(build) = 컴파일 과정+ 링크 과정 v 통합개발환경 IDE (Integrated Development Environment) § § 목적파일 sub 2. obj 센서활용프로그래밍 실행 (execute) 실행파일 (execute file) 목적파일 sub 1. obj 소스파일 sub 1. c #include <stdio. h> 01010100 10010101 11100101 목적파일 main. obj (object file) 소스파일 main. c (source file) #include <stdio. h> ※ library file : 자주 사용하는 프로그램을 미리 컴파일한 후에 저장되는 파일 6 편집기(editor) 컴파일러(compiler) 링커(linker) 디버거(debugger) prepared by Choon Woo Kwon
Arduino Software (IDE) LOGO v Arduino Software (IDE) § Sketch : Arduino Software (IDE)를 사용하여 작성한 프로그램 ü Text editor로 작성 ü 저장 파일 확장지 : . ino § 도구모음 확인(verify) : compile + check errors 업로드(upload) : compile + upload to board 새파일(new) : 새 sketch 생성 열기(open) : sketchbook에 있는 모든 sketch 가운데 하나를 현재 창에 읽어 들임 ü 저장(save) : sketch 저장 ü 시리얼 모니터(serial monitor) : 직렬 통신을 모니터링 ü ü § 메뉴 ü 파일(File), 편집(Edit), 스케치(Sketch) 도구(Tools), 도움말(Help) Dongyang Mirae University 센서활용프로그래밍 7 prepared by Choon Woo Kwon
Arduino Software (IDE) LOGO § 업로드(Uploading) ü 업로드 설정 : 도구 > 보드, 도구 > 포트 • 보드 설정 : 연결된 아두이노 보드를 선택 • 포트 설정(Mac) /dev/tty. usbmodem 241 (for an Uno or Mega 2560 or Leonardo) /dev/tty. usbserial-1 B 1 (for a Duemilanove or earlier USB board) /dev/tty. USA 19 QW 1 b 1 P 1. 1 (for a serial board connected with a Keyspan USB-to-Serial adapter) • 포트 설정(Windows) COM 1, COM 2 (for a serial board) COM 4, COM 5, COM 7, or higher (for a USB board) ※ 윈도 장치관리자의 포트 부분에서 확인 • 포트 설정(Linux) /dev/tty. ACMx , /dev/tty. USBx ü Arduino bootloader : Sketch 업로드 될 때 함께 사용됨 • 가장 최근에 업로드 된 Sketch가 수행 • 보드 리셋(reset) 시 수 초(a few second) 후 bootloader가 활성화(active) 됨 Dongyang Mirae University 센서활용프로그래밍 9 prepared by Choon Woo Kwon
Arduino Software (IDE) LOGO § 라이브러리(Libraries) ü Sketch에 추가 기능을 제공 ü #include 명령 사용 • Sketch의 상단 삽입한 Sketch와 함께 compile • 스케치 > Include Library > (라이브러리 선택) • (예) Dongyang Mirae University 센서활용프로그래밍 10 prepared by Choon Woo Kwon
Arduino Software (IDE) LOGO § 시리얼 모니터 (Serial Monitor) ü 아두이노(혹은 Genuino board) 보드에서 보내 온 직렬 데이터를 표시 ü USB 또는 serial board 사용하여 통신 ü Baud rate 설정 • Sketch에서 Serial. begin 명령에서 지정한 속도와 동일하게 설정 Dongyang Mirae University 센서활용프로그래밍 11 prepared by Choon Woo Kwon
아두이노 프로그램 - 구조 LOGO v Arduino Program의 구성 : 3개 부분 § 구조(Structure) § 값(Values) : 변수(variables), 상수(constants) § 함수(Functions ) v Structure int button. Pin = 3; § 프로그램의 구성 ü C 언어의 main() 대신 setup()과 loop()로 구성 ü setup() • Sketch가 시작할 때 한번 실행 • 변수(variables), 핀 모드(pin mode)를 초기화 • 라이브러리(Library) 사용 시작 ü loop() • setup()이 초기화 및 초기값 설정 후 수행 • loop() 내 명령문은 무한 반복 실행 void setup() { Serial. begin(9600); pin. Mode(button. Pin, INPUT); } void loop() { if (digital. Read(button. Pin) == HIGH) Serial. write('H'); else Serial. write('L'); delay(1000); } Dongyang Mirae University 센서활용프로그래밍 12 prepared by Choon Woo Kwon
아두이노 프로그램 – 구성 요소 v 구성 요소 Ø 예약어(reserved word) : 키워드(keyword) ü Compiler가 특별한 의미로 인식하는 단어 ü 변수로 사용 불가 ü setup, loop, for, while, do, if, switch, int, char, float 등 § 식별자(identifier) ü ü 변수명, 배열명, 함수명 등의 용도로 사용하는 단어 영문자, 숫자, 밑줄(_) 사용 대소문자 구별하여 사용 첫 글자는 영문자 또는 밑줄(_) • (예1) LED 1, led 1(LED 1과 다른 식별자) • (예2) dong_counter, _mydevice, , color_LED 5 § 문장(statement) ü 문장의 끝에는 ‘; ’(semicolon) 사용 § 블록(block) ü 여러 명령어를 묶어 하나의 단위로 처리 ü 중괄호(Curly Braces) ‘{‘와 ‘}’를 사용 § 주석문(comment) LOGO /* Liquid. Crystal Library - Blink */ // include the library code: #include <Liquid. Crystal. h> // initialize the library with the numbers of the interface pins Liquid. Crystal lcd(12, 11, 5, 4, 3, 2); void setup() { // set up the LCD's number of columns and rows: lcd. begin(16, 2); // Print a message to the LCD. lcd. print("hello, world!"); } void loop() { // Turn off the blinking cursor: lcd. no. Blink(); delay(3000); // Turn on the blinking cursor: lcd. blink(); delay(3000); } ü 한 줄 주석 : ‘//’를 사용 ü 여러 줄 주석 : ‘/*”와 ‘*/”을 사용 Dongyang Mirae University 센서활용프로그래밍 13 prepared by Choon Woo Kwon
아두이노 프로그램 – 연산자(operators) LOGO v 연산자(operators) ü Operator : 대상 자료에 대해 조작하는 부호 ü Operand : 연산 대상의 자료 § 산술연산자(Arithmetic Operators) = (assignment operator) - (subtraction) / (division) + (addition) * (multiplication) % (modulo) § 관계연산자(Comparison Operators) == (equal to) < (less than) <= (less than or equal to) != (not equal to) > (greater than) >= (greater than or equal to) § 논리연산자(Boolean Operators) && (and) ! (not) || (or) § 비트연산자(Bitwise Operators) & (bitwise and) ^ (bitwise xor) << (bitshift left) Dongyang Mirae University | (bitwise or) ~ (bitwise not) >> (bitshift right) 센서활용프로그래밍 14 prepared by Choon Woo Kwon
아두이노 프로그램 – 연산자(operators) LOGO § 복합 연산자(Compound Operators) ++ (increment) += (compound addition) *= (compound multiplication) %= (compound modulo) |= (compound bitwise or) -- (decrement) -= (compound subtraction) /= (compound division) &= (compound bitwise and) § 포인터 접근 연산자(Pointer Access Operators) * dereference operator Dongyang Mirae University & reference operator 센서활용프로그래밍 15 prepared by Choon Woo Kwon
아두이노 프로그램 – 상수(constants) LOGO § 상수(constants 또는 literals) Ø ‘const’ 키워드(keyword)로 정의 : 값이 변하지 않는 고정된 값(‘read-only’) • 프로그램 수행 도중에는 값 변경 불가 • ‘#define’ 보다 선호됨 Ø ‘#define’ Statement : 프로그래머가 상수 값에 이름을 부여 • 컴파일 단계에서 부여된 이름 대신에 상수 값을 사용(전처리 지시자) § 아두이노 칩(chip)의 메모리 공간을 차지하지 않음 § 일반 변수와 구분하기 위해 이름은 주로 대문자 사용(예: PHI, BOM 등) • 세미콜론(semicolon ‘; ’) 사용 않음 <const 예시!!> const float pi = 3. 14; pi = 7; (error) <#define 사용 Error 예시!!> #define led. Pin 3; ( error) #define led. Pin # 3; (error) #define led. Pin = 3; (error) ü 정수 상수(integer constants) • 스케치에서 직접 사용되는 숫자 • 기본적인 숫자는 10진수 값 • U & L formatters § U/u : unsigned data format (예) 33 U § L/l : long data format (예) 100000 L, § UL/ul : unsigned long data format (예) 32767 ul Base Example Formatter 10 (decimal) 123 none 2 (binary) B 1111011 leading 'B' characters 0 -1 valid(0 to 255) 8 (octal) 0173 leading "0" characters 0 -7 valid 16 (hexadecimal) 0 x 7 B leading "0 x" characters 0 -9, A-F, a-f valid Dongyang Mirae University 센서활용프로그래밍 Comment 17 prepared by Choon Woo Kwon
아두이노 프로그램 – 상수(constants) LOGO ü 부동소수(실수) 상수(floating point constants) • 컴파일 시점에 값이 평가(evaluation)되어 사용 구분 floating-point evaluates to 부동소수/실수 10. 0 10 지수 2. 34 E 5 2. 34 * 10^5 234000 지수 67 e-12 67. 0 * 10^-12 . 0000067 also evaluates to ü 문자 상수(character constants) • 1개 문자 : single quote (ASCII 표) § (일반 문자) ‘b’, ‘C’ § (특수 문자) ‘n’, ‘”’ ü 문자열 상수(string constants) • 여러 문자 : double quote § “ABC”, “Arduino Sketch Programming” Dongyang Mirae University 센서활용프로그래밍 18 prepared by Choon Woo Kwon
아두이노 프로그램 – 상수(constants) LOGO § (상수) Arduino 언어에서 미리 정의된 값(상수) ü 논리 레벨(Logical Level) • false(=0) • true(=1) • ‘true’ ‘false’는 소문자를 사용 • ‘true’의 Boolean에서 의미 : → ‘true’는 ‘ 0’이 아닌 모든 정수를 의미 (-1, 2, -200 은 ‘true’ ü 핀 레벨(Pin Level) • 핀 설정이 INPUT/OUTPUT에 따라 의미가 다름 • HIGH 경우 § pin. Mode( - , INPUT)인 경우, digital. Read() 값 (5 V/3 V board, pin 입력이) 3 V/2 V보다 큰 경우 (pin 입력이) ‘LOW’로 입력되지 않은 모든 경우 § pin. Mode( - , OUTPUT)인 경우, digital. Write() 값 (5 V/3 V board, pin 출력은) 5 V/3. 3 V • LOW 경우 § pin. Mode( - , INPUT)인 경우, digital. Read() 값 (5 V/3 V board, pin 입력이) 3 V/2 V보다 작은 경우 § pin. Mode( - , OUTPUT)인 경우, digital. Write() 값 (5 V/3 V board, pin 출력은) 0 V/0 V ü 핀 구성(Pin Configuration) • INPUT § Arduino 핀을 고-임피던스 상태(high-impedance state)로 설정 § “floating” 되는 경우를 대비하여 pull-up/pull-down resistor 필요 • OUTPUT Dongyang Mirae University 센서활용프로그래밍 19 prepared by Choon Woo Kwon
아두이노 프로그램 – 자료형(Data Type) § 자료형(Data Type) ← 변수 선언 ü void LOGO // return no information to the function from which is was called void setup() { //. . . } • 함수 선언에서만 사용 • 함수의 return 값이 없을 경우 사용 ü boolean • 2가지 값 : ‘true’ ‘false’ • 1 byte memory 사용 int LEDpin = 5; // LED on pin 5 int switch. Pin = 13; // momentary switch on 13, other side connected to ground ü char • 문자 값을 지정 § 1개 문자 : single quote ‘A’ (ASCII 표) § 여러 문자 : double quote “ABC” • Data Type : encoding 값 (-128 ~ 127) § unsigned 1 -byte Data type 경우 : ‘byte’ 사용 • 문자 당 1 byte memory 사용 ü byte / unsigned char boolean running = false; char my. Char = 'A'; char my. Char = 65; // both are equivalent // "B" is the binary formatter (B 10010 = 18 decimal) byte b = B 10010; unsigned char my. Char = 240; • encoding 값 : 0 ~ 255 • Arduino 프로그래밍 스타일 : ‘byte’ 가 선호됨 • 문자 당 1 byte memory 사용 ü int • 기본적인 숫자 저장 타입 • 16 -bit 값 저장(Arduino Uno) § -32, 768 ~ +32767 : -2^15 and a maximum value of (2^15)– 1 § 음수 : 2’s complement math Dongyang Mirae University 센서활용프로그래밍 27 prepared by Choon Woo Kwon
아두이노 프로그램 – 자료형(Data Type) LOGO v word w = 10000; long speed. Of. Light = 186000 L; unsigned long time; short led. Pin = 13; float myfloat; float sensor. Calbrate = 1. 117; Dongyang Mirae University 센서활용프로그래밍 28 prepared by Choon Woo Kwon
아두이노 프로그램 – 자료형(Data Type) LOGO ü string–char array • 문자열 표시 방법 : 2가지 § String § 배열(array) : char 와 null-terminate • 문자열의 끝 : null character(ASCII code ‘ 0’) § Serial. print() 경우 유효 //Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str 2 char Str 2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'}; //Explicitly add the null character, Str 3 char Str 3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '