使用粒子剂进行距离测量的超声波传感器

时间:2018-07-28 03:58:55

标签: python arduino particles arduino-ultra-sonic particle.io

我有一个运行Raspberry pi的颗粒剂,并且一直在寻找触发超声波传感器的最佳方法。我有办法在代理上的.ino文件上进行云计算吗?我已经将其与python脚本一起使用,无法使其与pi上的Particle-agent一起使用。

这是python代码。

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 17
ECHO = 27

# print "Distance Measurement In Progress"

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)

# while True:

# print "Waiting For Sensor To Settle"
def main():
    time.sleep(1)

    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)

    while GPIO.input(ECHO)==0:
        pulse_start = time.time()

    while GPIO.input(ECHO)==1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150

    distance = round(distance, 1)

    print "dis=",distance,"'C"

    # return distance
    time.sleep(1)
    return distance
    GPIO.cleanup()

if __name__ == '__main__':
    main()

我确实尝试过这样做,但是没有用。我的值始终为0。(检查了引脚,并使用python脚本运行了此功能)

unsigned long duration;

void setup()
{

    pinMode(D3, OUTPUT);   // LED on D7 pin 22

                           // ultrasonic range finder Robotshop RB-lte-54

                           // GND pin goes to ground
    pinMode(D2, INPUT);    // echo pin 27
    pinMode(D1, OUTPUT);   // Trig pin 17
                           // VCC pin goes to VIN on the pi 5v

}

void loop(){

        digitalWrite(D1, HIGH);         // activate trigger
        delayMicroseconds(10);
        digitalWrite(D1, LOW);          // de-activate trigger

        duration = pulseIn(D2, HIGH);   // how long until a reply?
                                        // a blocking call so may wait a few seconds                
        if (duration > 2000    ){        // raw data from 200 to 16000                                         
            digitalWrite(D3, HIGH);     // D7 Blue LED on if far
        } else {
            digitalWrite(D3, LOW);      // D7 Blue LED off
        } 
        delay(3000);  
}

谢谢!

0 个答案:

没有答案