求大神看看Arduino代码 是关于GY-30光照强度传感器的代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求大神看看Arduino代码 是关于GY-30光照强度传感器的代码相关的知识,希望对你有一定的参考价值。
如题,GY-30除了电源口还有三个口 分别是 SCL时钟线 SDA数据线 ADDR地址引脚。求告知代码中这三个口分别连接到Arduino的是哪个端口?
#include <Wire.h>
// GY-30
// BH1750FVI
// in ADDR 'L' mode 7bit addr
#define ADDR 0b0100011
// addr 'H' mode
// #define ADDR 0b1011100
void setup()
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
Wire.begin();
pinMode(13, OUTPUT);
Wire.beginTransmission(ADDR);
Wire.write(0b00000001);
Wire.endTransmission();
void loop()
// put your main code here, to run repeatedly:
int val = 0;
// reset
Wire.beginTransmission(ADDR);
Wire.write(0b00000111);
Wire.endTransmission();
digitalWrite(13, LOW);
delay(100);
Wire.beginTransmission(ADDR);
Wire.write(0b00100000);
Wire.endTransmission();
// typical read delay 120ms
delay(120);
Wire.requestFrom(ADDR, 2); // 2byte every time
for (val=0; Wire.available()>=1; )
char c = Wire.read();
//Serial.println(c, HEX);
val = (val << 8) + (c & 0xFF);
val = val / 1.2;
Serial.print("lx: ");
Serial.println(val);
Serial.println("OK");
digitalWrite(13, HIGH);
delay(100);
如果你用其他版本的arduino需要看一下上面哪个是i2c口 参考技术A 一般是SDA 接 A4 ; SCL接A5 ; ADDR接GND
c++求大神帮我看看为啥这个程序运行不了?关于图形工厂总是出现redefinition of 'class BaseShape'
源程序代码如下:
// BaseShape.h文件
#include <iostream>
using namespace std;
class BaseShape
public:
BaseShape() ;
virtual ~BaseShape() ;
virtual void DrawShape() = 0;
;
//*********************************
//ShapeFactory.h文件
#include <fstream>
#include <string>
#include "Line.h"
#include "BaseShape.h"
//定义工厂类
class ShapeFactory
public:
ShapeFactory();
~ShapeFactory();
//BaseShape* SelectShape(string);
BaseShape* SelectShape(string shape)
if(shape=="Line" || shape=="线")return new Line();
else cout<< "输入的图形名称错误!" <<endl;
;
// ShapeFactory.cpp文件
#include "ShapeFactory.h"
ShapeFactory::ShapeFactory(void)
ShapeFactory::~ShapeFactory(void)
//********************************************
// Line.h文件
///////////////////直线Line/////////////////////
#include "BaseShape.h"
class Line : public BaseShape
private:
int n;
static int L;
public:
Line() ;
virtual ~Line() ;
//构造函数成员初始化列表,构造函数之后,函数体之前。
Line ( int n) : n(n)
//覆盖抽象类的纯虚函数
virtualvoid DrawShape();
;
// Line.cpp文件
#include "Line.h"
//绘制直线
void Line ::DrawShape()
int n;
static int L;
cout<< "请选择直线长度: " <<endl;
cin>> n;
for(int i=0; i<n; i++)
cout<< "*" ;
L++;
cout<<endl<< "^_^ 您已经绘制"<< L << "条直线^_^ "<<endl;
//********************************************
**********************************************
*********************************************
#include <fstream>
#include <vector>
#include <string>
#include "Line.h"
#include "BaseShape.h"
#include "ShapeFactory.h"
#include <iostream>
using namespace std;
int main()
/*
读infile类 的文件ShapeFactoryFile.txt中可绘制的图形并为每行加上"行号-",输出到运行屏幕上。
*/
ifstream infile("ShapeFactoryFile.txt");
if(!infile) //检验文件是否成功打开
cout<< "sorry! Unable to open ShapeFactoryFile. " <<endl;
return -1;
string line;
vector<string> text;
while (getline(infile,line))
text.push_back(line);
for(int j=0; j<text.size(); j++)
cout<< j+1 << " - " << text[j] <<endl;
cout<<endl << "*************以上是可以绘制的图形名***************" <<endl<<endl;
//定义工厂对象、基类指针
ShapeFactory factory;
BaseShape * pBaseShape;
string shape;
int t=0;
while(true)
cout<<"请输入图形(英文或中文)名称(以#为结束程序): ";
cin>>shape;
if(shape=="#") break;
BaseShape* pBaseShape=factory.SelectShape(shape);//选择具体类
pBaseShape->DrawShape();//调用具体类的画法
delete pBaseShape;
t++;
cout<< "您总共已经输出"<< t <<"个图形。" <<endl;
return 0;
每个类的头文件最开始都加上
#pragma once本回答被提问者采纳
以上是关于求大神看看Arduino代码 是关于GY-30光照强度传感器的代码的主要内容,如果未能解决你的问题,请参考以下文章
c++求大神帮我看看为啥这个程序运行不了?关于图形工厂总是出现redefinition of 'class BaseShape'