I have two TSL2561 luminosity sensors hooked up to my project, using the blynk app, they both run through the I2C bus and I am wanting to compare the outputs from both of them. Instead of it displaying the values of both sensors on the display I want it to display what percent higher sensor 1 is compared to sensor 2. The problem I am running into is that both of the sensors data is called with the same command and I dont know how to differentiate them so that the arithmetic can be done... Any advice for me on this?
You can see the command to get the data is tsl.getEvent(&event) and tsl1.getEvent(&event) I just cannot get this figured out so that I can compare these numbers...
Thanks, I will look into those when I get home (I am at work right now). Almost all of the code came from the example sketches I have just been using trial and error to "merge" them into my project. I will number all of the variables in the set LOL. And I will serial print event.light and see what it does thanks for the advice!
ok, I used your guys advice and have it comparing the values. The problem is the serial monitor will only show zero... just for the sake of argument lets say that light1 = 103 and light2 = 76. now I want to find the percentage increase, that means I have to take the difference of light1 and light2. so that is:
difference = light1 - light2;
next I need to take the difference and divide it by the original number (lets call that part1), that is:
part1 = difference / light1;
next i need to multiply that answer by 100 (lets call that percent), that is:
percent = part1 * 100;
I have all of my variables declared as so
int light1, light2, difference, part1, percent;
Now no matter what the values of light1 and light2 are I get zero. the only reason I can come up with is that the arduino does not work with decimals well because light1 - light2 is 27
27 divided by light1 is 0.262
Now 0.262 would be part1, if i serial print part1 I get a zero. Why is it just printing the whole number? even if i multiply part1(0.262) by 100 I still only get 0. does arduino not look past the whole number when doing arithmatic?
Here is the code I wound up with,... What do you think? I added some comments to make it easier to follow... my eyes were crossing
//Temp probe board data output to dital pin #2
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
Adafruit_TSL2561_Unified tsl1 = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
Adafruit_TSL2561_Unified tsl2 = Adafruit_TSL2561_Unified(TSL2561_ADDR_LOW, 67890);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "1e8e804e3c7943e188857eff5f85b1f4";
SimpleTimer timer;
void configureSensor(void)
{
tsl1.enableAutoRange(true);
tsl1.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
tsl2.enableAutoRange(true);
tsl2.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
}
void setup()
{
sensors.begin();
Serial.begin(9600);
Blynk.begin(auth);
timer.setInterval(10000L, sendUptime);
if(!tsl1.begin()) //initialises the tsl sensors
{
Blynk.virtualWrite(0, "***"); //No TSL2561 Detected
while(1);
}
configureSensor(); //sets up the tsl sensors
}
void sendUptime()
{
sensors.requestTemperatures();
Blynk.virtualWrite(0, sensors.getTempFByIndex(0)); //prints the temperature data of the DS18B20 (one wire address 0)
Blynk.virtualWrite(1, sensors.getTempFByIndex(1)); //prints the temperature data of the DS18B20 (one wire address 1)
Blynk.virtualWrite(2, sensors.getTempFByIndex(2)); //prints the temperature data of the DS18B20 (one wire address 2)
Blynk.virtualWrite(3, sensors.getTempFByIndex(3)); //prints the temperature data of the DS18B20 (one wire address 3)
Blynk.virtualWrite(4, sensors.getTempFByIndex(4)); //prints the temperature data of the DS18B20 (one wire address 4)
Blynk.virtualWrite(5, sensors.getTempFByIndex(5)); //prints the temperature data of the DS18B20 (one wire address 5)
float light1, light2, difference, part1, percent, percentdifference; //variables for tsl1 and tsl2 data storage to do arithmetic with
sensors_event_t event;
tsl1.getEvent(&event); //get the value of tsl1
if (event.light)
{
Blynk.virtualWrite(6, event.light); //writes the value of tsl1 to the Blynk app
}
else
{
Blynk.virtualWrite(6, 0); //dispays 0 if too little light is present to be measured
}
tsl2.getEvent(&event); //get the value of tsl2
if (event.light)
{
Blynk.virtualWrite(7, event.light); //writes the value of tsl2 to the Blynk app
}
else
{
Blynk.virtualWrite(7, 0); //displays 0 if too little light is present to be measured
}
tsl1.getEvent(&event); //assigning the light data from tsl1 to a variable
light1 = event.light;
tsl2.getEvent(&event); //assigning the light data from tsl2 to a variable
light2 = event.light;
difference = light1 - light2; //math for getting the difference between tsl1 and tsl2
part1 = difference / light1; //first part of math for getting the percent of light transmission
percent = part1 * 100.00; //second part of math for getting the percent of light transmission (this number is how much bigger light1 is than light2 in percent)
percentdifference = 100.00 - percent; //third part of math for getting the percent of light transmission (this number is "how close are light1 and light2 to being the same")
//if light1 and light2 were both the same value this would come up to 100 percent showing that they are the same
Blynk.virtualWrite(8, percentdifference); //writes the percent of light transmission to the Blynk app
}
void loop()
{
Blynk.run(); //initiates Blynk
timer.run(); //initiates SimpleTimer
}