我有一个使用arduino,MIT App Inventor和hc06蓝牙模块的项目草图。基本上,arduino应该从加速度计(MPU6050)和数字PIR传感器获取数据,因此如果Pir传感器为高,并且没有加速度,则finVal(临时变量)将变高。然后将该finVal传输到App,其中在finVal为真的不同时间给出某些协议。如果没有按下应用程序上的按钮,并且如果finVal为true,则out变为true(为arduino板上的扬声器和指示灯供电)。我有以下arduino sketch和mit app发明者块,并且不知道它为什么不起作用。我将不胜感激任何帮助。谢谢!
int pirInput = A3;
int accValue;
int pirValue;
int finVal;
String t;
int out = A0;
int out1=A1;
int OldAcX;
int OldAcY;
//_bluetooth.enable();
int state = LOW;
#include "I2Cdev.h"
#include "MPU6050.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 accelgyro;
const int MPU = 0x68; // I2C address of the MPU-6050
int16_t ax, ay, az, gx, gy, gz;
#define OUTPUT_READABLE_ACCELGYRO
void setup() {
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
accelgyro.initialize();
Serial.begin(9600);
delay(750);
pinMode(out, OUTPUT);
pinMode(finVal, OUTPUT);
pinMode(pirInput, INPUT);
pinMode(out1,OUTPUT);
digitalWrite(pirInput, LOW);
digitalWrite(out, 0);
digitalWrite(out, LOW);
digitalWrite(accValue, LOW);
}
void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// #ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y values
Serial.print("x" + ax); Serial.print("\t");
Serial.print("y" + ay); Serial.print("\t");
//if motion is changing in any direction more than the allowed tolerance
if (abs(ax - OldAcX) < 200 && abs(ay - OldAcY) < 200)
{
accValue == HIGH;
}
else {
accValue == LOW;
}
pirValue = digitalRead(pirInput);
Serial.println("pir" + pirValue);
if (pirValue == HIGH && accValue == HIGH)
{
finVal = 1;
}
else {
finVal = 0;
}
while(Serial.available()){
t+=(char)Serial.read();
}
//turns alarm on/off
if(t=="on"){
digitalWrite(out, HIGH);
digitalWrite(out, 1);
delay(1000);
}
else {
digitalWrite(out, LOW);
digitalWrite(out, 0);
delay(1000);
}
if (out >0) //blinks led on external breadboard
{
digitalWrite(out1, HIGH);
delay(1000);
digitalWrite(out1, LOW);
delay(1000);
}
//new accValues
OldAcX = ax;
OldAcY = ay;
}