Last active
February 11, 2019 06:27
-
-
Save penpencool/b36c9c14403c426c48ccb662d79f35c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ลิงค์โคดนี้มาจากร้าน ArduinoAll.com เท่านั้น | |
// ถ้าเปิดมาจากร้านอื่น แสดงว่าก็อปปี้ ละเมิดลิขสิทธิ์ มาจากร้าน ArduinoAll.com | |
/* | |
Example By ArduinoAll.com | |
ตัวอย่างการต่อสายสำหรับบอร์ด Arduino Uno | |
VCC - 3.3V | |
GND - GND | |
SDA - A4 | |
SCL - A5 | |
*/ | |
#include <Wire.h> | |
#include <ClosedCube_OPT3001.h> | |
ClosedCube_OPT3001 opt3001; | |
#define OPT3001_ADDRESS 0x44 // กำหนดตำแหน่ง I2C Address | |
void setup() | |
{ | |
Serial.begin(9600); | |
Serial.println("ArduinoAll OPT3001 Arduino Test"); | |
opt3001.begin(OPT3001_ADDRESS); | |
Serial.print("OPT3001 Manufacturer ID"); | |
Serial.println(opt3001.readManufacturerID()); | |
Serial.print("OPT3001 Device ID"); | |
Serial.println(opt3001.readDeviceID()); | |
configureSensor(); | |
printResult("High-Limit", opt3001.readHighLimit()); | |
printResult("Low-Limit", opt3001.readLowLimit()); | |
Serial.println("----"); | |
} | |
void loop() | |
{ | |
OPT3001 result = opt3001.readResult(); // อ่านค่าจากเซนเซอร์ | |
printResult("OPT3001", result); // แสดงค่าจากเซนเซอร์ | |
delay(1000); | |
} | |
// ส่วนของฟังก์ชั่นตั้งค่าต่าง ๆ | |
void configureSensor() { | |
OPT3001_Config newConfig; | |
newConfig.RangeNumber = B1100; | |
newConfig.ConvertionTime = B0; | |
newConfig.Latch = B1; | |
newConfig.ModeOfConversionOperation = B11; | |
OPT3001_ErrorCode errorConfig = opt3001.writeConfig(newConfig); | |
if (errorConfig != NO_ERROR) | |
printError("OPT3001 configuration", errorConfig); | |
else { | |
OPT3001_Config sensorConfig = opt3001.readConfig(); | |
Serial.println("OPT3001 Current Config:"); | |
Serial.println("------------------------------"); | |
Serial.print("Conversion ready (R):"); | |
Serial.println(sensorConfig.ConversionReady, HEX); | |
Serial.print("Conversion time (R/W):"); | |
Serial.println(sensorConfig.ConvertionTime, HEX); | |
Serial.print("Fault count field (R/W):"); | |
Serial.println(sensorConfig.FaultCount, HEX); | |
Serial.print("Flag high field (R-only):"); | |
Serial.println(sensorConfig.FlagHigh, HEX); | |
Serial.print("Flag low field (R-only):"); | |
Serial.println(sensorConfig.FlagLow, HEX); | |
Serial.print("Latch field (R/W):"); | |
Serial.println(sensorConfig.Latch, HEX); | |
Serial.print("Mask exponent field (R/W):"); | |
Serial.println(sensorConfig.MaskExponent, HEX); | |
Serial.print("Mode of conversion operation (R/W):"); | |
Serial.println(sensorConfig.ModeOfConversionOperation, HEX); | |
Serial.print("Polarity field (R/W):"); | |
Serial.println(sensorConfig.Polarity, HEX); | |
Serial.print("Overflow flag (R-only):"); | |
Serial.println(sensorConfig.OverflowFlag, HEX); | |
Serial.print("Range number (R/W):"); | |
Serial.println(sensorConfig.RangeNumber, HEX); | |
Serial.println("------------------------------"); | |
} | |
} | |
void printResult(String text, OPT3001 result) { | |
if (result.error == NO_ERROR) { | |
Serial.print(text); | |
Serial.print(": "); | |
Serial.print(result.lux); | |
Serial.println(" lux"); | |
} | |
else { | |
printError(text, result.error); | |
} | |
} | |
void printError(String text, OPT3001_ErrorCode error) { | |
Serial.print(text); | |
Serial.print(": [ERROR] Code #"); | |
Serial.println(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment