mirror of
https://github.com/KevinMidboe/Arduino.git
synced 2025-10-29 17:40:11 +00:00
Init commit with many years of arduino sketches and projects. I dont know if the esp8266 includes much, but there are also libraries. I hope they dont have crazy automatic versioning through the Arduino IDE.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#ifndef _TSL2561_H_
|
||||
#define _TSL2561_H_
|
||||
|
||||
#define TSL2561_VISIBLE 2 // channel 0 - channel 1
|
||||
#define TSL2561_INFRARED 1 // channel 1
|
||||
#define TSL2561_FULLSPECTRUM 0 // channel 0
|
||||
|
||||
#define TSL2561_LUX_LUXSCALE (14) // Scale by 2^14
|
||||
#define TSL2561_LUX_RATIOSCALE (9) // Scale ratio by 2^9
|
||||
#define TSL2561_LUX_CHSCALE (10) // Scale channel values by 2^10
|
||||
#define TSL2561_LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE
|
||||
#define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE
|
||||
|
||||
// T, FN and CL package values
|
||||
#define TSL2561_LUX_K1T (0x0040) // 0.125 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B1T (0x01f2) // 0.0304 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M1T (0x01be) // 0.0272 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K2T (0x0080) // 0.250 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B2T (0x0214) // 0.0325 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M2T (0x02d1) // 0.0440 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K3T (0x00c0) // 0.375 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B3T (0x023f) // 0.0351 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M3T (0x037b) // 0.0544 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K4T (0x0100) // 0.50 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B4T (0x0270) // 0.0381 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M4T (0x03fe) // 0.0624 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K5T (0x0138) // 0.61 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B5T (0x016f) // 0.0224 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M5T (0x01fc) // 0.0310 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K6T (0x019a) // 0.80 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B6T (0x00d2) // 0.0128 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M6T (0x00fb) // 0.0153 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K7T (0x029a) // 1.3 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B7T (0x0018) // 0.00146 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M7T (0x0012) // 0.00112 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K8T (0x029a) // 1.3 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B8T (0x0000) // 0.000 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M8T (0x0000) // 0.000 * 2^LUX_SCALE
|
||||
|
||||
// Auto-gain thresholds
|
||||
#define TSL2561_AGC_THI_13MS (4850) // Max value at Ti 13ms = 5047
|
||||
#define TSL2561_AGC_TLO_13MS (100)
|
||||
#define TSL2561_AGC_THI_101MS (36000) // Max value at Ti 101ms = 37177
|
||||
#define TSL2561_AGC_TLO_101MS (200)
|
||||
#define TSL2561_AGC_THI_402MS (63000) // Max value at Ti 402ms = 65535
|
||||
#define TSL2561_AGC_TLO_402MS (500)
|
||||
|
||||
// Clipping thresholds
|
||||
#define TSL2561_CLIPPING_13MS (4900)
|
||||
#define TSL2561_CLIPPING_101MS (37000)
|
||||
#define TSL2561_CLIPPING_402MS (65000)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,100 @@
|
||||
// Sketch to explore the luminosity sensor TSL2561 (breakout board by Adafruit)
|
||||
|
||||
#define SDA_PORT PORTD
|
||||
#define SDA_PIN 3
|
||||
#define SCL_PORT PORTD
|
||||
#define SCL_PIN 5
|
||||
|
||||
#include <SoftI2CMaster.h>
|
||||
#include "TSL2561Soft.h"
|
||||
|
||||
#define ADDR 0x72
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
unsigned long computeLux(unsigned long channel0, unsigned long channel1){
|
||||
|
||||
/* Make sure the sensor isn't saturated! */
|
||||
uint16_t clipThreshold = TSL2561_CLIPPING_402MS;;
|
||||
|
||||
/* Return 0 lux if the sensor is saturated */
|
||||
if ((channel0 > clipThreshold) || (channel1 > clipThreshold))
|
||||
{
|
||||
Serial.println(F("Sensor is saturated"));
|
||||
return 32000;
|
||||
}
|
||||
|
||||
/* Find the ratio of the channel values (Channel1/Channel0) */
|
||||
unsigned long ratio1 = 0;
|
||||
if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0;
|
||||
|
||||
/* round the ratio value */
|
||||
unsigned long ratio = (ratio1 + 1) >> 1;
|
||||
|
||||
unsigned int b, m;
|
||||
|
||||
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T))
|
||||
{b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;}
|
||||
else if (ratio <= TSL2561_LUX_K2T)
|
||||
{b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;}
|
||||
else if (ratio <= TSL2561_LUX_K3T)
|
||||
{b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;}
|
||||
else if (ratio <= TSL2561_LUX_K4T)
|
||||
{b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;}
|
||||
else if (ratio <= TSL2561_LUX_K5T)
|
||||
{b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;}
|
||||
else if (ratio <= TSL2561_LUX_K6T)
|
||||
{b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;}
|
||||
else if (ratio <= TSL2561_LUX_K7T)
|
||||
{b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;}
|
||||
else if (ratio > TSL2561_LUX_K8T)
|
||||
{b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;}
|
||||
|
||||
unsigned long temp;
|
||||
temp = ((channel0 * b) - (channel1 * m));
|
||||
|
||||
/* Do not allow negative lux value */
|
||||
if (temp < 0) temp = 0;
|
||||
|
||||
/* Round lsb (2^(LUX_SCALE-1)) */
|
||||
temp += (1 << (TSL2561_LUX_LUXSCALE-1));
|
||||
|
||||
/* Strip off fractional portion */
|
||||
uint32_t lux = temp >> TSL2561_LUX_LUXSCALE;
|
||||
|
||||
return lux;
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
|
||||
Serial.begin(19200);
|
||||
Serial.println("Initializing ...");
|
||||
i2c_init();
|
||||
|
||||
if (!i2c_start(ADDR | I2C_WRITE)) Serial.println(F("Device does not respond"));
|
||||
if (!i2c_write(0x80)) Serial.println(F("Cannot address reg 0"));
|
||||
if (!i2c_write(0x03)) Serial.println(F("Cannot wake up"));
|
||||
i2c_stop();
|
||||
}
|
||||
|
||||
void loop (void) {
|
||||
unsigned int low0, high0, low1, high1;
|
||||
unsigned int chan0, chan1;
|
||||
unsigned int lux;
|
||||
|
||||
delay(1000);
|
||||
i2c_start(ADDR | I2C_WRITE);
|
||||
i2c_write(0x8C);
|
||||
i2c_rep_start(ADDR | I2C_READ);
|
||||
low0 = i2c_read(false);
|
||||
high0 = i2c_read(false);
|
||||
low1 = i2c_read(false);
|
||||
high1 = i2c_read(true);
|
||||
i2c_stop();
|
||||
Serial.print(F("Raw values: chan0="));
|
||||
Serial.print(chan0=(low0+(high0<<8)));
|
||||
Serial.print(F(" / chan1="));
|
||||
Serial.println(chan1=(low1+(high1<<8)));
|
||||
lux = computeLux(chan0,chan1);
|
||||
Serial.print(F("Lux value="));
|
||||
Serial.println(lux);
|
||||
}
|
||||
Reference in New Issue
Block a user