modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
const int buzzer = 9; // pin that the BUZZER is attached to
const int threshold = 500; // an arbitrary threshold level that's in the range of the analog input
void setup() {
// initialize the buzzer pin as an output:
pinMode(buzzer, OUTPUT);
pinMode(UVOUT, INPUT);
// initialize serial communications:
Serial.begin(9600);
pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);
}
void loop() {
// read the value of the potentiometer:
int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);
//Use the 3.3V power pin as a reference to get a very accurate output value
// if the analog value is high enough, turn on the Piezo Speaker:
float outputVoltage = 3.3 / refLevel * uvLevel;
float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level
Serial.print("output: ");
Serial.print(refLevel);
Serial.print("ML8511 output: ");
Serial.print(uvLevel);
Serial.print(" / ML8511 voltage: ");
Serial.print(outputVoltage);
Serial.print(" / UV Intensity (mW/cm^2): ");
Serial.print(uvIntensity);
Serial.println();
delay(100);
if (analogValue > 500) {
digitalWrite(buzzer, HIGH);
} else {
digitalWrite(buzzer, LOW);
}
// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
}