본문 바로가기
project/2020.12-02 (라즈베리파이) 눈으로운전하는휠체어

아두이노 초음파센서 4개, 부저 RGB LED 4개

by sj0020 2021. 1. 22.

50 < distance 이면 led 꺼짐 추가

#include<LiquidCrystal.h> //LCD 라이브러리 추가
//LiquidCrystal lcd(RS, Enable, DB4, DB5, DB6, DB7);
LiquidCrystal lcd(40, 41, 42, 43, 44, 45);

int go = 52; //buzzer

int trigPin1 = 23; //후방 
int echoPin1 = 22;
int red1 = 10;
int grn1 = 9;
int blu1 = 8;

int trigPin2 = 24; //전방 
int echoPin2 = 25;
int red2 = 13;
int grn2 = 12;
int blu2 = 11;

int trigPin3 = 32; //좌 
int echoPin3 = 33;
int red3 = 7;
int grn3 = 6;
int blu3 = 5;

int trigPin4 = 28; //우 
int echoPin4 = 29; 
int red4 = 4;
int grn4 = 3;
int blu4 = 2;
 
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
long duration4, distance4;

unsigned long previousMillis = 0; //이전시간
const long delayTime = 500; //0.5초 대기시간 (sound1에 사용)
const long delayTime1 = 1; //sound2 에 사용 
boolean goState = false;

void lcdF(){ //전방거리 lcd에 출력
  if (distance2 >=100){
      lcd.setCursor(0, 0);
      lcd.print("F:Safe");
    }
  else if ((distance2 <100)&&(distance2>=10) ){
    lcd.setCursor(0, 0);
    lcd.print("F:");     
    lcd.setCursor(2, 0); 
    lcd.print(distance2);    
    lcd.setCursor(4, 0); 
    lcd.print("cm");
  }
  else if (distance2<10) { //거리가 1의자리수일 경우 lcd한칸 뒤에 출력
    lcd.setCursor(0, 0);
    lcd.print("F: "); 
    lcd.setCursor(3, 0);
    lcd.print(distance2);  
    lcd.setCursor(4, 0);
    lcd.print("cm");
  }
  //delay(1000);  
}

void lcdB(){ //후방거리 lcd에출력
  if (distance1 >=100){
    lcd.setCursor(10, 0);
    lcd.print("B:Safe");
  }
  else if ((distance1 <100)&&(distance1>=10) ){
    lcd.setCursor(10, 0);
    lcd.print("B:");     
    lcd.setCursor(12, 0); 
    lcd.print(distance1);    
    lcd.setCursor(14, 0); 
    lcd.print("cm");
  }
  else if (distance1<10) {
    lcd.setCursor(10, 0);
    lcd.print("B: "); 
    lcd.setCursor(13, 0);
    lcd.print(distance1);  
    lcd.setCursor(14, 0);
    lcd.print("cm");
  }
  //delay(1000);  
}

void lcdL(){ //왼쪽거리 lcd에 출력
  if (distance3 >=100){
      lcd.setCursor(0, 1);
      lcd.print("L:Safe");
  }
  else if ((distance3 <100)&&(distance3 >=10) ){
    lcd.setCursor(0, 1);
    lcd.print("L:");     
    lcd.setCursor(2, 1); 
    lcd.print(distance3);    
    lcd.setCursor(4, 1); 
    lcd.print("cm");
  }
  else if (distance3<10) {
    lcd.setCursor(0, 1);
    lcd.print("L: "); 
    lcd.setCursor(3, 1);
    lcd.print(distance3);  
    lcd.setCursor(4, 1);
    lcd.print("cm");
  }
  //delay(1000);  
}

void lcdR(){ //오른쪽거리 lcd에출력
  if (distance4 >=100){
      lcd.setCursor(10, 1);
      lcd.print("R:Safe");
  }
  else if ((distance4 <100)&&(distance4>=10) ){
    lcd.setCursor(10, 1);
    lcd.print("R:");     
    lcd.setCursor(12, 1); 
    lcd.print(distance4);    
    lcd.setCursor(14, 1); 
    lcd.print("cm");
  }
  else if (distance4<10) {
    lcd.setCursor(10, 1);
    lcd.print("R: "); 
    lcd.setCursor(13, 1);
    lcd.print(distance4);  
    lcd.setCursor(14, 1);
    lcd.print("cm");
  }
  //delay(1000);  
}

