arduino - 当我将它与更大的代码集成时,Arduino 忽略传感器 ping
问题描述
我有两个独立工作的文件,但是当我将它们添加在一起时,一个或两个脚本都会停止工作。我在一个脚本中使用温度和湿度传感器和土壤湿度传感器,而另一个脚本 ping 超声波传感器。当我从超声波传感器的文件中添加相关的代码行时,我没有看到我是如何破坏脚本的。我将在下面粘贴两者,并用一行连字符分隔。
#include <dht_nonblocking.h>
#include <Wire.h>
#include <DS3231.h>
#include <SPI.h>
#include <SD.h>
DS3231 clock;
RTCDateTime dt;
/* Uncomment according to your sensortype. */
#define DHT_SENSOR_TYPE DHT_TYPE_11
//#define DHT_SENSOR_TYPE DHT_TYPE_21
//#define DHT_SENSOR_TYPE DHT_TYPE_22
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
//Soil Moisture
const float AirValue = 543; //you need to replace this value with Value_1
const float WaterValue = 232; //you need to replace this value with Value_2
float SoilValue;
float SoilMoisturePercent;
int chipSelect = 4;
File dataFile;
/* Initialize the serial port.
*/
void setup( )
{
Serial.begin( 9600);
clock.begin();
clock.setDateTime(__DATE__, __TIME__);
SD.begin(4);
}
/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
/*
* Main program loop.
*/
void loop( )
{
float temperature;
float humidity;
dt = clock.getDateTime();
SoilValue = analogRead(A0); //put Sensor insert into soil
SoilMoisturePercent = ((SoilValue-AirValue)/(WaterValue-AirValue)*100);
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if(measure_environment( &temperature, &humidity ) == true )
{
dataFile = SD.open("DATA.txt", FILE_WRITE);
Serial.print(dt.hour);
Serial.print(dt.minute); Serial.print(": ");
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.print( "% " );
Serial.print( "Soil Moisture Percent: " );
Serial.print(SoilMoisturePercent);
Serial.println( "% " );
delay(1000);
dataFile.print(dt.hour);
dataFile.print(dt.minute);
dataFile.print(",");
dataFile.print( temperature, 1 );
dataFile.print( "," );
dataFile.print( humidity, 1 );
dataFile.print( "," );
dataFile.println(SoilMoisturePercent);
dataFile.close();
}
}
-------------------------------------------------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
float uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
delay(1000); // Wait 1000ms between pings (about 1 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
解决方案
推荐阅读
- python - 使用 dataframe.loc 时无法将 dtyped [datetime64[ns]] 数组与 [bool] 类型的标量进行比较的错误
- javascript - 动态创建警报时,Bootstrap closed.bs.alert 不会触发
- laravel-5 - laravel 私人频道和 laravel-echo-server 的身份验证问题
- php - Laravel 5.6 分页(Model.links())总是显示第一页
- graphql-ruby - `orphan_types` 是为接口指定可能的解析类型的唯一方法吗?
- python - 首先检查 NaN,然后根据另一列替换值的函数
- reactjs - React Navigation V2 从底部选项卡导航器中隐藏单个选项卡
- ios - 如何在 Swift 中分隔字符串中的两个 URL?
- python - 如何设置 python-mode 以便 linter 检查 python 3 和 python 2 错误
- vue.js - 作为道具传递的对象数组不更新子组件