Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Arduino蝙蝠传感器记录次数过多_Java_Arduino_Sensors - Fatal编程技术网

Java Arduino蝙蝠传感器记录次数过多

Java Arduino蝙蝠传感器记录次数过多,java,arduino,sensors,Java,Arduino,Sensors,我需要为一个项目设计一个Arduino,我想我应该添加一些新奇的东西,一个LED变色的东西。LED有一种循环,它可以改变颜色,大约需要40秒。不过,使LED点亮的蝙蝠传感器会记录整个时间,并每秒告诉LED几次,让LED再次亮起。LED没有时间改变颜色,只保留第一种颜色 我不知道如何解决这个问题。我试图给LED一个延迟或什么,但显然我做错了。到目前为止,守则是这样的 //Pin which triggers ultrasonic sound. const int pingPin = 13; //

我需要为一个项目设计一个Arduino,我想我应该添加一些新奇的东西,一个LED变色的东西。LED有一种循环,它可以改变颜色,大约需要40秒。不过,使LED点亮的蝙蝠传感器会记录整个时间,并每秒告诉LED几次,让LED再次亮起。LED没有时间改变颜色,只保留第一种颜色

我不知道如何解决这个问题。我试图给LED一个延迟或什么,但显然我做错了。到目前为止,守则是这样的

//Pin which triggers ultrasonic sound.
const int pingPin = 13;

//Pin which delivers time to receive echo using pulseIn().
int inPin = 12;

//Range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers the red LED.
int safeZone = 10;

//LED pin numbers
int redLed = 3, greenLed = 5;

void setup() {
    //Initialize serial communication
    Serial.begin(9600);

    //Initializing the pin states
    pinMode(pingPin, OUTPUT);
    pinMode(redLed, OUTPUT);
    pinMode(greenLed, OUTPUT);
}

void loop()
{
    //Raw duration in milliseconds, cm is the
    //converted amount into a distance.
    long duration, cm;

    //Sending the signal, starting with LOW for a clean signal 2 staat voor reactie.
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);

    //Setting up the input pin, and receiving the duration in
    //microseconds for the sound to bounce off the object in front.
    pinMode(inPin, INPUT);
    duration = pulseIn(inPin, HIGH); //Documentation for pulseIn(): 
                                     //http://www.arduino.cc/en/Reference/PulseIn

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

    //Printing the current readings to the serial display
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();

    //If het is groter dan 10 dan gaat het lichtje uit
    //else het is binnen bepaalde cm dan gaat het aan van 0 naar 255.
    if(cm>10)
    {
        analogWrite(redLed, 0);
    }
    else{
        analogWrite(redLed, map(cm,0,10,255,0));
        dela
    }

    if(cm>5)
    {
        analogWrite(greenLed, 0);
    }
    else{
        analogWrite(greenLed, map(cm,0,5,255,0));
    }

    delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
但我认为它仍然需要一些延迟。我不确定我使用的传感器叫什么,但它有两轮,一轮发送,一轮接收,它测量接收声音需要多长时间,在我的代码中,我将其转换为cm

我希望您能帮助并理解我的问题所在,因为我对这门语言的知识非常贫乏。

根据需要使用
delay(ms)
延迟40ms。这将在Arduino处理来自超声波传感器的任何数据之前关闭Arduino 40毫秒。

为其设置超时值。否则程序卡在
duration=pulseIn(inPin,HIGH)行中因为如果先前的超声波脉冲未产生回声,则您没有机会发送另一个超声波脉冲

在这种情况下,最大范围为10cm(声音脉冲的移动距离为20cm),因此可以相应地设置超时值(s为距离,v为速度,t为时间):

假设声速在20°C的干燥空气中

考虑到输出脉冲的宽度为2µs,则总时间为584.8µs

而不是

duration = pulseIn(inPin, HIGH);
使用


其他说明:

  • 输出脉冲非常短,预期为2µs

  • ,约为5µs,因此实际脉冲可能长于2µs。即使如此,超声波传感器也可能无法在如此短的时间内启动

  • 即使输出脉冲比您想象的要长,它也是一个周期(如果超声换能器在100kHz下工作,周期为10µs)


  • 尝试使用更长的范围和更长的输出脉冲进行试验,以确保这不是问题。

    这不会造成伤害,但应设置输入引脚、线路引脚模式(inPin,input);,应移出功能回路()。超声波传感器的工作频率是多少?40千赫?100 kHz?是否有一些电子设备与超声波传感器(振荡器、放大器等)相关?
    duration = pulseIn(inPin, HIGH);
    
    duration = pulseIn(inPin, HIGH, 585);