|
#include <SoftwareSerial.h> //Arduino I2C library
#include "DHT.h"
#define DHTPIN A1
#define DHTTYPE DHT11 // DHT 11
//byte serialA;
//#include <Wire.h>
const int trig = 8; //define Arduino pin
const int echo = 7;
const int TX = 1;
const int RX = 0;
const int delay_time = 400; //delay 1000 ms for every measurment
const int Motor_E2 = 11;
const int Motor_E1 = 10;
const int Motor_M1 = 12;
const int Motor_M2 = 13;
char val; // variable to receive data from the serial port(bluetooth)
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600); //set baud rate of Bluetooth
pinMode(Motor_M1, OUTPUT);
pinMode(Motor_M2, OUTPUT);
pinMode(trig, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor)
pinMode(echo, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor)
dht.begin();
}
void loop()
{
byte Data[7];
byte cmmd[20];
int insize;
int x =pulseIn(echo, HIGH);
int y = dht.readHumidity();
int z = dht.readTemperature();
//serialA=I2CBT.read();
Data[0]=97;
Data[1]=x/256;
Data[2]=x%256;
Data[3]=y/256;
Data[4]=y%256;
Data[5]=z/256;
Data[6]=z%256;
float duration; //duration to record the time of every back and forth
float distance;
digitalWrite(trig, HIGH); //trigger sona for 1 ms
delayMicroseconds(1000);
digitalWrite(trig, LOW);
//int x =pulseIn(echo, HIGH);
duration = pulseIn(echo, HIGH);
//use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds
distance = (duration / 2) * 0.034;
//distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) )
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
//Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
delay(200);
int sendData = (int) (distance * 100); //times 100 and convert disance to integer
byte Data[3];
//Data[0] = 97; //key send to phone
//Data[1] = sendData / 256; //divides sendData to two 1 byte packets
//Data[2] = sendData % 256;
這邊原本是傳送超音波的
那我現在要傳送三種資料
要怎麼把這幾行寫到上面的Data[0]~Data[2]
複製到上面去就可以了嗎?
if(Serial.available()>0)
{
val = Serial.read();
switch(val)
{
case 97: //測距離
for(int i = 0; i < 6; i++)
Serial.write(Date[ i]); //send packet to phone
break;
case 'f': // car forward
forward(0, 255);
break;
case 'd': // car backward
backward(0, 255);
break;
case 'l': // car turn left
left(0, 255);
break;
case 'r': // car turn right
right(0, 255);
break;
case 's': // car stop
motorstop(0, 0);
break;
}
}
}
void motorstop(byte flag, byte motorspeed)
{
digitalWrite( Motor_E1, motorspeed);
digitalWrite( Motor_E2, motorspeed);
}
void forward(byte flag, byte motorspeed)
{
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 200);
analogWrite( Motor_E2, 200);
}
void backward(byte flag, byte motorspeed)
{
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, LOW);
analogWrite( Motor_E1, 200);
analogWrite( Motor_E2, 200);
}
void right(byte flag, byte motorspeed)
{
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 150);
analogWrite( Motor_E2, 0);
}
void left(byte flag, byte motorspeed)
{
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 0);
analogWrite( Motor_E2, 150);
}
我想再問一下紅色的三行 我應該要有嗎
//#include <Wire.h> 這行我查(1) I2C 地址設定 及 I2C 地址掃瞄是這樣
因為我編譯會再serialA=I2CBT.read();這行有錯誤
但是我當初傳送超音波沒有用到這幾行
但是我找範例傳送溫度資料他們是有那幾行
|
|