아두이노 기초2015. 2. 24. 15:14


예제 1

#define TRIG 2
#define ECHO 3
#define RED 11
#define GREEN 10
#define BLUE 9

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  
  long distance = pulseIn(ECHO, HIGH) / 58.2;
  
  analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0
  
  if(distance < 10) {
    analogWrite(RED, 255);
  } else if(distance < 20) {
    analogWrite(GREEN, 255);
  } else if(distance < 30) {
    analogWrite(BLUE, 255);
  }
  
  delay(100);
}


예제 2

#define TRIG 2
#define ECHO 3

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  
  long distance = pulseIn(ECHO, HIGH) / 58.2;
  
  tone(8, 1000, 20);
  delay(100);
  tone(8, 1000, 20);
  
  delay(distance);
}


'아두이노 기초' 카테고리의 다른 글

11. 시리얼 통신  (0) 2015.02.25
10. 서브모터  (0) 2015.02.24
9. 7세그먼트  (0) 2015.02.24
8. LCD 패널  (0) 2015.02.24
6. 조도센서  (0) 2015.02.24
Posted by trikyu