eagler8 发表于 2019-10-6 17:38:17

eagler8 发表于 2019-10-6 17:39:27

eagler8 发表于 2019-10-6 18:27:12

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十三:夏普GP2Y0A02YK0F 红外测距传感器模块 20-150cm 距离传感器
项目:测试距离的方案之二
Module      UNO
VCC   ——   5V
GND——   GND
VO    ——   A0
*/

#define pin A0

void setup () {
      Serial.begin (9600);
      pinMode (pin, INPUT);
}

void loop () {
      uint16_t value = analogRead (pin);
      uint16_t range = get_gp2y0a02 (value);
      Serial.println (value);
      Serial.print (range);
      Serial.println (" cm");
      Serial.println ();
      delay (500);
}

//return distance (cm)
uint16_t get_gp2y0a02 (uint16_t value) {
      if (value < 70)value = 70;
      return 12777.3/value-1.1;      //(cm)
      //return (62.5/(value/1023.0*5)-1.1);      //(cm)
      //return ((67870.0 / (value - 3.0)) - 40.0); //gp2d12 (mm)
}

eagler8 发表于 2019-10-6 18:28:29

eagler8 发表于 2019-10-6 18:55:52

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十三:夏普GP2Y0A02YK0F 红外测距传感器模块 20-150cm 距离传感器
项目:测试距离的方案之三
说明;参考V-cm图, 用查表法, 连接Vout到Arduino的A0引脚, 测量频率20Hz
需要进一步校对与调整参数
Module      UNO
VCC   ——   5V
GND——   GND
VO    ——   A0
*/

float distance[] = {20, 30, 40, 50, 60, 70, 80, 90, 100,110,120,130,140,150};
float voltage = {2.5, 2, 1.55, 1.25, 1.1, 0.85, 0.8, 0.73, 0.7, 0.65, 0.6, 0.5, 0.45, 0.4};

typedef struct {
float maxDistance;//cm
float minDistance;//cm
float offset; //cm
float distance; //cm,
int frequency;//Hz
int pin;
}SHARP;

SHARP Sharp = {150, 20, 0, 0, 20, A0};

void getDistance(SHARP* Sharp) {
float v = analogRead(Sharp->pin);
v = v / 1024.0 * 5;
int index = 0;
for(index = 0; index < 14; index++) {
    if(v >= voltage) {
      break;
    }
}
if(index == 0) {
    Sharp->distance = 20;
} else if(index == 14) {
    Sharp->distance = 150;
} else {
    Sharp->distance = map(v, voltage, voltage, distance, distance);
}
}

void setup() {
Serial.begin(115200);
}

void loop() {
static unsigned long lastTime = millis();
if(millis() - lastTime > 1000/Sharp.frequency) {
    lastTime = millis();
    getDistance(&Sharp);
    Serial.println(Sharp.distance);
//    int v = analogRead(Sharp.pin);
//    Serial.println(v);
}
}

eagler8 发表于 2019-10-6 18:57:28

eagler8 发表于 2019-10-6 19:24:35

/*
【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验一百三十三:夏普GP2Y0A02YK0F 红外测距传感器模块 20-150cm 距离传感器
库地址:https://github.com/guillaume-rico/SharpIR
项目:测试距离的方案之四
Module      UNO
VCC   ——   5V
GND——   GND
VO    ——   A0
*/

#include <SharpIR.h>

#define ir A0    //模拟输入引脚
#define model 20150    //传感器型号
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:1080 for GP2Y0A21Y
//                                          20150 for GP2Y0A02Y
//                                          100500 for GP2Y0A710K0F
//                                          430   forGP2YA41SK0F
//                                          (working distance range according to the datasheets)

SharpIR SharpIR(ir, model);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
delay(500);

unsigned long pepe1 = millis(); // takes the time before the loop on the library begins

int dis = SharpIR.distance(); // this returns the distance to the object you're measuring


Serial.print("Mean distance: ");// returns it to the serial monitor
Serial.println(dis);

unsigned long pepe2 = millis() - pepe1; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(pepe2);
}

eagler8 发表于 2019-10-6 19:26:26

eagler8 发表于 2019-10-6 19:44:08

eagler8 发表于 2019-10-6 20:02:39

页: 1 [2]
查看完整版本: 【Arduino】108种传感器系列实验(133)---GP2Y0A02YK0F红外测距