jj

Dependencies:   VL6180x

Committer:
the_grudge
Date:
Wed May 12 13:56:16 2021 +0000
Revision:
0:ff03c2bb4518
eee

Who changed what in which revision?

UserRevisionLine numberNew contents of line
the_grudge 0:ff03c2bb4518 1 #include "mbed.h"
the_grudge 0:ff03c2bb4518 2 #include "stdint.h"
the_grudge 0:ff03c2bb4518 3 #include <VL6180x.h>
the_grudge 0:ff03c2bb4518 4 #define VL6180X_ADDRESS 0x29
the_grudge 0:ff03c2bb4518 5 VL6180xIdentification identification;
the_grudge 0:ff03c2bb4518 6 Serial pc(PC_10,PC_11);
the_grudge 0:ff03c2bb4518 7 // mbed uses 8bit addresses shift address by 1 bit left
the_grudge 0:ff03c2bb4518 8 VL6180x sensor(A4, A5, VL6180X_ADDRESS<<1);//距离传感器
the_grudge 0:ff03c2bb4518 9
the_grudge 0:ff03c2bb4518 10 AnalogIn switch1(PA_0); //光敏传感器
the_grudge 0:ff03c2bb4518 11
the_grudge 0:ff03c2bb4518 12 I2C i2c(I2C_SDA, I2C_SCL); //颜色传感器
the_grudge 0:ff03c2bb4518 13 int sensor_addr = 41 << 1;
the_grudge 0:ff03c2bb4518 14 DigitalOut green(LED1);
the_grudge 0:ff03c2bb4518 15
the_grudge 0:ff03c2bb4518 16 void ret_rgb(int r,int g,int b) //该函数用于返回颜色值
the_grudge 0:ff03c2bb4518 17 {
the_grudge 0:ff03c2bb4518 18
the_grudge 0:ff03c2bb4518 19 if (r > g*1.3 && r > b*1.3)
the_grudge 0:ff03c2bb4518 20 {
the_grudge 0:ff03c2bb4518 21 pc.printf("red\n");
the_grudge 0:ff03c2bb4518 22 }
the_grudge 0:ff03c2bb4518 23 else if (g > r*1.3 && g > b*1.3)
the_grudge 0:ff03c2bb4518 24 {
the_grudge 0:ff03c2bb4518 25 pc.printf("green\n");
the_grudge 0:ff03c2bb4518 26 }
the_grudge 0:ff03c2bb4518 27 else if (b > r*1.3 && b > g*1.3)
the_grudge 0:ff03c2bb4518 28 {
the_grudge 0:ff03c2bb4518 29 pc.printf("blue\n");
the_grudge 0:ff03c2bb4518 30 }
the_grudge 0:ff03c2bb4518 31 else
the_grudge 0:ff03c2bb4518 32 pc.printf("no\n");
the_grudge 0:ff03c2bb4518 33 wait(0.1);
the_grudge 0:ff03c2bb4518 34 }
the_grudge 0:ff03c2bb4518 35 void bgr()
the_grudge 0:ff03c2bb4518 36 {
the_grudge 0:ff03c2bb4518 37 //打开颜色传感器
the_grudge 0:ff03c2bb4518 38 int r,g,b;
the_grudge 0:ff03c2bb4518 39 green = 1; // off
the_grudge 0:ff03c2bb4518 40 pc.baud(115200);
the_grudge 0:ff03c2bb4518 41 // Connect to the Color sensor and verify whether we connected to the correct sensor.
the_grudge 0:ff03c2bb4518 42
the_grudge 0:ff03c2bb4518 43 i2c.frequency(100000);
the_grudge 0:ff03c2bb4518 44
the_grudge 0:ff03c2bb4518 45 char id_regval[1] = {146};
the_grudge 0:ff03c2bb4518 46 char data[1] = {0};
the_grudge 0:ff03c2bb4518 47 i2c.write(sensor_addr,id_regval,1, true);
the_grudge 0:ff03c2bb4518 48 i2c.read(sensor_addr,data,1,false);
the_grudge 0:ff03c2bb4518 49
the_grudge 0:ff03c2bb4518 50 if (data[0]==68) {
the_grudge 0:ff03c2bb4518 51 green = 0;
the_grudge 0:ff03c2bb4518 52 wait (2);
the_grudge 0:ff03c2bb4518 53 green = 1;
the_grudge 0:ff03c2bb4518 54 } else {
the_grudge 0:ff03c2bb4518 55 green = 1;
the_grudge 0:ff03c2bb4518 56 }
the_grudge 0:ff03c2bb4518 57
the_grudge 0:ff03c2bb4518 58 // Initialize color sensor
the_grudge 0:ff03c2bb4518 59
the_grudge 0:ff03c2bb4518 60 char timing_register[2] = {129,0};
the_grudge 0:ff03c2bb4518 61 i2c.write(sensor_addr,timing_register,2,false);
the_grudge 0:ff03c2bb4518 62
the_grudge 0:ff03c2bb4518 63 char control_register[2] = {143,0};
the_grudge 0:ff03c2bb4518 64 i2c.write(sensor_addr,control_register,2,false);
the_grudge 0:ff03c2bb4518 65
the_grudge 0:ff03c2bb4518 66 char enable_register[2] = {128,3};
the_grudge 0:ff03c2bb4518 67 i2c.write(sensor_addr,enable_register,2,false);
the_grudge 0:ff03c2bb4518 68
the_grudge 0:ff03c2bb4518 69 // Read data from color sensor (Clear/Red/Green/Blue)
the_grudge 0:ff03c2bb4518 70
the_grudge 0:ff03c2bb4518 71
the_grudge 0:ff03c2bb4518 72 char clear_reg[1] = {148};
the_grudge 0:ff03c2bb4518 73 char clear_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 74 i2c.write(sensor_addr,clear_reg,1, true);
the_grudge 0:ff03c2bb4518 75 i2c.read(sensor_addr,clear_data,2, false);
the_grudge 0:ff03c2bb4518 76
the_grudge 0:ff03c2bb4518 77 int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
the_grudge 0:ff03c2bb4518 78
the_grudge 0:ff03c2bb4518 79 char red_reg[1] = {150};
the_grudge 0:ff03c2bb4518 80 char red_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 81 i2c.write(sensor_addr,red_reg,1, true);
the_grudge 0:ff03c2bb4518 82 i2c.read(sensor_addr,red_data,2, false);
the_grudge 0:ff03c2bb4518 83
the_grudge 0:ff03c2bb4518 84 int red_value = ((int)red_data[1] << 8) | red_data[0];
the_grudge 0:ff03c2bb4518 85
the_grudge 0:ff03c2bb4518 86 char green_reg[1] = {152};
the_grudge 0:ff03c2bb4518 87 char green_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 88 i2c.write(sensor_addr,green_reg,1, true);
the_grudge 0:ff03c2bb4518 89 i2c.read(sensor_addr,green_data,2, false);
the_grudge 0:ff03c2bb4518 90
the_grudge 0:ff03c2bb4518 91 int green_value = ((int)green_data[1] << 8) | green_data[0];
the_grudge 0:ff03c2bb4518 92
the_grudge 0:ff03c2bb4518 93 char blue_reg[1] = {154};
the_grudge 0:ff03c2bb4518 94 char blue_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 95 i2c.write(sensor_addr,blue_reg,1, true);
the_grudge 0:ff03c2bb4518 96 i2c.read(sensor_addr,blue_data,2, false);
the_grudge 0:ff03c2bb4518 97
the_grudge 0:ff03c2bb4518 98 int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
the_grudge 0:ff03c2bb4518 99
the_grudge 0:ff03c2bb4518 100 // print sensor readings
the_grudge 0:ff03c2bb4518 101
the_grudge 0:ff03c2bb4518 102 //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value);
the_grudge 0:ff03c2bb4518 103 wait(0.5);
the_grudge 0:ff03c2bb4518 104
the_grudge 0:ff03c2bb4518 105 //颜色传感器显示数据
the_grudge 0:ff03c2bb4518 106 r = red_value;
the_grudge 0:ff03c2bb4518 107 g = green_value;
the_grudge 0:ff03c2bb4518 108 b = blue_value;
the_grudge 0:ff03c2bb4518 109 pc.printf("R %d G %d B %d\n ",r,g,b);
the_grudge 0:ff03c2bb4518 110 ret_rgb(r,g,b);
the_grudge 0:ff03c2bb4518 111
the_grudge 0:ff03c2bb4518 112 }
the_grudge 0:ff03c2bb4518 113
the_grudge 0:ff03c2bb4518 114
the_grudge 0:ff03c2bb4518 115 void color_read()
the_grudge 0:ff03c2bb4518 116 {
the_grudge 0:ff03c2bb4518 117 //打开颜色传感器
the_grudge 0:ff03c2bb4518 118 int r,g,b;
the_grudge 0:ff03c2bb4518 119 green = 1; // off
the_grudge 0:ff03c2bb4518 120 pc.baud(115200);
the_grudge 0:ff03c2bb4518 121 // Connect to the Color sensor and verify whether we connected to the correct sensor.
the_grudge 0:ff03c2bb4518 122
the_grudge 0:ff03c2bb4518 123 i2c.frequency(100000);
the_grudge 0:ff03c2bb4518 124
the_grudge 0:ff03c2bb4518 125 char id_regval[1] = {146};
the_grudge 0:ff03c2bb4518 126 char data[1] = {0};
the_grudge 0:ff03c2bb4518 127 i2c.write(sensor_addr,id_regval,1, true);
the_grudge 0:ff03c2bb4518 128 i2c.read(sensor_addr,data,1,false);
the_grudge 0:ff03c2bb4518 129
the_grudge 0:ff03c2bb4518 130 if (data[0]==68) {
the_grudge 0:ff03c2bb4518 131 green = 0;
the_grudge 0:ff03c2bb4518 132 wait (2);
the_grudge 0:ff03c2bb4518 133 green = 1;
the_grudge 0:ff03c2bb4518 134 } else {
the_grudge 0:ff03c2bb4518 135 green = 1;
the_grudge 0:ff03c2bb4518 136 }
the_grudge 0:ff03c2bb4518 137
the_grudge 0:ff03c2bb4518 138 // Initialize color sensor
the_grudge 0:ff03c2bb4518 139
the_grudge 0:ff03c2bb4518 140 char timing_register[2] = {129,0};
the_grudge 0:ff03c2bb4518 141 i2c.write(sensor_addr,timing_register,2,false);
the_grudge 0:ff03c2bb4518 142
the_grudge 0:ff03c2bb4518 143 char control_register[2] = {143,0};
the_grudge 0:ff03c2bb4518 144 i2c.write(sensor_addr,control_register,2,false);
the_grudge 0:ff03c2bb4518 145
the_grudge 0:ff03c2bb4518 146 char enable_register[2] = {128,3};
the_grudge 0:ff03c2bb4518 147 i2c.write(sensor_addr,enable_register,2,false);
the_grudge 0:ff03c2bb4518 148
the_grudge 0:ff03c2bb4518 149 // Read data from color sensor (Clear/Red/Green/Blue)
the_grudge 0:ff03c2bb4518 150
the_grudge 0:ff03c2bb4518 151 while (true) {
the_grudge 0:ff03c2bb4518 152 char clear_reg[1] = {148};
the_grudge 0:ff03c2bb4518 153 char clear_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 154 i2c.write(sensor_addr,clear_reg,1, true);
the_grudge 0:ff03c2bb4518 155 i2c.read(sensor_addr,clear_data,2, false);
the_grudge 0:ff03c2bb4518 156
the_grudge 0:ff03c2bb4518 157 int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
the_grudge 0:ff03c2bb4518 158
the_grudge 0:ff03c2bb4518 159 char red_reg[1] = {150};
the_grudge 0:ff03c2bb4518 160 char red_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 161 i2c.write(sensor_addr,red_reg,1, true);
the_grudge 0:ff03c2bb4518 162 i2c.read(sensor_addr,red_data,2, false);
the_grudge 0:ff03c2bb4518 163
the_grudge 0:ff03c2bb4518 164 int red_value = ((int)red_data[1] << 8) | red_data[0];
the_grudge 0:ff03c2bb4518 165
the_grudge 0:ff03c2bb4518 166 char green_reg[1] = {152};
the_grudge 0:ff03c2bb4518 167 char green_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 168 i2c.write(sensor_addr,green_reg,1, true);
the_grudge 0:ff03c2bb4518 169 i2c.read(sensor_addr,green_data,2, false);
the_grudge 0:ff03c2bb4518 170
the_grudge 0:ff03c2bb4518 171 int green_value = ((int)green_data[1] << 8) | green_data[0];
the_grudge 0:ff03c2bb4518 172
the_grudge 0:ff03c2bb4518 173 char blue_reg[1] = {154};
the_grudge 0:ff03c2bb4518 174 char blue_data[2] = {0,0};
the_grudge 0:ff03c2bb4518 175 i2c.write(sensor_addr,blue_reg,1, true);
the_grudge 0:ff03c2bb4518 176 i2c.read(sensor_addr,blue_data,2, false);
the_grudge 0:ff03c2bb4518 177
the_grudge 0:ff03c2bb4518 178 int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
the_grudge 0:ff03c2bb4518 179
the_grudge 0:ff03c2bb4518 180 // print sensor readings
the_grudge 0:ff03c2bb4518 181
the_grudge 0:ff03c2bb4518 182 //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value);
the_grudge 0:ff03c2bb4518 183 wait(0.5);
the_grudge 0:ff03c2bb4518 184
the_grudge 0:ff03c2bb4518 185 //颜色传感器显示数据
the_grudge 0:ff03c2bb4518 186 r = red_value;
the_grudge 0:ff03c2bb4518 187 g = green_value;
the_grudge 0:ff03c2bb4518 188 b = blue_value;
the_grudge 0:ff03c2bb4518 189 pc.printf("R %d G %d B %d\n ",r,g,b);
the_grudge 0:ff03c2bb4518 190 ret_rgb(r,g,b);
the_grudge 0:ff03c2bb4518 191 }
the_grudge 0:ff03c2bb4518 192 }
the_grudge 0:ff03c2bb4518 193
the_grudge 0:ff03c2bb4518 194 void light_read(){
the_grudge 0:ff03c2bb4518 195 float s1;
the_grudge 0:ff03c2bb4518 196 pc.baud(115200);
the_grudge 0:ff03c2bb4518 197 while(1)
the_grudge 0:ff03c2bb4518 198 {
the_grudge 0:ff03c2bb4518 199
the_grudge 0:ff03c2bb4518 200 s1 = switch1.read();
the_grudge 0:ff03c2bb4518 201 s1 = s1*5000;
the_grudge 0:ff03c2bb4518 202 pc.printf("measure = %.0f\n",s1);
the_grudge 0:ff03c2bb4518 203 wait(0.3);
the_grudge 0:ff03c2bb4518 204
the_grudge 0:ff03c2bb4518 205 }
the_grudge 0:ff03c2bb4518 206 }
the_grudge 0:ff03c2bb4518 207
the_grudge 0:ff03c2bb4518 208 void distance_read(){
the_grudge 0:ff03c2bb4518 209 uint8_t retaddr;
the_grudge 0:ff03c2bb4518 210 pc.baud(115200);
the_grudge 0:ff03c2bb4518 211 wait_ms(100); // delay .1s
the_grudge 0:ff03c2bb4518 212 sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
the_grudge 0:ff03c2bb4518 213 if(sensor.VL6180xInit() != 0) {
the_grudge 0:ff03c2bb4518 214 printf("FAILED TO INITALIZE\n"); //Initialize device and check for errors
the_grudge 0:ff03c2bb4518 215 };
the_grudge 0:ff03c2bb4518 216
the_grudge 0:ff03c2bb4518 217 sensor.VL6180xDefautSettings(); //Load default settings to get started.
the_grudge 0:ff03c2bb4518 218
the_grudge 0:ff03c2bb4518 219 wait(1);
the_grudge 0:ff03c2bb4518 220 retaddr=sensor.changeAddress(0x29,0x27);
the_grudge 0:ff03c2bb4518 221 sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
the_grudge 0:ff03c2bb4518 222
the_grudge 0:ff03c2bb4518 223 wait(1);
the_grudge 0:ff03c2bb4518 224 retaddr=sensor.changeAddress(0x27,0x29);
the_grudge 0:ff03c2bb4518 225 sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
the_grudge 0:ff03c2bb4518 226
the_grudge 0:ff03c2bb4518 227 while(1) {
the_grudge 0:ff03c2bb4518 228
the_grudge 0:ff03c2bb4518 229
the_grudge 0:ff03c2bb4518 230 //Get Distance and report in mm
the_grudge 0:ff03c2bb4518 231 printf("Distance measured (mm) = ");
the_grudge 0:ff03c2bb4518 232 printf("%d\n", sensor.getDistance() );
the_grudge 0:ff03c2bb4518 233
the_grudge 0:ff03c2bb4518 234 wait_ms(500);
the_grudge 0:ff03c2bb4518 235
the_grudge 0:ff03c2bb4518 236 }
the_grudge 0:ff03c2bb4518 237
the_grudge 0:ff03c2bb4518 238 }
the_grudge 0:ff03c2bb4518 239
the_grudge 0:ff03c2bb4518 240 int main(){
the_grudge 0:ff03c2bb4518 241 char order = pc.getc();
the_grudge 0:ff03c2bb4518 242 printf("welcome to the matainance system :\n");
the_grudge 0:ff03c2bb4518 243 switch(order){
the_grudge 0:ff03c2bb4518 244 case 'a'://颜色传感器
the_grudge 0:ff03c2bb4518 245 color_read();
the_grudge 0:ff03c2bb4518 246 break;
the_grudge 0:ff03c2bb4518 247
the_grudge 0:ff03c2bb4518 248 case 'b'://距离传感器
the_grudge 0:ff03c2bb4518 249 distance_read();
the_grudge 0:ff03c2bb4518 250 break;
the_grudge 0:ff03c2bb4518 251
the_grudge 0:ff03c2bb4518 252 case 'c'://光敏传感器
the_grudge 0:ff03c2bb4518 253 light_read();
the_grudge 0:ff03c2bb4518 254 break;
the_grudge 0:ff03c2bb4518 255
the_grudge 0:ff03c2bb4518 256 case 'd'://led闪烁
the_grudge 0:ff03c2bb4518 257 for(int p=0;p<10;p++){
the_grudge 0:ff03c2bb4518 258 green = ~green;
the_grudge 0:ff03c2bb4518 259 }
the_grudge 0:ff03c2bb4518 260 break;
the_grudge 0:ff03c2bb4518 261
the_grudge 0:ff03c2bb4518 262 case 'e':
the_grudge 0:ff03c2bb4518 263 pc.printf("00\n");
the_grudge 0:ff03c2bb4518 264 break;
the_grudge 0:ff03c2bb4518 265
the_grudge 0:ff03c2bb4518 266 case 'f':
the_grudge 0:ff03c2bb4518 267 pc.printf("01\n");
the_grudge 0:ff03c2bb4518 268 break;
the_grudge 0:ff03c2bb4518 269
the_grudge 0:ff03c2bb4518 270 case 'g':
the_grudge 0:ff03c2bb4518 271 pc.printf("10\n");
the_grudge 0:ff03c2bb4518 272 break;
the_grudge 0:ff03c2bb4518 273
the_grudge 0:ff03c2bb4518 274 case 'h':
the_grudge 0:ff03c2bb4518 275 pc.printf("11\n");
the_grudge 0:ff03c2bb4518 276 break;
the_grudge 0:ff03c2bb4518 277
the_grudge 0:ff03c2bb4518 278 }
the_grudge 0:ff03c2bb4518 279 }