找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4059|回复: 2
打印 上一主题 下一主题
收起左侧

发一个利用两个IIC数字光传感器(BH1750),进行平面定位

[复制链接]
跳转到指定楼层
楼主
ID:125150 发表于 2018-3-31 16:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
看了去年电赛,在网上查呢下,很少有资料分享,最近刚好在做可见光定位,所以先将写的两个IIC数字光传感器(BH1750),采用差分方式进行定位程序分享下,还在改进中,完成后将全部开放!(用的是arduino UNOd的板子)

arduino源程序如下:
  1. #include <Wire.h> //BH1750 IIC Mode
  2. #include <math.h>
  3. #include <SPI.h>  
  4. #include <Wire.h>  
  5. #include <Adafruit_GFX.h>  
  6. #include <Adafruit_SSD1306.h>  
  7. int potpin=0;
  8. int a;//定义模拟接口0
  9. int now;
  10. int BH1750address0 = 0x5c; //setting i2c address
  11. int BH1750address1 = 0x23;
  12. uint16_t val0=0;
  13.   uint16_t val1=0;
  14. byte buff0[2];
  15. byte buff1[2];
  16. #define OLED_RESET 4  
  17. Adafruit_SSD1306 display(OLED_RESET);  
  18.   
  19. static const unsigned char PROGMEM logo[] =  
  20. {   

  21. /*--  调入了一幅图像:这是您新建的图像  --*/
  22. /*--  宽度x高度=128x64  --*/
  23. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  29. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30. 0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31. 0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32. 0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33. 0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  34. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  54. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  55. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  56. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  57. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  58. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  59. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  60. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  61. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  63. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  64. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  65. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  66. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  67. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  68. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  69. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  70. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  71. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  72. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  73. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  74. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  75. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  76. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  77. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  78. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  79. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  80. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  81. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  82. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  83. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  84. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  85. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  86. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

  87. };  
  88.    
  89. #if (SSD1306_LCDHEIGHT != 64)  
  90. #error("Height incorrect, please fix Adafruit_SSD1306.h!");  
  91. #endif  
  92.   int outputValue,outputValue1;
  93. void setup()
  94. {  outputValue=0;
  95.   outputValue1=0;      
  96.   Wire.begin();
  97.   Serial.begin(19200);
  98.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);   
  99.   display.clearDisplay();  
  100. }

  101. void loop()
  102. {a=analogRead(potpin);//读取模拟接口0 的值,
  103.   int i,j;
  104. // uint16_t val0=0;
  105. // uint16_t val1=0;
  106.   BH1750_Init(BH1750address0);
  107.   BH1750_Init(BH1750address1);
  108.   delay(200);

  109.   if(2==BH1750_Read0(BH1750address0))
  110.   {
  111.     val0=((buff0[0]<<8)|buff0[1])/1.2;
  112.      Serial.print(now);
  113.   Serial.print( ",");
  114.     Serial.print(val0,DEC);     
  115.     Serial.print(",");
  116.       Serial.print(val1,DEC);     
  117.    Serial.print(",");
  118.        Serial.print(val1,DEC);     
  119.     Serial.print(";");
  120.   }
  121.   delay(10);

  122. if(2==BH1750_Read1(BH1750address1))
  123.   {
  124.     val1=((buff1[0]<<8)|buff1[1])/1.2;
  125.      Serial.print(now);
  126.   Serial.print( ",");
  127.     Serial.print(val0,DEC);     
  128.    Serial.print(",");
  129. Serial.print(val1,DEC);     
  130.     Serial.print(",");
  131.     Serial.print(val1,DEC);     
  132.     Serial.print(";");
  133.    
  134.   }
  135.   delay(10);
  136.   
  137.    outputValue = map(val0, 0, 700, 0, 64);
  138.     outputValue1 = map(val1, 0, 700, 0, 128);
  139.    display.drawBitmap(outputValue1,outputValue , logo, 128, 64,1);  //在坐标X0,Y0的位置开始显示图片  
  140.                                                //128是取模软件显示的宽度  
  141.                                                //64是取模软件显示的高度  
  142. display.display();                           //把缓存的都显示  
  143.   delay(1);  
  144.    
  145.   display.clearDisplay();    //清除屏幕  
  146.   delay(10);
  147. }

  148. int BH1750_Read0(int address) //
  149. {
  150.   int i=0;
  151.   Wire.beginTransmission(address);
  152.   Wire.requestFrom(address, 2);
  153.   while(Wire.available()) //
  154.   {
  155.     buff0[i] = Wire.read();  // receive one byte
  156.     i++;
  157.   }
  158.     Wire.endTransmission();  
  159.     return i;
  160. }

  161. int BH1750_Read1(int address) //
  162. {
  163.   int j=0;
  164.   Wire.beginTransmission(address);
  165.   Wire.requestFrom(address, 2);
  166.   while(Wire.available()) //
  167.   {
  168.     buff1[j] = Wire.read();  // receive one byte
  169.     j++;
  170.   }
  171.   Wire.endTransmission();  
  172.   return j;
  173. }

  174. void BH1750_Init(int address)
  175. {
  176.   Wire.beginTransmission(address);
  177.   Wire.write(0x10);//1lx reolution 120ms
  178.   Wire.endTransmission();
  179. }

复制代码

下载:
bh1750s.rar (1.32 KB, 下载次数: 22)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:454017 发表于 2019-4-14 10:19 来自手机 | 只看该作者
楼主做完了吗?我用51单片机去做,速度赶不赶得上
回复

使用道具 举报

板凳
ID:560595 发表于 2019-6-28 15:55 | 只看该作者
楼主做完了吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表