서울창조경제혁신센터
창조경제 IoT 해커톤대회
2015.9.12 ~ 9.13
3일차교육
아두이노 함수 정리
#1
함수란?
• 변수 : 변하는값
• 상수 : 변하지않는값
• 함수 : 두변수 x, y에대하여 x가정해지면그에따라 y의값이하나만결정될때,
y를 x의함수. y = f(x)
Arduino Language Referencehttps://www.arduino.cc/en/Reference/HomePage
아두이노 함수 : 디지털 입출력void pinMode(pin, mode)
• 매개변수
pin : 설정하고자 하는 디지털 핀 번호
mode : INPUT, OUTPUT, INPUT_PULLUP
• 반환 값 : 없음
void명사
(격식또는문예체) (커다란) 빈공간, 공동; 공허감
형용사(격식) …이하나도[전혀] 없는(격식) 텅빈 참고 null
int (Integer)
[명사] (수학) 정수(整數)
void digitalWrite(pin, value)
• 매개변수
pin : 출력 신호를 내보낼 디지털 핀 번호
mode : HIGH, LOW
• 반환 값 : 없음
int digitalRead(pin)
• 매개변수
pin : 입력 신호를 읽어 올 디지털 핀 번호
• 반환 값 : HIGH, LOW
아두이노 예제 소스
Example : Blink
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Example : Blink
LED : 긴다리
Example : Blink
Example : Fading (PWM)int ledPin = 9; // LED connected to digital pin 9
void setup() {//
}
void loop() {// fade in from min to max in increments of 5 points:for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {// sets the value (range from 0 to 255):analogWrite(ledPin, fadeValue);// wait for 30 milliseconds to see the dimming effectdelay(30);
}
// fade out from max to min in increments of 5 points:for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {// sets the value (range from 0 to 255):analogWrite(ledPin, fadeValue);// wait for 30 milliseconds to see the dimming effectdelay(30);
}}
Example : Fading (PWM)
100 : 255 = 75 : X
X = (255 x 75) / 100
X = 191.25
디지털 입출력
디지털 입출력 예제 : LED, Button
디지털 입출력 예제 (1)
int led = 8;
int button = 9;
void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
int button_status = digitalRead(button);
digitalWrite(led, button_status);
}
led_button.ino
디지털 입출력 예제 (2)int led = 8;
int button = 9;
void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
int button_status = digitalRead(button);
if(button_status == LOW)
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);
}
led_button2.ino
아날로그 입출력
소프트웨어 시리얼 모니터
void setup() {
// initialize serial communications baud rate.
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
Serial.println(“Hello IoT Hackathon!”); // write to PC monitor
delay(1000); // wait for a second
}
serial_test.ino
아날로그 입력 예제 : 가변저항
<출처> http://inodev.co.kr/bbs/board.php?bo_table=lec&wr_id=7
아날로그 입력 예제 : 가변저항void setup() {
Serial.begin(9600);
}
void loop() {
String str;
int potentiometer = analogRead(A0);
str = “Current Potentiometer Value : ” + String(potentiometer);
Serial.println(str);
delay(100);
}
potentiometer.ino
아날로그 입출력 예제 : 가변저항 (2)int led = 3;
void setup() {
//
}
void loop() {
int potentiometer = analogRead(A0);
int brightness = potentiometer / 4; // 0~1023 -> 0~255
analogWrite(led, brightness); // PWM Output
}
potentiometer_led.ino
센서
센서 예제 : 조도 센서 (CDS)
<출처> http://makethat.tistory.com/15
센서 예제 : 조도 센서 (CDS)
<출처> http://danbiksh.egloos.com/v/10539771
센서 예제 : 조도 센서 (CDS)
<출처> http://makethat.tistory.com/15
센서 예제 : 조도 센서 (CDS)
void setup() {
Serial.begin(9600);
}
void loop() {
int cds_value = analogRead(A0);
Serial.println(String(cds_value)); // 0~1023 -> 0~255
delay(500); // PWM Output
}
cds.ino
센서 예제 (2) : 조도 센서 (CDS)int led = 3;
void setup() {
Serial.begin(9600);
}
void loop() {
int cds_value = analogRead(A0);
int mapped_value = map(cds_value, 0, 1023, 0, 255);
// 0~1023 -> map() -> 0~255
analogWrite(led, mapped_value); // PWM Output
Serial.println("CDS Value : " + String(cds_value) + ", PWM Value : "
+ String(mapped_value));
delay(500);
}
cds_pwm_led.ino
센서 예제 : 온도 센서 (LM35)
<출처> http://blog.opid.kr/458
※주의 : +5V 와 GND 를잘못연결하면매우뜨거워
화상의우려가있으니주의할것!!!
<출처> http://www.instructables.com/id/LM35-Temperature-Sensor/
센서 예제 : 온도 센서 (LM35)int led = 3;
void setup() {
Serial.begin(9600);
}
void loop() {
int lm35_value = analogRead(A0);
int mapped_value = map(lm35_value, 0, 1023, 0, 255);
// 0~1023 -> map() -> 0~255
analogWrite(led, mapped_value); // PWM Output
Serial.println(“LM35 Value : " + String(lm35_value) + ", PWM Value :
" + String(mapped_value));
delay(500);
}
lm35.ino
Share Knowledge & Share Spirit
유 명 환, [email protected]
Top Related