If then statement using a piezo speaker & ML8511 UV Sensor

I want the code to run so that the piezo (connected to breadboard via Arduino) beeps when UV levels reach over 500 (input from sensor ML8511)

I was able to get the ML8511 sensor to work separately and the piezo speaker but I can't get the code to work with if ML8511 voltage output > 500, then make piezo speaker beep beep beep)

Show the complete code that you have. Use code tags.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom.. :slight_smile:

This is the code for piezo:

[/

const int buzzer = 9; //buzzer to arduino pin 9


void setup() {

 pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output

}

void loop() {

 tone(buzzer, 1000); // Send 1KHz sound signal...
 delay(1000);        // ...for 1 sec
 noTone(buzzer);     // Stop sound...
 delay(1000);        // ...for 1sec

}

This is the code for the ML8511

[code][/void setup() {
 {
  Serial.begin(9600);

  pinMode(UVOUT, INPUT);
  pinMode(REF_3V3, INPUT);

  Serial.println("ML8511 example");
    pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
 
}

}

void loop() {
{
  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 from sensor
  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);
  
  
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);        // ...for 1sec
  
}

//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
{
  byte numberOfReadings = 8;
  unsigned int runningValue = 0; 

  for(int x = 0 ; x < numberOfReadings ; x++)
    runningValue += analogRead(pinToRead);
  runningValue /= numberOfReadings;

  return(runningValue);  
}

//The Arduino Map function but for floats

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
 


code]

Here is a picture, does this help?

It would be more useful to see your best attempt at combining the codes. You said you tried to do that but it didn't work for you. So post the code that didn't work and tell us exactly what it does and what it does not do that it should do.

Steve

Hi,
Ops picture;


Tom... :slight_smile:

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
}

Hi

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks Tom.... :slight_smile:

Here is my code:

*/

// 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
}

That code doesn't even compile. And the code you had originally that you said was working code for the ML8511 doesn't compile either. I think you have probably missed out a library Include and at least one function definition in your copying.

Try to get a version that compiles and is reasonably complete.Then we can try to help you to get it working.

Ste

Hi,

float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0);

Where did you get "mapfloat"
map function works with integer values and returns integer values.

You need to look at your code closely, check how many times you declare pinMode for two pins, in your setup.

Tom... :slight_smile: