#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int trigPin = 5;
const int echoPin = 18;
const char* apikey = "TRP12D019CRNLC3N";
const char* server = "api.thingspeak.com";
int ledPin = 26;
int buzzer = 25; // choose the pin for the LED
int inputPin = 12; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int irsensor = 14;
int val = 0;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
Adafruit_MPU6050 mpu;
Adafruit_Sensor *mpu_temp, *mpu_accel, *mpu_gyro;
typedef struct {
float temp;
float accX;
float accY;
float accZ;
float gyroX;
float gyroY;
float gyroZ;
} MPU6050;
MPU6050 mpu6050;
SemaphoreHandle_t xMutexMPU6050 = NULL; //创建信号量Handler
TickType_t timeOut = 1000; //用于获取信号量的Timeout 1000 ticks
void mpu6050Task(void *pvParam) {
mpu.begin();
mpu_temp = mpu.getTemperatureSensor();
mpu_temp->printSensorDetails();
mpu_accel = mpu.getAccelerometerSensor();
mpu_accel->printSensorDetails();
mpu_gyro = mpu.getGyroSensor();
mpu_gyro->printSensorDetails();
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
while (1) {
if (xSemaphoreTake(xMutexMPU6050, timeOut) == pdPASS) {
//获取MPU数据
mpu_temp->getEvent(&temp);
mpu_accel->getEvent(&accel);
mpu_gyro->getEvent(&gyro);
mpu6050.temp = temp.temperature;
mpu6050.accX = accel.acceleration.x;
mpu6050.accY = accel.acceleration.y;
mpu6050.accZ = accel.acceleration.z;
mpu6050.gyroX = gyro.gyro.x;
mpu6050.gyroY = gyro.gyro.y;
mpu6050.gyroZ = gyro.gyro.z;
xSemaphoreGive(xMutexMPU6050); //释放钥匙
} else {
//Unable to obtain MUTEX
}
vTaskDelay(500);
}
}
void lcdTask(void *ptParam) { //LCD任务主体
lcd.init();
lcd.backlight();
//定义是 2004 LCD
byte lcdLine = 4;
byte lcdChar = 20;
char line0[lcdChar + 1], line1[lcdChar + 1], line2[lcdChar + 1], line3[lcdChar + 1];
char * line[] = { line0, line1, line2, line3, };
while (1) {
if (xSemaphoreTake(xMutexMPU6050, timeOut) == pdPASS) {
sprintf(line0, " MPU6050 %d", xTaskGetTickCount() / 100);
sprintf(line1, " Temperature %.2f", mpu6050.temp);
sprintf(line2, " ACC %.2f %.2f %.2f", mpu6050.accX, mpu6050.accY, mpu6050.accZ);
sprintf(line3, " GYRO %.2f %.2f %.2f", mpu6050.gyroX, mpu6050.gyroY, mpu6050.gyroZ);
xSemaphoreGive(xMutexMPU6050); //释放钥匙
} else {
//Unable to obtain MUTEX
}
// 显示数据
for (int i = 0; i < 4; i++) {
lcd.setCursor(0, i);
lcd.print(line[i]);
}
vTaskDelay(1000);
}
}
void setup()
{
Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzer, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT);
pinMode(irsensor,OUTPUT); // Sets the echoPin as an Input
xMutexMPU6050 = xSemaphoreCreateMutex(); //创建MUTEX
xTaskCreate(mpu6050Task, "MPU6050", 1024 * 8, NULL, 1, NULL);
vTaskDelay(1000); //让MPU6050提前先运行一秒获取第一笔数据
xTaskCreate(lcdTask, "lcd", 1024 * 8, NULL, 1, NULL);
}
void loop() {
val = digitalRead(inputPin);
int ir = digitalRead(irsensor);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
delay(1); // turn LED ON
} else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
delay(1);// turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
if(ir > 0){
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
Serial.print("Object Detected");
delay(1); // turn LED ON
}
else{
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
Serial.println("Object Not Detected");
delay(1); // turn LED ON
}
if (distanceCm < 100)
{
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
Serial.println("Something Here....");
delay(2);
}
else{
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
Serial.println("Nothing Here....");
delay(2);
}
}
delay(1000);
}