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

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

by sj0020 2021. 1. 26.

1m 이상일 경우 안전함 추가

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

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 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); 
    }
  }
  //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);
}

1~99cm 까지만 프린트 가능

#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)&&(distanc2>=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)&&(distanc1>=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");
  }
  else if (distance1 >= 500 || distance1 <= 0){
   lcd.setCursor(10, 0);
    lcd.print("B: "); 
    lcd.setCursor(13, 0);
    lcd.print("O");
  }
  delay(1000);  
}

void lcdL(){ //왼쪽거리 lcd에 출력
  if ((distance3 <100)&&(distanc3 >=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)&&(distanc4>=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); 
    }
  }
  //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);
}