DIY PIXHAWK APM等飞控用的声纳-LMLPHP

代码:

SR04 + ApmSonar.ino 打包下载

注意,使用到了SR04的类库。

ApmSonar.ino

// sr04 to apm I2c sonar
// by panxu mail: [email protected]
// Created 2014
#include <Wire.h>
#include "SR04.h"
#define TRIG_PIN 2
#define ECHO_PIN 3
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
uint16_t reading_cm;
uint16_t tempread;
void setup()
{
reading_cm=;
Wire.begin(0x70); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
} void loop()
{
tempread=sr04.DistanceAvg(,);
if (tempread<)
reading_cm=tempread;
}
void requestEvent()
{
byte sendhi;
byte sendli;
byte sendbyte[];
uint16_t tempreading_cm;
tempreading_cm=reading_cm;
sendhi=tempreading_cm>>;
sendli=tempreading_cm&0xff;
sendbyte[]=sendhi;
sendbyte[]=sendli;
Wire.write(sendbyte,);
}
05-11 15:14