User Tools

Site Tools


코드_주석
예제 03A: 버튼이 눌리면 LED를 켜고
다시 누를 때 까지 유지하기
이 예제를 새 아두이노 스케치에 복사해 붙일 것
LED가 사용하는 핀 번호
푸쉬버튼이 연결되어 있는
입력 핀 번호
val은 입력 핀의 상태를
저장하기 위해 사용됨
0 = LED 꺼짐, 1 = LED 켜짐
아두이노에게 LED는 출력
BUTTON은 입력임을 알려줌
입력 값을 읽어 저장
입력이 HIGH (버튼이 눌림)인지 와
상태가 변했는지 검사
LED 켬
예제 05: 버튼이 눌렸을 때 LED를 켬
간단한 바운싱 제거가 포함됨.
버튼을 누르고 있으면 밝기가 변한다.
푸쉬버튼이 사용한 입력 핀
입력핀의 상태 저장
"val" 값의 이전 상태 저장
밝기 값 저장
버튼이 눌리기 시작한 시간
냠냠. 새 값이다.
상태가 변했는지 검사
상태를 끔에서 켬으로 또는
그 반대로 변경
millis() 는 아두이노의 시계 임
이 함수는 보드가 재시작(reset) 
된 후 지난 시간을 미리초 단위로
반환한다.
(이 줄은 마지막으로 버튼이 눌린
시간을 기억한다)
버튼이 계속 눌려 있는지 검사
버튼이 500ms 이상 눌려 있는지
밝기를 1 만큼 증가
밝기가 너무 빨리 변하는 것을
방지하기 위해 대기
255이 밝기의 최대 값
값이 255이 넘는 경우
0으로 초기화 함
val 값은 이제 오래됨. 저장해 둔다
현재 밝기로
LED를 켬
LED를 끔
예제 03C: 버튼을 누르면 LED를 켬
간단한 바운스 제거 포함
개선된 새 버젼!!
이 변수는 변수 "val"의
이전 상태를 저장하기 위함
0 = LED 꺼짐, 1 = LED 켜짐
val 값은 이제 오래됨. 저장해 둔다
예제 04: 애플 컴퓨터의 절전기능 처럼
LED를 서서히 켜고 끄기
증가와 감소를 세기 위해 이 변수를 사용
0에서 254까지 반복 (서서히 켜짐)
LED의 밝기 설정
analogWrite의 반응이 즉각적이므로
변화를 보기 위해서 10ms 만큼
대기해야 함
255에서 1까지 반복 (서서히 꺼짐)
10초간 대기
예제 06A: 아나로그 입력 값의
비율로 LED를 깜박이기
센서에서 들어오는 값을
저장하기 위한 변수
LED는 출력
참고: 아나로그 핀들은 자동으로
입력으로 설정됨
센서로 부터
값을 읽음
LED를 켬
일정 시간 만큼
프로그램을 멈춤
LED를 끔
예제 07: 아나로그 입력 0번에서
읽은 값을 컴퓨터로 전송하기
스케치를 올릴 후 반드시
"시리얼 모니터(Serial Monitor)"를 클릭할 것
센서 저항에 사용할
입력 핀 선택
센서에서 들어오는 값을
저장할 변수
데이터를 컴퓨터로 전송할
시리얼 포트를 연다.
속도는 초당 9600 비트
값을 시리얼 포트에
출력
매 전송 간
100ms 씩 대기
예제 08B: 아두이노 네트워크 램프
센서에서 들어오는 값을 저장하기 위한 변수
시리얼 포트를 연다
센서의 값 읽음
수신한 바이트를 읽음
마커를 찾으면, 따라오는 char 6개가 색 값
char 6개를 처리
버퍼에 저장
저장위치(pointer)를 1 만큼 이동
이제 16진수(HEX)로 저장된 3개의 숫자를 얻음
이 숫자들을 r, g, b 3 바이트로 디코드 해야 함 
버퍼를 다시 사용하기 위해 저장위치를 초기화
변화가 있는지 검사
램프를 켜는 경우
컴퓨터에서 보내 온
색 값으로
LED를 켠다
반대의 경우 끈다
각 전송 간 100ms 씩 대기
하나의 16진 글자를 숫자로 변환
예제 02: 버튼이 눌려있는 동안 LED를 켜기
입력이 HIGH (버튼이 눌림) 인지 검사
예제 03B: 버튼이 눌리면 LED를 켜기
개선된 방법 사용
예제 06B: Set the brightness of LED to
a brightness specified by the
센서에서 받은
밝기 값으로
LED를 켠다
예제 01 : LED 깜박이기
LED는 디지털 핀
13번에 연결되어 있음
디지털 핀을
출력으로 설정
LED를 켠다
1초 동안 대기
LED를 끈다
예제 08A: 아두이노 네트워크 램프 
일부 코드는 토드 E. 커트가 올린 블로그 포스팅
(todbot.com)에서 영감을 받음
이 예제를 새 프로세싱 스케치에 복사해서 붙일 것
매 60초 마다 피드를 받아 옴
마지막으로 피드 내용을 받은 시간
램프에서 측정한 빛의 밝기
아두이노에서 받아 온 글자들을 계산
빨리 갱신할 필요는 없다
중요 공지:
Serial.list()로 받아온 포트 중 첫번째가
포트가 여러분의 아두이노 일 것이다. 아니라면,
다음 줄 앞의 //을 지워 주석을 해제하고, 스케치를
다시 실행하여 시리얼 포트 목록을 확인하라. 그리고,
[ 와 ] 사이의 0을 여러분의 아두이노가 연결된
포트의 번호로 바꾸라.
아두이노에 접속
3 값을 기반으로 색을 구성
아두이노에 전송할 문자열 준비
화면에 색 문자열 출력
1023을 100에 맞춤
아두이노에 데이터 전송
기다리고 있는 데이터가 있는지 검사
한 바이트를 읽음
바이트가 새 줄(newline)인지 검사
버퍼에 추가함
줄의 끝에 도착. 이제 데이터를 처리하자
충분한 데이터가 있는지 확실히 함
마지막 글자(char)를 잘라냄. 이 글자는 캐리지 리턴 임
(캐리지 리턴은 텍스트에서 각 줄의 끝에
위치하는 글자 임)
버터의 스트링을 십진수로 변환
다음 읽기 주기를 위해 버퍼를 비움
우리는 아두이노에서 읽은 값들을 받은 후에
처리할 것이다. 그러므로, 다음에 읽는 값을 최신
으로 하기 위해 버퍼에 쌓여 있던 센서에서 읽은
값들을 지운다.
이 문자열(String)들은 피드를 파싱하기 위해 사용
카운터들을 0으로 초기화
URL을 표현하는 객체
접속을 준비 함
이제 웹사이트에 접속
this is a bit of virtual plumbing as we connect
the data coming from the connection to a buffered
reader that reads the data one line at a time.
피드의 각 줄을 읽음
데이터를 분해 함
각 데이터 조각은 소문자가 된다
"love"를 발견했는가?
love를 1 만큼 증가
"peace"를 발견했는가?
peace를 1 만큼 증가
"arduino"를 발견했는가?
arduino를 1 만큼 증가
64를 우리가 처리할 수 있는 최대 값으로 설정
4를 곱함. 최대값은 255가 된다
이 값은 4 바이트(ARGB)로 만들어진 색
을 만들때 편리하다
오류가 있으면 스케치를 멈춤
예제 03A: Turn on LED when the button is pressed
and keep it on after it is released
Copy and paste this example into an empty Arduino sketch
the pin for the LED
the input pin where the
pushbutton is connected
val will be used to store the state
of the input pin
0 = LED off while 1 = LED on
tell Arduino LED is an output
and BUTTON is an input
read input value and store it
check if the input is HIGH (button pressed)
and change the state
turn LED ON
예제 05: Turn on LED when the button is pressed
including simple de-bouncing.
If the button is held, brightness changes.
input pin of the pushbutton
stores the state of the input pin
stores the previous value of "val"
Stores the brightness value
when did we begin pressing?
yum, fresh
check if there was a transition
change the state from off to on
or vice-versa
millis() is the Arduino clock
it returns how many milliseconds
have passed since the board has
been reset.
(this line remembers when the button
was last pressed)
check whether the button is being held down
If the button is held for more than 500ms.
increment brightness by 1
delay to avoid brightness going
up too fast
255 is the max brightness
if we go over 255
let’s go back to 0
val is now old, let’s store it
turn LED ON at the
current brightness level
turn LED OFF
예제 03C: Turn on LED when the button is pressed
including simple de-bouncing
Now with another new and improved formula!!
this variable stores the previous
value of "val"
0 = LED off and 1 = LED on
val is now old, let's store it
예제 04: Fade an LED in and out like on
a sleeping Apple computer
We’ll use this to count up and down
loop from 0 to 254 (fade in)
set the LED brightness
Wait 10ms because analogWrite
is instantaneous and we would
not see any change
loop from 255 to 1 (fade out)
Wait 10ms
예제 06A: Blink LED at a rate specified by the
value of the analogue input
variable used to store the value
coming from the sensor
LED is as an OUTPUT
Note: Analogue pins are
automatically set as inputs
read the value from
the sensor
turn the LED on
stop the program for
some time
turn the LED off
예제 07: Send to the computer the values read from
analogue input 0
Make sure you click on "Serial Monitor"
after you upload
select the input pin for the
sensor resistor
variable to store the value coming
from the sensor
open the serial port to send
data back to the computer at
9600 bits per second
print the value to
the serial port
wait 100ms between
each send
예제 08B: Arduino Networked Lamp
variable to store the value coming from the sensor
open the serial port
read the value from the sensor
read the incoming byte:
If the marker's found, next 6 characters are the colour
accumulate 6 chars
store in the buffer
move the pointer forward by 1
now we have the 3 numbers stored as hex numbers
we need to decode them into 3 bytes r, g and b
reset the pointer so we can reuse the buffer
Check if there was a transition
if the lamp is on
turn the leds on
at the colour
sent by the computer
otherwise turn off
wait 100ms between each send
converts one HEX character into a number
예제 02: Turn on LED while the button is pressed
check whether the input is HIGH (button pressed)
예제 03B: Turn on LED when the button is pressed
Now with a new and improved formula!
예제 06B: Set the brightness of LED to
a brightness specified by the
turn the LED on at
the brightness set
by the sensor
예제 01 : Blinking LED
LED connected to
digital pin 13
sets the digital
pin as output
turns the LED on
waits for a second
turns the LED off
예제 08A: Arduino networked lamp
parts of the code are inspired
by a blog post by Tod E. Kurt (todbot.com)
Copy and paste this example into an empty Processing sketch
retrieve feed every 60 seconds;
the last time we fetched the content
light level measured by the lamp
Accumulates characters coming from Arduino
we don't need fast updates
IMPORTANT NOTE:
The first serial port retrieved by Serial.list()
should be your Arduino. If not, uncomment the next
line by deleting the // before it, and re-run the
sketch to see a list of serial ports. Then, change
the 0 in between [ and ] to the number of the port
that your Arduino is connected to.
connect to Arduino
Build a colour based on the 3 values
Prepare a string to be sent to Arduino
write the colour string to the screen
this turns 1023 into 100
send data to Arduino
check if there is data waiting
read one byte
if byte is not newline
just add it to the buffer
newline reached, let's process the data
make sure there is enough data
chop off the last character, it's a carriage return
(a carriage return is the character at the end of a
line of text)
turn the buffer from string into an integer number
clean the buffer for the next read cycle
We're likely falling behind in taking readings
from Arduino. So let's clear the backlog of
incoming sensor readings so the next reading is
up-to-date.
we use these strings to parse the feed
zero the counters
An object to represent the URL
prepare a connection
now connect to the Website
this is a bit of virtual plumbing as we connect
the data coming from the connection to a buffered
reader that reads the data one line at a time.
read each line from the feed
break it down
each chunk of data is made lowercase
found "love"?
increment love by 1
found "peace"?
increment peace by 1
found "arduino"?
increment arduino by 1
Set 64 to be the maximum number of references we care about.
multiply by 4 so that the max is 255,
which comes in handy when building a
colour that is made of 4 bytes (ARGB)
If there was an error, stop the sketch
코드_주석.txt · Last modified: 2013/08/03 05:04 by 127.0.0.1