int IRpin = 1; // analog pin for reading the IR sensor
void setup() {
Serial.begin(9600); // start the serial port
}
void loop() {
float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance); // print the distance
delay(100); // arbitary wait time.
}
그런데 이 값을 통해 핀을 조작하고자 한다면 다음과 같이 첨부하면 된다.
출처 : http://arduino.cc/en/Reference/PinMode
pinMode()
Description
Configures the specified pin to behave either as an input or an output. See the description of digital pins for details on the functionality of the pins.
As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pullups.
Syntax
pinMode(pin, mode)
Parameters
pin: the number of the pin whose mode you wish to set
voidsetup() { pinMode(ledPin,OUTPUT);// sets the digital pin as output }
voidloop() { digitalWrite(ledPin,HIGH);// sets the LED on delay(1000);// waits for a second digitalWrite(ledPin,LOW);// sets the LED off delay(1000);// waits for a second }
위 두개를 합치면 다음과 같은 코드
(난 가까이오면 13번 디지털 핀이 온 되는걸 하고자함, 다른때는 꺼지고.)
int IRpin = 1; // analog pin for reading the IR sensor
int IRpin = 13; // 13번으로 아웃풋나감
void setup() {
Serial.begin(9600); // start the serial port
pinMode(IRpin, OUTPUT);
}
void loop() {
float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance); // print the distance
delay(100); // arbitary wait time.
if(distance < 30){
digitalWrite(IRpin,HIGH);
} else{
digitalWrite(IRpin,LOW);
}
}
내가쓰는 센서 스펙
이건 이번에 하려는 계획!
연결방식
출처 : http://robobob.co.kr/56
다른 블로그의 이미지 예시.. 위와 거의 비슷하나 나는 IR센서가 추가되므로 그부분도 진행후 사진올리겠당