데이크루 5기

차량 급커브 전복 감지장치

2023.05.31 12:46 864 조회

차량 급커브 전복 감지장치 창작 프로그래밍






아두이노를 이용한 직접 창작코딩입니다.



#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include <Arduino_SensorKit.h>

#include <Arduino_SensorKit_LIS3DHTR.h>


#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire , -1 );

int buzzerPin = 5 ;

int ledPin = 6 ;

// 버저 멜로디

const float melody[] = {700 ,400 ,500 ,430 , 300 };

const int noteDuration = 200 ;

void setup (){

 Serial .begin (115200 );

 while (!Serial );

 display .begin (SSD1306_SWITCHCAPVCC, 0x 3C ); // OLED 주소가 0x3C라고 가정합니다.

 display .clearDisplay ();

 pinMode (buzzerPin, OUTPUT );

 pinMode (ledPin, OUTPUT );

 Accelerometer .begin ();

}

void loop (){

 float Ax = Accelerometer .readX ();

 float Ay = Accelerometer .readY ();

 float Az = Accelerometer .readZ ();

 display .clearDisplay ();

 display .setTextColor (SSD1306_WHITE );

 display .setTextSize (1 );

 display .setCursor (0 , 0 );

 display .print ("X: ");

 display .print (Ax, 2 );

 display .print (" Y: ");

 display .print (Ay, 2 );

 display .print (" Z: ");

 display .println (Az, 2 );

 display .display ();

 if ((Ax >= -0.6 &&Ax <= -0.3 ) || (Ay >= -0.6 &&Ay <= -0.3 ) || (Az >= -0.6 &&Az <= -0.3 ) ||

   (Ax >= 0.3 &&Ax <= 0.6 ) || (Ay >= 0.3 &&Ay <= 0.6 ) || (Az >= 0.3 &&Az <= 0.6 )){

  digitalWrite (ledPin, HIGH ); // LED 켜기

  playMelody ();        // 버저 멜로디 연주

 }else {

  digitalWrite (ledPin, LOW ); // LED 끄기

 }

 delay (500 );

}

void playMelody (){

 for (int i = 0 ; i <sizeof (melody ) / sizeof (melody [0 ]); i++){

  tone (buzzerPin, melody [i], noteDuration );

  delay (noteDuration );

  noTone (buzzerPin );

  delay (50 ); // 다음 음 사이의 일시적인 딜레이

 }

}

 



자동차가 급커브시 일정각도 기울어지면 소리와 위험등이 켜집니다.


공유시 댓글남겨주세요.