void sound1() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime){ //0.5초 시간이 흘렀는지 체크
    previousMillis = currentMillis; //0.5초가 지나 참임으로 0.5초 지난 현재시간을 이전시간에 저장
    goState=!goState;   //if문이 참이니깐 0.5초 단위로 ledState 값을 반전시키면 0.5초 단위로 참/거짓됨.
    digitalWrite(go, goState);   //참(5V) or 거짓(0V) 
   }
}
void sound2() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime1){ 
     previousMillis = currentMillis; 
     goState=!goState;   
     digitalWrite(go, goState);   
   }
}
void sound3() {
  analogWrite(go,255);
}

void setup() {
  pinMode(go, OUTPUT);
  pinMode(trigPin1, OUTPUT); // trigPin을 출력 
  pinMode(echoPin1, INPUT); // echoPin을 입력 
  pinMode(trigPin2, OUTPUT); 
  pinMode(echoPin2, INPUT); 
  pinMode(red1, OUTPUT);
  pinMode(blu1, OUTPUT);
  pinMode(grn1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(blu2, OUTPUT);
  pinMode(grn2, OUTPUT); 
  pinMode(trigPin3, OUTPUT); 
  pinMode(echoPin3, INPUT); 
  pinMode(red3, OUTPUT);
  pinMode(blu3, OUTPUT);
  pinMode(grn3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode(red4, OUTPUT);
  pinMode(blu4, OUTPUT);
  pinMode(grn4, OUTPUT);
  lcd.begin(16, 2); //16열 2행짜리 LCD를 사용
  Serial.begin(9600); // 시리얼 포트를 시작
}

void loop() {
  digitalWrite(trigPin1, LOW); //초음파 센서를 초기화
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH); // 트리거 핀에서 나온 펄스를 받아서 
  distance1= duration1*0.034/2; // 거리를 측정
  
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2= duration2*0.034/2;
  
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3= duration3*0.034/2;
  
  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4= duration4*0.034/2;
  
  lcdF();
  lcdB();
  lcdL();
  lcdR();

  if (distance1 >= 500 || distance1 <= 0){ //500보다 크거나, 0보다 작으면 측정이 불가하다는 것을 프린트
    Serial.println("1 Back sensor Out of range"); 
    noTone(go); //부저 소리 끔 
    digitalWrite(red1, LOW);
    digitalWrite(grn1, LOW);
    digitalWrite(blu1, LOW);    
  }
  else  {
    Serial.print ( "Sensor1 : "); //센서 1에
    Serial.print ( distance1);// 거리 값
    Serial.println("cm"); // cm를 출력
    if ((distance1 <=50)&&(distance1>30) ) {
      sound1(); 
      digitalWrite(red1, LOW);
      digitalWrite(grn1, HIGH);
      digitalWrite(blu1, HIGH); 
    }
    else if ((distance1 <=30)&&(distance1>10)){
      sound2();
      digitalWrite(red1, HIGH);
      digitalWrite(grn1, HIGH);
      digitalWrite(blu1, LOW); 
    }
    else if ((distance1 <=10)&&(distance1>0)) {
      sound3();
      digitalWrite(red1, HIGH);
      digitalWrite(grn1, LOW);
      digitalWrite(blu1, LOW); 
    }
    else if (distance1 > 50) {
      noTone(go); //부저 소리 끔 
      digitalWrite(red1, LOW);
      digitalWrite(grn1, LOW);
      digitalWrite(blu1, LOW); 
    }
  }
  //delay(100); // 0.1초 delay 

  if (distance2 >= 500 || distance2 <= 0){
    Serial.println("2 Front sensor Out of range");
    digitalWrite(red2, LOW);
    digitalWrite(grn2, LOW);
    digitalWrite(blu2, LOW);
  }
  else {
    Serial.print("Sensor2 : ");
    Serial.print(distance2);
    Serial.println("cm");
    if ((distance2 <=50)&&(distance2>30)) {
      digitalWrite(red2, LOW);
      digitalWrite(grn2, HIGH);
      digitalWrite(blu2, HIGH); 
    }
    else if ((distance2 <=30)&&(distance2>10)){
      digitalWrite(red2, HIGH);
      digitalWrite(grn2, HIGH);
      digitalWrite(blu2, LOW); 
    }
    else if ((distance2 <=10)&&(distance2>0)) {
      digitalWrite(red2, HIGH);
      digitalWrite(grn2, LOW);
      digitalWrite(blu2, LOW); 
    }
    else if (distance2 > 50) {
      digitalWrite(red2, LOW);
      digitalWrite(grn2, LOW);
      digitalWrite(blu2, LOW); 
    }
  }
  //delay(100);
  
  if (distance3 >= 500 || distance3 <= 0){
    Serial.println("3 Left Sensor Out of range");
    digitalWrite(red3, LOW);
    digitalWrite(grn3, LOW);
    digitalWrite(blu3, LOW);
  }
  else {
    Serial.print("Sensor3 : ");
    Serial.print(distance3);
    Serial.println("cm");
    if ((distance3 <=50)&&(distance3>30)) {
      digitalWrite(red3, LOW);
      digitalWrite(grn3, HIGH);
      digitalWrite(blu3, HIGH); 
    }
    else if ((distance3 <=30)&&(distance3>10)){
      digitalWrite(red3, HIGH);
      digitalWrite(grn3, HIGH);
      digitalWrite(blu3, LOW); 
    }
    else if ((distance3 <=10)&&(distance3>0)) {
      digitalWrite(red3, HIGH);
      digitalWrite(grn3, LOW);
      digitalWrite(blu3, LOW); 
    }
    else if (distance3 > 50) { 
      digitalWrite(red3, LOW);
      digitalWrite(grn3, LOW);
      digitalWrite(blu3, LOW); 
    }
  }
  //delay(100);

  if (distance4 >= 500 || distance4 <= 0){
    Serial.println("4 Right sensor Out of range");
    digitalWrite(red4, LOW);
    digitalWrite(grn4, LOW);
    digitalWrite(blu4, LOW);
  }
  else {
    Serial.print("Sensor4 : ");
    Serial.print(distance4);
    Serial.println("cm");
    Serial.println("");
    if ((distance4 <=50)&&(distance4>30)) {
      digitalWrite(red4, LOW);
      digitalWrite(grn4, HIGH);
      digitalWrite(blu4, HIGH); 
    }
    else if ((distance4 <=30)&&(distance4>10)){
      digitalWrite(red4, HIGH);
      digitalWrite(grn4, HIGH);
      digitalWrite(blu4, LOW); 
    }
    else if ((distance4 <=10)&&(distance4>0)) {
      digitalWrite(red4, HIGH);
      digitalWrite(grn4, LOW);
      digitalWrite(blu4, LOW); 
    }
    else if (distance4 > 50) { 
      digitalWrite(red4, LOW);
      digitalWrite(grn4, LOW);
      digitalWrite(blu4, LOW); 
    }
  }
  //delay(100);
}

최종코드

int go = 52; //buzzer

int trigPin1 = 23; //후방 
int echoPin1 = 22;
int red1 = 10;
int grn1 = 9;
int blu1 = 8;

int trigPin2 = 24; //전방 
int echoPin2 = 25;
int red2 = 13;
int grn2 = 12;
int blu2 = 11;

int trigPin3 = 28; //좌 
int echoPin3 = 29;
int red3 = 7;
int grn3 = 6;
int blu3 = 5;

int trigPin4 = 32; //우 
int echoPin4 = 33; 
int red4 = 4;
int grn4 = 3;
int blu4 = 2;
 
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
long duration4, distance4;

unsigned long previousMillis = 0; //이전시간
const long delayTime = 500; //0.5초 대기시간 (sound1에 사용)
const long delayTime1 = 1; //sound2 에 사용 
boolean goState = false;
void sound1() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime){ //0.5초 시간이 흘렀는지 체크
    previousMillis = currentMillis; //0.5초가 지나 참임으로 0.5초 지난 현재시간을 이전시간에 저장
    goState=!goState;   //if문이 참이니깐 0.5초 단위로 ledState 값을 반전시키면 0.5초 단위로 참/거짓됨.
    digitalWrite(go, goState);   //참(5V) or 거짓(0V) 
	}
}
void sound2() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime1){ 
  	previousMillis = currentMillis; 
  	goState=!goState;   
  	digitalWrite(go, goState);   
	}
}
void sound3() {
  analogWrite(go,255);
}

