当我将其与较大的代码集成时,Arduino会忽略传感器ping

当我将其与较大的代码集成时,Arduino会忽略传感器ping,arduino,sensors,Arduino,Sensors,我有两个独立工作的文件,但当我将它们添加到一起时,一个或两个脚本都停止工作。我在一个脚本中使用温湿度传感器和土壤湿度传感器,另一个脚本ping一个超声波传感器。当我从超声波传感器的文件中添加相关代码行时,我看不出我是如何破坏脚本的。我将在下面用一行连字符将两者分开粘贴 #include <dht_nonblocking.h> #include <Wire.h> #include <DS3231.h> #include <SPI.h> #includ

我有两个独立工作的文件,但当我将它们添加到一起时,一个或两个脚本都停止工作。我在一个脚本中使用温湿度传感器和土壤湿度传感器,另一个脚本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");
}
#包括
#包括
#包括
#包括
#包括
DS3231时钟;
RTCDateTime-dt;
/*根据您的传感器类型取消注释*/
#定义DHT\U传感器类型DHT\U类型11
//#定义DHT\U传感器类型DHT\U类型21
//#定义DHT\U传感器类型DHT\U类型22
静态常数int DHT_传感器_引脚=2;
DHT_非阻塞DHT_传感器(DHT_传感器引脚,DHT_传感器类型);
//土壤水分
常量浮点数=543//您需要将此值替换为值_1
常量浮点数=232//您需要将此值替换为值_2
浮动土壤价值;
浮土含水率;
int-chipSelect=4;
文件数据文件;
/*初始化串行端口。
*/
无效设置()
{
Serial.begin(9600);
clock.begin();
clock.setDateTime(_日期,_时间);
SD.begin(4);
}
/*
*轮询度量值,使状态机保持活动状态。退换商品
*如果测量值可用,则为true。
*/
静态布尔测量环境(浮子*温度、浮子*湿度)
{
静态无符号长度量_timestamp=millis();
/*每四秒钟测量一次*/
如果(毫秒()-测量时间戳>3000ul)
{
如果(dht_传感器测量值(温度、湿度)=真值)
{
测量时间戳=毫秒();
返回(真);
}
}
返回(假);
}
/*
*主程序循环。
*/
空循环()
{
浮子温度;
浮子湿度;
dt=clock.getDateTime();
SoilValue=analogRead(A0);//将传感器插入土壤中
土壤含水量百分比=((土壤值空气值)/(水价空气值)*100);
/*测量温度和湿度。如果功能恢复
如果为true,则可以进行测量*/
if(测量环境(温度和湿度)=真)
{
dataFile=SD.open(“DATA.txt”,FILE\u WRITE);
串行打印(dt.小时);
Serial.print(dt.minute);Serial.print(“:”);
Serial.print(“T=”);
串行打印(温度,1);
串行打印(“摄氏度,H=”);
连续打印(湿度,1);
连续打印(“%”);
连续打印(“土壤水分百分比:”);
连续打印(土壤湿度百分比);
Serial.println(“%”);
延迟(1000);
dataFile.print(dt.hour);
dataFile.print(dt.minute);
dataFile.print(“,”);
数据文件打印(温度,1);
dataFile.print(“,”);
数据文件打印(湿度,1);
dataFile.print(“,”);
dataFile.println(土壤湿度百分比);
dataFile.close();
}
}
-------------------------------------------------------------------------------------------------------------------
#包括
#定义触发器引脚9//Arduino引脚与超声波传感器上的触发器引脚相连。
#定义ECHO_引脚10//Arduino引脚与超声波传感器上的ECHO引脚相连。
#定义最大距离200//我们要ping的最大距离(以厘米为单位)。最大传感器距离的额定值为400-500cm。
新建声纳(触发针、回波针、最大距离);//新建引脚设置和最大距离。
无效设置(){
Serial.begin(9600);//以115200波特打开串行监视器以查看ping结果。
引脚模式(触发器引脚,输出);
pinMode(ECHO_引脚,输入);
}
void循环(){
float uS=sonar.ping();//发送ping,获取ping时间,单位为微秒(uS)。
延迟(1000);//在两次ping之间等待1000ms(大约1pings/sec)。29ms应该是两次ping之间最短的延迟。
序列号。打印(“Ping:”);
Serial.print(uS/uS_往返_CM);//将ping时间转换为距离并打印结果(0=超出设置的距离范围,无ping回波)
序列号。打印号(“cm”);
}