Código #1 TSL235

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
2.8 kB
2
Indexable
Never
/* Example: TSL235R
  
   Sensor:
   http://www.sparkfun.com/datasheets/Sensors/Imaging/TSL235R-LF.pdf
   Measurement area: 0.92mm2
   
   Wiring:
   TSL235R    Arduino pins
   GND        GND
   Vcc        +5V
   Out        Digital pin 2
   Wire a 0.1uF capacitator between Vcc and GND close to the sensor
   Wire a led between pin 13 and GND
*/

// Pin definitions
# define TSL235R 2                   // Pin to TSL235R output
# define LEDpin 13                   // Led to show when the sensors is measuring

// Constants
int period = 2000;                   // measurement period, in miliseconds
int ScalingFactor = 1;               // Scaling factor of this sensor
float area = 0.0092;                 // Sensing area of TSL25R device, in cm2

// Variables
unsigned long counter = 0;           // Counter of measurements
volatile unsigned long pulses = 0;   // Counter pf pulses in each measurement period
unsigned long frequency;             // Store the frequency calculated
float irradiance;                    // Store the irradiance calculated
unsigned long startTime;             // Stablish the time when measurement starts
unsigned long currentTime;           // Used to measure the time during measurement
 

void setup(){
  Serial.begin(9600);    // Starts the serial port
  pinMode(TSL235R, INPUT);                    // Declare the pin such as an input of data
  pinMode(LEDpin, OUTPUT);
  Serial.println("Testing a TSL235R sensor:");  // Splash screen
  Serial.println("-------------------------");
  Serial.println(" v0.2      20100720");  
  Serial.println();
}

void loop(){
  digitalWrite(LEDpin, HIGH);
  counter++;
  Serial.print(counter);
  Serial.print("   ");
  startTime = millis();
  currentTime= millis();
  while (currentTime - startTime <= period){
    attachInterrupt(0, count_pulses, RISING);
    //detachInterrupt(0);                          // Stop to sensing pulses from TSL235R sensor
  }
  digitalWrite(LEDpin, LOW);
  Serial.print("pulses: ");
  Serial.print(pulses);
  Serial.print(";  ");
  Serial.print("frequency: ");
  Serial.print(getfrequency(), DEC);
  Serial.print("  Hz;  ");
  Serial.print("irradiance: ");
  Serial.print(getirradiance(), DEC);
  Serial.println("  uW/cm2");
  pulses = 0;                                   // Reset the pulses counter for the next measurement
  delay(5000);                                  // Wait 5 seconds until next measurement
}

void count_pulses()
{
 pulses++;
 currentTime=millis();
}
 

unsigned long getfrequency(){
 frequency = pulses/(period/1000);
 return(frequency);
}

long getirradiance() {
  irradiance = frequency / area;                // Calculate Irradiance (uW/cm2)
  return (irradiance);
}