/* I2C Light Sensor
The circuit:
* analog sensors on analog ins PC0, PC1, and PC2
* SD card attached to SPI bus as follows:
** MOSI/CMD - PB3
** MISO/DAT0 - PB4
** CLK - PB5
** CS/CD/DAT3 - PB2 (10)
*/
#include <Wire.h>
#include <math.h>
#include <SD.h>
#define chipSelect 10
#define address 0x44 // A0 = L
#define SENSE_VIS 0
#define SENSE_IR 1
#define LIGHT_AUTORANGE 0
#define LIGHT_RANGE1 1
#define LIGHT_RANGE2 2
#define LIGHT_RANGE3 3
#define LIGHT_RANGE4 4
int count=0;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
// I2C pins PD4, PD5
//
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
int data = 0;
int light_sensor_setup;
void led_blink()
{
digitalWrite(3, HIGH); // set the LED on
delay(500);
digitalWrite(3, LOW); // set the LED off
delay(500);
}
int set_light_sensor(int mode)
{
int command;
switch (mode)
{
case SENSE_VIS:
{
command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1)
break;
}
case SENSE_IR:
{
command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx])
break;
}
default:
return 3;
}
// Setup device
Wire.beginTransmission(address);
Wire.send(0x00); // sends address
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1)
Wire.endTransmission(); // stop transmitting
// Connect to device and set register address
Wire.beginTransmission(address);
Wire.send(0x00); // sends address (command register)
Wire.endTransmission(); // stop transmitting
// verify written command byte
Wire.beginTransmission(address);
Wire.requestFrom(address, 1);
if (command != Wire.receive())
{
return 4;
Serial.print(data, BIN);
}
Wire.endTransmission(); // stop transmitting
light_sensor_setup=command;
}
float get_light_measurement()
{
int ret=0;
// Connect to device and set register address
Wire.beginTransmission(address);
Wire.send(0x01); // sends address of LSB reagister
Wire.endTransmission(); // stop transmitting
// Connect to device and request one byte
Wire.beginTransmission(address);
Wire.requestFrom(address, 1);
ret = Wire.receive();
Wire.endTransmission(); // stop transmitting
// Connect to device and set register address
Wire.beginTransmission(address);
Wire.send(0x02); // sends address of MSB register
Wire.endTransmission(); // stop transmitting
// Connect to device and request one byte
Wire.beginTransmission(address);
Wire.requestFrom(address, 1);
ret +=256 * Wire.receive();
Wire.endTransmission(); // stop transmitting
return (1000.0/pow(2.0,16)*ret);
}
void loop()
{
float luxVIS=0;
float luxIR=0;
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring
led_blink(); // Delay for measurement
luxVIS=get_light_measurement();
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring
led_blink(); // Delay for measurement
luxIR=get_light_measurement();
// make a string for assembling the data to log:
String dataString = "$LOG";
dataString += String((int)count); // print number of measurement
dataString += ",";
dataString += String((int)luxVIS, DEC); // print number of measurement
dataString += ",";
dataString += String((int)luxIR, DEC); // print number of measurement
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
count++;
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}