240 likes | 414 Vues
Ubiquitous Computing Practice ( Photo Resistor ). Youn-Hee Han, In- Seok Kang { yhhan , Iseka }@kut.ac.kr Laboratory of Intelligent Networks Advanced Technology Research Center Korea University of Technology http://link.kut.ac.kr. Contents. Introduction PhotoResistor LED 밝기 조절
E N D
Ubiquitous Computing Practice(Photo Resistor) Youn-Hee Han, In-Seok Kang {yhhan, Iseka}@kut.ac.kr Laboratory of Intelligent NetworksAdvanced Technology Research CenterKorea University of Technology http://link.kut.ac.kr
Contents • Introduction • PhotoResistor • LED 밝기 조절 • Processing를 이용한 Graph
Introduction • PhotoResistor (Cadmium sulfide or Light dependent resistor) • 빛의 세기에 따라 저항 값이 변하는 센서이다
Introduction http://www.ladyada.net/learn/sensors/cds.html
Reference • Functions • analogRead(pin) – analog pin 에서 int(0~1023) 반환 • analogWrite(pin, val) – val(0~255) 값출력 • Serial • begin(baud) – bits per second • ex)9600, 19200.. • print(val) – 시리얼 통신 이용 val출력 • Serial.print(78) gives "78" • print(val, format) – val을 원하는 format에 맞춰 출력 • Serial.print(78, BIN) gives "1001110"
Reference • Math • map(value, fromLow, fromHigh, toLow, toHigh) • Value 현재 값의 범위 fromLow ~ fromHigh • 변경하고 싶은 값의 범위 toLow, toHigh • ex) intval = analogRead(0); val= map(val, 0, 1023, 0, 255); • constrain(inputVal, lowRange, highRange) • inputVal값이 lowRange ~ highRange사이의 값이면 inputVal반환 • inputVal값이 lowRange값 보다 작으면 lowRange값 반환 • inputVal값이 highRange값 보다 크면 highRange값 반환
센서의 analog값 출력 Sketch intphotocellPin = 0; intphotocellReading; void setup(void) { Serial.begin(9600);// bit per second. } void loop(void) { photocellReading= analogRead(photocellPin); Serial.print("Analog reading = "); Serial.println(photocellReading); delay(1000); }
LED 밝기 조절 Sketch intlightPin = 0; intledPin = 11; void setup() {pinMode(ledPin, OUTPUT); } void loop() {intlightLevel = analogRead(lightPin); lightLevel = 1023 – lightLevel;lightLevel = map(lightLevel, 0, 1023, 0, 255); lightLevel = constrain(lightLevel, 0, 255);analogWrite(ledPin, lightLevel); }
Processing 을 이용하여 Graph를 출력하기 Processing를 이용한 Graph
Arduino - Sketch intlightPin = 0; void setup() { Serial.begin(9600); } void loop() { Serial.println(analogRead(lightPin)); delay(100); }
Processing - Sketch import processing.serial.*; Serial myPort; // The serial port intxPos = 1; // horizontal position of the graph void setup () { size(400, 300); println(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.bufferUntil('\n'); background(0); } void draw () { }
Processing - Sketch void serialEvent (Serial myPort) { String inString = myPort.readStringUntil('\n'); if (inString != null) { inString = trim(inString); float inByte = float(inString); inByte = map(inByte, 0, 1023, 0, height); stroke(127,34,255); line(xPos, height, xPos, height - inByte); if (xPos >= width) { xPos = 0; background(0); } else { xPos++; } } }