void setup() {
  pinMode(go, OUTPUT);
  pinMode(trigPin1, OUTPUT); // trigPin을 출력 
  pinMode(echoPin1, INPUT); // echoPin을 입력 
  pinMode(trigPin2, OUTPUT); 
  pinMode(echoPin2, INPUT); 
  pinMode(red1, OUTPUT);
  pinMode(blu1, OUTPUT);
  pinMode(grn1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(blu2, OUTPUT);
  pinMode(grn2, OUTPUT); 
  pinMode(trigPin3, OUTPUT); 
  pinMode(echoPin3, INPUT); 
  pinMode(red3, OUTPUT);
  pinMode(blu3, OUTPUT);
  pinMode(grn3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode(red4, OUTPUT);
  pinMode(blu4, OUTPUT);
  pinMode(grn4, OUTPUT);
  Serial.begin(9600); // 시리얼 포트를 시작
}

void loop() {
  digitalWrite(trigPin1, LOW); //초음파 센서를 초기화
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH); // 트리거 핀에서 나온 펄스를 받아서 
  distance1= duration1*0.034/2; // 거리를 측정
  
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2= duration2*0.034/2;
  
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3= duration3*0.034/2;
  
  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4= duration4*0.034/2;

  if (distance1 >= 500 || distance1 <= 0){ //500보다 크거나, 0보다 작으면 측정이 불가하다는 것을 프린트
    Serial.println("1 Back sensor Out of range"); 
    noTone(go); //부저 소리 끔 
    digitalWrite(red1, LOW);
    digitalWrite(grn1, LOW);
    digitalWrite(blu1, LOW);    
  }
  else  {
    Serial.print ( "Sensor1 : "); //센서 1에
    Serial.print ( distance1);// 거리 값
    Serial.println("cm"); // cm를 출력
    if ((distance1 <=50)&&(distance1>30) ) {
      sound1(); 
      digitalWrite(red1, LOW);
      digitalWrite(grn1, HIGH);
      digitalWrite(blu1, HIGH); 
    }
    else if ((distance1 <=30)&&(distance1>10)){
      sound2();
      digitalWrite(red1, HIGH);
      digitalWrite(grn1, HIGH);
      digitalWrite(blu1, LOW); 
    }
    else if ((distance1 <=10)&&(distance1>0)) {
      sound3();
      digitalWrite(red1, HIGH);
      digitalWrite(grn1, LOW);
      digitalWrite(blu1, LOW); 
    }
  }
  //delay(100); // 0.1초 delay 

  if (distance2 >= 500 || distance2 <= 0){
    Serial.println("2 Front sensor Out of range");
    digitalWrite(red2, LOW);
    digitalWrite(grn2, LOW);
    digitalWrite(blu2, LOW);
  }
  else {
    Serial.print("Sensor2 : ");
    Serial.print(distance2);
    Serial.println("cm");
    if ((distance2 <=50)&&(distance2>30)) {
      digitalWrite(red2, LOW);
      digitalWrite(grn2, HIGH);
      digitalWrite(blu2, HIGH); 
    }
    else if ((distance2 <=30)&&(distance2>10)){
      digitalWrite(red2, HIGH);
      digitalWrite(grn2, HIGH);
      digitalWrite(blu2, LOW); 
    }
    else if ((distance2 <=10)&&(distance2>0)) {
      digitalWrite(red2, HIGH);
      digitalWrite(grn2, LOW);
      digitalWrite(blu2, LOW); 
    }
  }
  //delay(100);
  
  if (distance3 >= 500 || distance3 <= 0){
    Serial.println("3 Left Sensor Out of range");
    digitalWrite(red3, LOW);
    digitalWrite(grn3, LOW);
    digitalWrite(blu3, LOW);
  }
  else {
    Serial.print("Sensor3 : ");
    Serial.print(distance3);
    Serial.println("cm");
    if ((distance3 <=50)&&(distance3>30)) {
      digitalWrite(red3, LOW);
      digitalWrite(grn3, HIGH);
      digitalWrite(blu3, HIGH); 
    }
    else if ((distance3 <=30)&&(distance3>10)){
      digitalWrite(red3, HIGH);
      digitalWrite(grn3, HIGH);
      digitalWrite(blu3, LOW); 
    }
    else if ((distance3 <=10)&&(distance3>0)) {
      digitalWrite(red3, HIGH);
      digitalWrite(grn3, LOW);
      digitalWrite(blu3, LOW); 
    }
  }
  //delay(100);

  if (distance4 >= 500 || distance4 <= 0){
    Serial.println("4 Right sensor Out of range");
    digitalWrite(red4, LOW);
    digitalWrite(grn4, LOW);
    digitalWrite(blu4, LOW);
  }
  else {
    Serial.print("Sensor4 : ");
    Serial.print(distance4);
    Serial.println("cm");
    Serial.println("");
    if ((distance4 <=50)&&(distance4>30)) {
      digitalWrite(red4, LOW);
      digitalWrite(grn4, HIGH);
      digitalWrite(blu4, HIGH); 
    }
    else if ((distance4 <=30)&&(distance4>10)){
      digitalWrite(red4, HIGH);
      digitalWrite(grn4, HIGH);
      digitalWrite(blu4, LOW); 
    }
    else if ((distance4 <=10)&&(distance4>0)) {
      digitalWrite(red4, HIGH);
      digitalWrite(grn4, LOW);
      digitalWrite(blu4, LOW); 
    }
  }
  //delay(100);
}

 

초음파센서 4개 + led4개 + 부저 1개 ( 후방 에서만 작동)

int go = 52; //buzzer

int trigPin1 = 23; //후방 
int echoPin1 = 22;
int red1 = 13;
int grn1 = 12;
int blu1 = 11;

int trigPin2 = 24; //전방 
int echoPin2 = 25;
int red2 = 10;
int grn2 = 9;
int blu2 = 8;

int trigPin3 = 26; //좌 
int echoPin3 = 27;
int red3 = 7;
int grn3 = 6;
int blu3 = 5;

int trigPin4 = 28; //우 
int echoPin4 = 29; 
int red4 = 4;
int grn4 = 3;
int blu4 = 2;
 
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
long duration4, distance4;

unsigned long previousMillis = 0; //이전시간
const long delayTime = 500; //0.5초 대기시간 (sound1에 사용)
const long delayTime1 = 1; //sound2 에 사용 
boolean goState = false;
void sound1() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime){ //0.5초 시간이 흘렀는지 체크
    previousMillis = currentMillis; //0.5초가 지나 참임으로 0.5초 지난 현재시간을 이전시간에 저장
    goState=!goState;   //if문이 참이니깐 0.5초 단위로 ledState 값을 반전시키면 0.5초 단위로 참/거짓됨.
    digitalWrite(go, goState);   //참(5V) or 거짓(0V) 
}
}
void sound2() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime1){ 
  previousMillis = currentMillis; 
  goState=!goState;   
  digitalWrite(go, goState);   
}
}
void sound3() {
  analogWrite(go,255);
}

