1

I'm new to this Arduino programming. So I want to ask about my current project. My lecturer told me to make a temperature sensor using an LM35, where a green LED will light up when below some certain temperature that I set. The same goes for yellow and red LEDs for medium, and high temperatures. The tricky part here is that I need to make sure my buzzer (piezo) is on forever if it reaches the high temperature, until I press a button to reset it.

This is my coding so far:

const int greenLed = 3;
const int redLed = 4;
const int yellowLed = 5;
float tempC;
int reading;
int tempPin = 0;
int speakerOut = 11;
const int buttonPin = 2;     
int buttonState = 0;     
long duration = 10000;
int tempo = 0;

void setup() {
// initialize digital pin 13 as an output.
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
pinMode(11, OUTPUT);
analogReference(INTERNAL);
Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
buttonState = digitalRead(buttonPin);
reading = analogRead(tempPin);
tempC = reading / 9.31;
Serial.println(tempC);
delay(500);

if (tempC < 30.00) {
//Serial.println("Temp LOW");
digitalWrite(3, HIGH);
delay(10);
}else {
digitalWrite(3,LOW);
delay(10);

}

if (31.00 < tempC < 33){
//Serial.println("Temp MED");
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
delay(50);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
delay(50);

}else {
digitalWrite(4,LOW);
digitalWrite(5,LOW);

}

if(tempC >= 34){
**WHAT SHOULD I FILL UP HERE TO MAKE SURE MY PIEZO BUZZER ON FOREVER**
}
if(buttonState = 1);{
digitalWrite(11, LOW);
}

}

Any help would be great. Or if you have a better idea on how to do this, I'm gonna love it.

  • 2
    Please post the wiring of this and format the code. Also use the constants you have defined. Why define "greenLed = 3" and still use "digitalWrite(3, ...)"? – Mikael Patel Feb 12 '16 at 10:27
  • uhmm i dont get what you mean.its my 1st month learning this.well if i do "greenLed =3 , it is not good to use digitalWrite(3,...)"? .Whats the better way? – Naqib Nadzir Feb 12 '16 at 11:34
  • A better way is "digitalWrite(greenLed, ...)". The whole idea behind giving a pin a "name" is to allow change but also make the code easier to read. But your lecturer should have told you that :) – Mikael Patel Feb 12 '16 at 12:19
5

You can use the toneAC library, which allows even better handling of piezo speakers then the standard "tone"-functions.

Features: Nearly twice the volume (because it uses two out of phase pins in push/pull fashion) Higher quality (less clicking) Capability of producing higher frequencies (even if running at a lower clock speed) Nearly 1.5k smaller compiled code Bug fixes (standard tone library can generate some odd and unpredictable results) Can set not only the frequency but also the sound volume Less stress on the speaker so it will last longer and sound better

With the library you can do something like:

int freq = 1000
int vol = 10
int length = 0
boolean background = true

toneAC(freq, vol, length, background);

The tone now will play forever in the background, until you stop it with:

toneAC(0);
  • Ohh cool.Gonna try this one later. Thanks for ur help! – Naqib Nadzir Feb 12 '16 at 11:35
0
  1. The answer is simple add

    digitalWrite(speakerOut,HIGH);
    
  2. Common mistake made by beginners is to use assign operator instead of compare operator

    if(buttonState=1) 
    

    should actually be

    if(buttonState==1)
    
  3. Never pull that pin LOW

    if(buttonState = 1);{
    digitalWrite(11, LOW);
    }
    

    is wrong use the below code

    if(buttonState == 1){
        digitalWrite(11, LOW);
    }
    

Don't forget to connect the button to buttonPin or you will get random HIGH or LOW from that pin since its floating

Hope this helps.

  • 1
    That's not how a Piezo-Speaker works... – Michael B Feb 12 '16 at 10:55
  • Comments are not for extended discussion; this conversation has been moved to chat. – Avamander Feb 12 '16 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.