gpt4 book ai didi

c - 嵌入式C:实现逻辑

转载 作者:行者123 更新时间:2023-11-30 20:38:36 25 4
gpt4 key购买 nike

我写了一个代码来检测颜色,当检测到颜色时,然后读取超声波传感器。

但是在当前代码中,它每次都会检查颜色传感器,然后测试超声波传感器的输出...我的意思是它会针对每个读数检查颜色传感器...
我实际上只想检查一次色彩传感器的输出,然后开始获取读数而不必担心色彩...
但是,如果一段时间后再次检测到相同的颜色,则应停止读取读数...

这是代码:

 int sensorPin=A0;    // select ip which reads from color                       //sensor  to detect blue color
int sensorValue=0;


const int pingPin = 13; //pin which triggers ultrasonic sound


int inPin = 12; //pin which delivers time to receive echo




void setup() {

Serial.begin(9600);// initialize serial communication


pinMode(pingPin, OUTPUT); //initializing the pin states
pinMode(inPin, INPUT); //setting up the input pin

}

void loop()
{

sensorValue= analogRead(sensorPin);

long duration, cm;

if(sensorValue >= 1 )
{
digitalWrite(pingPin, LOW); //sending the signal, starting with LOW for a clean signal
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
}



duration = pulseIn(inPin, HIGH);


cm = microsecondsToCentimeters(duration);// convert the time into a distance

Serial.print(cm); //printing readings to serial display
Serial.print("cm");
Serial.println();


}


float microsecondsToCentimeters(float microseconds)
{

return microseconds / 29.0 / 2.0;
}

最佳答案

sensorValue  is a global variable.


您需要在 if内部清除它:

if(sensorValue >= 1 )
{
sensorValue = 0;
:
:
}


编辑:
上面的答案基于这样的假设:仅当检测到新的输入脉冲时, analogRead(sensorPin);函数将返回非零值。

关于c - 嵌入式C:实现逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com