void setup() {
  pinMode(go, OUTPUT);
  pinMode(trigPin1, OUTPUT); // trigPin을 출력 
  pinMode(echoPin1, INPUT); // echoPin을 입력 
  pinMode(trigPin2, OUTPUT); 
  pinMode(echoPin2, INPUT); 
  pinMode(red1, OUTPUT);
  pinMode(blu1, OUTPUT);
  pinMode(grn1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(blu2, OUTPUT);
  pinMode(grn2, OUTPUT); 
  pinMode(trigPin3, OUTPUT); 
  pinMode(echoPin3, INPUT); 
  pinMode(red3, OUTPUT);
  pinMode(blu3, OUTPUT);
  pinMode(grn3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode(red4, OUTPUT);
  pinMode(blu4, OUTPUT);
  pinMode(grn4, OUTPUT);
  Serial.begin(9600); // 시리얼 포트를 시작
}

void loop() {
  digitalWrite(trigPin1, LOW); //초음파 센서를 초기화
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH); // 트리거 핀에서 나온 펄스를 받아서 
  distance1= duration1*0.034/2; // 거리를 측정

  if (distance1 >= 500 || distance1 <= 0){ //500보다 크거나, 0보다 작으면 측정이 불가하다는 것을 프린트
    Serial.println("1 Back sensor Out of range"); 
    noTone(go); //부저 소리 끔 
    digitalWrite(red1, LOW);
    digitalWrite(grn1, LOW);
    digitalWrite(blu1, LOW);    
  }
  else  {
    Serial.print ( "Sensor1 : "); //센서 1에
    Serial.print ( distance1);// 거리 값
    Serial.println("cm"); // cm를 출력
    if ((distance1 <=50)&&(distance1>30) ) {
      sound1(); 
      digitalWrite(red1, LOW);
      digitalWrite(grn1, HIGH);
      digitalWrite(blu1, HIGH); 
    }
    else if ((distance1 <=30)&&(distance1>10)){
      sound2();
      digitalWrite(red1, HIGH);
      digitalWrite(grn1, HIGH);
      digitalWrite(blu1, LOW); 
    }
    else if ((distance1 <=10)&&(distance1>0)) {
      sound3();
      digitalWrite(red1, HIGH);
      digitalWrite(grn1, LOW);
      digitalWrite(blu1, LOW); 
    }
  }
  //delay(100); // 0.1초 delay 

  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2= duration2*0.034/2;

  if (distance2 >= 500 || distance2 <= 0){
    Serial.println("2 Front sensor Out of range");
//    sound2();
    digitalWrite(red2, HIGH);
    digitalWrite(grn2, LOW);
    digitalWrite(blu2, LOW);
  }
  else {
    Serial.print("Sensor2 : ");
    Serial.print(distance2);
    Serial.println("cm");
    digitalWrite(red2, LOW);
    digitalWrite(grn2, LOW);
    digitalWrite(blu2, LOW);
  }
  //delay(100);


  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3= duration3*0.034/2;

  if (distance3 >= 500 || distance3 <= 0){
    Serial.println("3 Left Sensor Out of range");
    //sound1();
    digitalWrite(red3, HIGH);
    digitalWrite(grn3, HIGH);
    digitalWrite(blu3, LOW);
  }
  else {
    Serial.print("Sensor3 : ");
    Serial.print(distance3);
    Serial.println("cm");
    digitalWrite(red3, LOW);
    digitalWrite(grn3, LOW);
    digitalWrite(blu3, LOW);
  }
  //delay(100);

  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4= duration4*0.034/2;

  if (distance4 >= 500 || distance4 <= 0){
    Serial.println("4 Right sensor Out of range");
    //noTone(go);
    digitalWrite(red4, HIGH);
    digitalWrite(grn4, HIGH);
    digitalWrite(blu4, LOW);
  }
  else {
    Serial.print("Sensor4 : ");
    Serial.print(distance4);
    Serial.println("cm");
    Serial.println("");
    digitalWrite(red4, LOW);
    digitalWrite(grn4, LOW);
    digitalWrite(blu4, LOW);
  }
  //delay(100);
}

 

 

초음파센서 4개 + led4개

int trigPin1 = 23;  //후방
int echoPin1 = 22;
int red1 = 13;
int grn1 = 12;
int blu1 = 11;

int trigPin2 = 24; //전방
int echoPin2 = 25;
int red2 = 10;
int grn2 = 9;
int blu2 = 8;

int trigPin3 = 26; //좌
int echoPin3 = 27;
int red3 = 7;
int grn3 = 6;
int blu3 = 5;

int trigPin4 = 28; //우
int echoPin4 = 29; 
int red4 = 4;
int grn4 = 3;
int blu4 = 2;


long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
long duration4, distance4;

void setup() {
  pinMode(trigPin1, OUTPUT); // trigPin을 출력으로 
  pinMode(echoPin1, INPUT); // echoPin을 입력이다.
  pinMode(trigPin2, OUTPUT); 
  pinMode(echoPin2, INPUT);
  pinMode(red1, OUTPUT);
  pinMode(blu1, OUTPUT);
  pinMode(grn1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(blu2, OUTPUT);
  pinMode(grn2, OUTPUT); 
  pinMode(trigPin3, OUTPUT); 
  pinMode(echoPin3, INPUT); 
  pinMode(red3, OUTPUT);
  pinMode(blu3, OUTPUT);
  pinMode(grn3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode(red4, OUTPUT);
  pinMode(blu4, OUTPUT);
  pinMode(grn4, OUTPUT);
  Serial.begin(9600); // 시리얼 포트 시작
}

void loop() {
  digitalWrite(trigPin1, LOW); //초음파 센서를 초기화
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH); // 트리거 핀에서 나온 펄스를 받아서 
  distance1= duration1*0.034/2; // 거리를 측정
  
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2= duration2*0.034/2;
  
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3= duration3*0.034/2;
  
  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4= duration4*0.034/2;

  if (distance1 >= 500 || distance1 <= 0){ //500보다 크거나, 0보다 작으면 측정이 불가하다는 것을 프린트합니다.
    Serial.println("1Out of range"); //측정 불가 라는 것을 프린트 합니다. 
    digitalWrite(red1, LOW);
    digitalWrite(grn1, LOW);
    digitalWrite(blu1, HIGH);
  }
  else {
    Serial.print ( "Sensor1 : "); //센서 1에
    Serial.print ( distance1);// 거리 값
    Serial.println("cm"); // cm를 출력합니다.
    digitalWrite(red1, LOW);
    digitalWrite(grn1, LOW);
    digitalWrite(blu1, LOW);
  }
  delay(100); // delay 0.1초  


  if (distance2 >= 500 || distance2 <= 0){
    Serial.println("2Out of range");
    digitalWrite(red2, HIGH);
    digitalWrite(grn2, LOW);
    digitalWrite(blu2, LOW);
  }
  else {
    Serial.print("Sensor2 : ");
    Serial.print(distance2);
    Serial.println("cm");
    digitalWrite(red2, LOW);
    digitalWrite(grn2, LOW);
    digitalWrite(blu2, LOW);
  }
  delay(100);


  if (distance3 >= 500 || distance3 <= 0){
    Serial.println("3Out of range");
    digitalWrite(red3, HIGH);
    digitalWrite(grn3, HIGH);
    digitalWrite(blu3, LOW);
  }
  else {
    Serial.print("Sensor3 : ");
    Serial.print(distance3);
    Serial.println("cm");
    digitalWrite(red3, LOW);
    digitalWrite(grn3, LOW);
    digitalWrite(blu3, LOW);
  }
  delay(100);


  if (distance4 >= 500 || distance4 <= 0){
    Serial.println("4Out of range");
    digitalWrite(red4, HIGH);
    digitalWrite(grn4, HIGH);
    digitalWrite(blu4, LOW);
  }
  else {
    Serial.print("Sensor4 : ");
    Serial.print(distance4);
    Serial.println("cm");
    Serial.println("");
    digitalWrite(red4, LOW);
    digitalWrite(grn4, LOW);
    digitalWrite(blu4, LOW);
  }
  delay(100);

}

초음파센서 4개



//핀을 정합니다. 
int trigPin1 = 2;//green
int echoPin1 = 3;//blue
int trigPin2 = 4; 
int echoPin2 = 5;

int trigPin3 = 6;
int echoPin3 = 7;
int trigPin4 = 8;
int echoPin4 = 9;

//변수를 설정합니다. 
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
long duration4, distance4;

void setup() {
  pinMode(trigPin1, OUTPUT); // trigPin을 출력으로 
  pinMode(echoPin1, INPUT); // echoPin을 입력이다.
  pinMode(trigPin2, OUTPUT); // trigPin을 출력으로 
  pinMode(echoPin2, INPUT); // echoPin을 입력이다.
  pinMode(trigPin3, OUTPUT); // trigPin을 출력으로 
  pinMode(echoPin3, INPUT); // echoPin을 입력이다.
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  Serial.begin(9600); // 시리얼 포트를 시작합니다.
}

void loop() {
  digitalWrite(trigPin1, LOW); //초음파 센서를 초기화 하는 과정입니다.
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH); // 트리거 핀에서 나온 펄스를 받아서 
  distance1= duration1*0.034/2; // 거리를 측정합니다. 

  if (distance1 >= 500 || distance1 <= 0){ //500보다 크거나, 0보다 작으면 측정이 불가하다는 것을 프린트합니다.
    Serial.println("1Out of range"); //측정 불가 라는 것을 프린트 합니다. 
  }
  else {
    Serial.print ( "Sensor1 : "); //센서 1에
    Serial.print ( distance1);// 거리 값
    Serial.println("cm"); // cm를 출력합니다.
  }
  delay(500); //2초마다 , 그리고 아래의 과정은 모두 동일합니다. 

  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2= duration2*0.034/2;

  if (distance2 >= 500 || distance2 <= 0){
    Serial.println("2Out of range");
  }
  else {
    Serial.print("Sensor2 : ");
    Serial.print(distance2);
    Serial.println("cm");
  }
  delay(500);
/*
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3= duration3*0.034/2;

  if (distance3 >= 500 || distance3 <= 0){
    Serial.println("3Out of range");
  }
  else {
    Serial.print("Sensor3 : ");
    Serial.print(distance3);
    Serial.println("cm");
  }
  delay(2000);

  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4= duration4*0.034/2;

  if (distance4 >= 500 || distance4 <= 0){
    Serial.println("4Out of range");
  }
  else {
    Serial.print("Sensor4 : ");
    Serial.print(distance4);
    Serial.println("cm");
    Serial.println("");
  }
  delay(2000);
*/  

 출처: blog.naver.com/bitelab

 

//초음파 센서의 핀번호를 설정한다.
int echoPin = 12;
int trigPin = 13;
int go = 6; //buzzer
int BLUE = 11;
int GREEN = 10;
int RED = 9;

unsigned long previousMillis = 0; //이전시간
const long delayTime = 500; //0.5초 대기시간
const long delayTime1 = 50;
boolean goState = false;
void sound1() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime){ //0.5초 시간이 흘렀는지 체크
    previousMillis = currentMillis; //0.5초가 지나 참임으로 0.5초 지난 현재시간을 이전시간에 저장
    goState=!goState;   //if문이 참이니깐 0.5초 단위로 ledState 값을 반전시키면 0.5초 단위로 참/거짓됨.
    digitalWrite(6, goState);   //참(5V) or 거짓(0V) 
}
}
void sound2() {
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= delayTime1){ //0.5초 시간이 흘렀는지 체크
  previousMillis = currentMillis; //0.5초가 지나 참임으로 0.5초 지난 현재시간을 이전시간에 저장
  goState=!goState;   //0.5초 if문이 참이니깐 0.5초 단위로 ledState 값을 반전시키면 0.5초 단위로 참/거짓됨.
  digitalWrite(6, goState);   //참(5V) or 거짓(0V) 
}
}
void sound3() {
  analogWrite(go,255);
}
void setup() {
  Serial.begin(9600);
  // trig를 출력모드로 설정, echo를 입력모드로 설정
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(go, OUTPUT);
}
void loop() {  
  // 초음파를 보낸다. 다 보내면 echo가 HIGH 상태로 대기하게 된다.
    digitalWrite(trigPin, LOW);
    digitalWrite(echoPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // echoPin 이 HIGH를 유지한 시간을 저장 한다.
    unsigned long duration = pulseIn(echoPin, HIGH); 
  // HIGH 였을 때 시간(초음파가 보냈다가 다시 들어온 시간)을 가지고 거리를 계산 한다.
  float distance = ((float)(340 * duration) / 10000) / 2;  
    Serial.print(distance);
  Serial.println("cm");
  // 수정한 값을 출력
  if ((distance <=30)&&(distance>20) ) {
    sound1();
    analogWrite(BLUE, 0);
    analogWrite(GREEN, 1);
    analogWrite(RED, 0);
  }
  else if ((distance <=20)&&(distance>10)){
    sound2();
    analogWrite(BLUE, 1);
    analogWrite(GREEN, 0);
    analogWrite(RED, 0);
  }
  else if ((distance <=10)&&(distance>0)) {
    sound3();
    analogWrite(BLUE, 0);
    analogWrite(GREEN, 0);
    analogWrite(RED, 1);
  }
  else {
    noTone(go);
    analogWrite(BLUE, 0);
    analogWrite(GREEN, 0);
    analogWrite(RED, 0);
  }
}

거리 = 30~21 -> 삑-삑-삑-  + led green

거리 = 20~11 -> 삑삑삑  + led blue

거리 = 10이하 -> 삐----- + led red

 

위의 코드는 초음파 센서 여러개 하니까 동작하지 않아서 다시 젤 위의 코드 찾아서 바꿈