Hey Everybody,
I've been playing with this light sensor from sparkfun
It interprets light into digital pulses
I found some code that I've been trying to modify, it uses an interrupt to read the rising pulses and returns the frequency.
int TSL_FREQ_PIN = 2; // output use digital pin2 for interrupt
#define mo 3
#define TSL_S0 5
#define TSL_S1 6
#define TSL_S2 7
#define TSL_S3 8
#define READ_TM 10 // milleseconds between frequency calculations
int sensor1;
int calcSensitivity;
unsigned long sensitivityHighThresh = 2000;
unsigned long sensitivityLowThresh = 100000;
unsigned long pulseCount = 0;
unsigned long currentTime = millis();
unsigned long startTime = currentTime;
unsigned int tm_diff = 0;
// freq will be modified by the interrupt handler so needs to be volatile
// freq holds the latest frequency calculation
unsigned long frequency;
long uWattCm2;
volatile unsigned long curPulseCount;
unsigned int count = 0;
unsigned int scale; // holds the TLS scale value, see below
void setup() {
Serial.begin(57600);
// attach interrupt to pin2, send output pin of TSL230R to arduino 2
// call handler on each rising pulse
attachInterrupt(0, add_pulse, RISING);
pinMode(TSL_FREQ_PIN, INPUT);
//pinMode(mo, OUTPUT);
pinMode(TSL_S0, OUTPUT);
pinMode(TSL_S1, OUTPUT);
pinMode(TSL_S2, OUTPUT);
pinMode(TSL_S3, OUTPUT);
digitalWrite(TSL_S0, HIGH); // S0 LOW and S1 HIGH = 10x sensitivity
digitalWrite(TSL_S1, HIGH);
digitalWrite(TSL_S2, HIGH); // S2 and S3 HIGH = /100 output scaling
digitalWrite(TSL_S3, HIGH);
scale = 100; // set this to match TSL_S2 and TSL_S3
}
void loop() {
Serial.print("\tuW/cm: ");
Serial.print(getUwattCm2(), DEC);
Serial.print("\tfrequency: ");
Serial.println(frequency, DEC);
}
void add_pulse() {
// increase pulse count
pulseCount++;
// DON'T calculate the frequency every READ_TM ms
// just store the pulse count to be used outside of the interrupt
currentTime = millis();
if( currentTime - startTime >= READ_TM )
{
curPulseCount = pulseCount; // use curPulseCount for calculating freq/uW
pulseCount = 0;
startTime = millis();
}
}
long getUwattCm2() {
// copy pulse counter and multiply.
// the multiplication is necessary for the current
// frequency scaling level.
frequency = curPulseCount * scale;
// get uW observed - assume 640nm wavelength
// calc_sensitivity is our divide-by to map to a given signal strength
// for a given sensitivity (each level of greater sensitivity reduces the signal
// (uW) by a factor of 10)
long uw_cm2 = frequency / calcSensitivity;
// extrapolate into entire cm2 area
uWattCm2 = uw_cm2 * ( 1 / 0.0136 );
calcSensitivity = 1000;
return(uWattCm2);
}
I want it to do the same thing, but instead of using an interrup that is triggered by RISING on the pin, t I want it to use the analog input, and when input goes from 0 to 1023
I tried doing this...
int TSL_FREQ_PIN = 2; // output use digital pin2 for interrupt
#define mo 3
#define TSL_S0 5
#define TSL_S1 6
#define TSL_S2 7
#define TSL_S3 8
#define READ_TM 10 // milleseconds between frequency calculations
int sensor1;
int calcSensitivity;
unsigned long sensitivityHighThresh = 2000;
unsigned long sensitivityLowThresh = 100000;
unsigned long pulseCount = 0;
unsigned long currentTime = millis();
unsigned long startTime = currentTime;
unsigned int tm_diff = 0;
// freq will be modified by the interrupt handler so needs to be volatile
// freq holds the latest frequency calculation
unsigned long frequency;
long uWattCm2;
volatile unsigned long curPulseCount;
unsigned int count = 0;
unsigned int scale; // holds the TLS scale value, see below
void setup() {
Serial.begin(57600);
// attach interrupt to pin2, send output pin of TSL230R to arduino 2
// call handler on each rising pulse
// attachInterrupt(0, add_pulse, RISING);
pinMode(TSL_FREQ_PIN, INPUT);
//pinMode(mo, OUTPUT);
pinMode(TSL_S0, OUTPUT);
pinMode(TSL_S1, OUTPUT);
pinMode(TSL_S2, OUTPUT);
pinMode(TSL_S3, OUTPUT);
digitalWrite(TSL_S0, HIGH); // S0 LOW and S1 HIGH = 10x sensitivity
digitalWrite(TSL_S1, HIGH);
digitalWrite(TSL_S2, HIGH); // S2 and S3 HIGH = /100 output scaling
digitalWrite(TSL_S3, HIGH);
scale = 100; // set this to match TSL_S2 and TSL_S3
}
void loop() {
Serial.print("\tuW/cm: ");
Serial.print(getUwattCm2(), DEC);
Serial.print("\tfrequency: ");
Serial.println(frequency, DEC);
sensor1 = analogRead(TSL_FREQ_PIN);
if(sensor1 == 1023)
{
// increase pulse count
pulseCount++;
// DON'T calculate the frequency every READ_TM ms
// just store the pulse count to be used outside of the interrupt
currentTime = millis();
if( currentTime - startTime >= READ_TM )
{
curPulseCount = pulseCount; // use curPulseCount for calculating freq/uW
pulseCount = 0;
startTime = millis();
}
}
}
long getUwattCm2() {
// copy pulse counter and multiply.
// the multiplication is necessary for the current
// frequency scaling level.
frequency = curPulseCount * scale;
// get uW observed - assume 640nm wavelength
// calc_sensitivity is our divide-by to map to a given signal strength
// for a given sensitivity (each level of greater sensitivity reduces the signal
// (uW) by a factor of 10)
long uw_cm2 = frequency / calcSensitivity;
// extrapolate into entire cm2 area
uWattCm2 = uw_cm2 * ( 1 / 0.0136 );
calcSensitivity = 1000;
return(uWattCm2);
}
It doesn't seem to work for me, I'm not really experienced in programming, can anybody see what I'm doing wrong?
let me know if my description of what Im trying to do can be more clear.
thanks for the help
-joe