2014-11-02 76 views
0

我喜歡這個網站。謝謝大家在這裏做的事!Arduino代碼組合 - 結合溫度傳感器和蜂鳴器的最大溫度代碼

我終於開始用Arduino Uno做自己的項目了。當我剛剛做時,我發現我學得最好。我找到了一本關於從我的TMP36GZ獲取溫度的教程,通過Ardino在我的16x2 LCD上讀出。太酷了!但是現在,我想知道當溫度達到最大和/或最小時如何使CEM1203(42)蜂鳴器熄滅。我發現了一個簡單的代碼來讓蜂鳴器也可以使用。現在我只需要將兩者結合起來。

以下是2個代碼。例如,當溫度=/< 65°F和=> 75°F時,我想說蜂鳴器響。

謝謝你提供的任何幫助。我很高興能夠學習這些東西!

/* 
 
Piezo 
 
    
 
This example shows how to run a Piezo Buzzer on pin 9 
 
using the analogWrite() function. 
 
    
 
It beeps 3 times fast at startup, waits a second then beeps continuously 
 
at a slower pace 
 
    
 
*/ 
 

 
void setup() { 
 
    // declare pin 9 to be an output: 
 
    pinMode(9, OUTPUT); 
 
    beep(50); 
 
    beep(50); 
 
    beep(50); 
 
    delay(1000); 
 
} 
 

 
void loop() { 
 
    beep(200); 
 
} 
 

 
void beep(unsigned char delayms){ 
 
    analogWrite(9, 20);  // Almost any value can be used except 0 and 255 
 
          // experiment to get the best tone 
 
    delay(delayms);   // wait for a delayms ms 
 
    analogWrite(9, 0);  // 0 turns it off 
 
    delay(delayms);   // wait for a delayms ms 
 
}
/* 
 
September 12 2013 
 
October 25 2012 
 
Based off of a project by DJ Mentzik. 
 
Enhanced and modified by WWC and citin. 
 
Supporting documents can be found at http://www.instructables.com/member/WWC/ 
 
Use and modife as needed. 
 

 
Displays Current, 8 sec Average, Max and Min Temperature. 
 

 
To wire your LCD screen to your Arduino, connect the following pins: 
 
LCD Pin 6 to digital pin 12 
 
LCD Pin 4 to digital pin 11 
 
LCD Pin 11 to digital pin 5 
 
LCD Pin 12 to digital pin 4 
 
LCD Pin 13 to digital pin 3 
 
LCD Pin 14 to digital pin 2 
 
LCD PIN 15 to POS 
 
LCD PIN 16 to GND 
 

 

 
*/ 
 

 
#include <LiquidCrystal.h>          // include the LCD driver library 
 

 
                   //declare variables 
 
float tempC = 0;             // Variable for holding Celcius temp (floating for decimal points precision) 
 
float tempf = 0;             // variable for holding Fareghneit temp 
 
int tempPin = 0;             // Declaring the Analog input to be 0 (A0) of Arduino board. 
 
float samples[8];            // Array to hold 8 samples for Average temp calculation 
 
float maxi = 0,mini = 100;          // Max/Min temperature variables with initial values. LM35 in simple setup only measures Temp above 0. 
 
int i; 
 

 
               
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);       // initialize the library with the numbers of the interface pins 
 

 
void setup() 
 
{ 
 
Serial.begin(9600);            // Opens serial port, sets data rate to 9600 bps 
 

 
lcd.begin(16, 2);            // Set up the LCD's number of columns and rows: 
 

 
lcd.setCursor(2, 0);           // Set LCD cursor position (column, row) 
 
lcd.print("Waynes World");          // Print text to LCD 
 
lcd.setCursor(3, 1);           // Set LCD cursor position (column,row) 
 
lcd.print("Thermometer");          // Print text to LCD 
 
delay(5000); // wait 500ms          // Delay to read text 
 
lcd.clear(); // clear LCD display        // Clear the display 
 
lcd.setCursor(2, 0);           // Set LCD cursor position (column, row) 
 
lcd.print("LCD Displayed");          // Print text to LCD 
 
lcd.setCursor(1, 1);           // Set LCD cursor position (column, row) 
 
lcd.print(" Averaged Temp ");         // Print text to LCD                                                                                                      
 
delay(5000);             // Delay to read text 
 
lcd.clear();             // Clear LCD 
 

 

 
} 
 

 
void loop() 
 
{ 
 
Serial.println("");           // Blank line for spacing in the serial monitor 
 
Serial.println("You are looking at a project built by WWC."); // Print text to Serial monitor 
 
Serial.print("Feal free to use and modife as needed."); 
 
Serial.println("");           // Blank line for spacing in the serial monitor 
 
Serial.print("LM35 Raw data: ");        // Print text to Serial monitor 
 
Serial.println(analogRead(tempPin));       // Displays on serial monitor the sampled value before conversion to real Temperature reading 
 
    
 
                   // Start of calculations FOR loop. 
 
for(i = 0;i<=7;i++){           // gets 8 samples of temperature 
 
samples[i] = (4.4 * analogRead(tempPin) * 26)/1024.0; // conversion math of tmp36GZ sample to readable temperature and stores result to samples array. 
 

 
                   
 
                                  
 
                   
 
Serial.println(samples[i]);         // Print samples [i] to sertiual monitor            
 
                                 
 
                   // (LCD note: line 1 is the second row, since counting begins with 0): 
 
lcd.setCursor(0, 0);           // Set LCD cursor position (column 0, row 0) 
 
lcd.print("Current Temp is: ");        // Print text to LCD 
 
lcd.setCursor(1, 1);           // Set LCD cursor position (column 1, row 1) 
 
lcd.print(" Celcius ");          // Print text to LCD 
 
lcd.setCursor(12, 1);           // Set LCD cursor position (column 12, row 1) 
 
lcd.print(samples[i]);           // print current Temp sample to LCD 
 
tempC = tempC + samples[i];         // do the addition for average temperature 
 
delay(800);             // wait 800ms 
 

 
}                // END of FOR loop 
 

 
Serial.println("");           // Blank line for spacing in the serial monitor 
 
Serial.println("");           // Blank line for spacing in the serial monitor 
 
tempC = tempC/8.0;            // calculated the averare of 8 samples in Celcius 
 

 
tempf = (tempC * 9)/ 5 + 32;         // converts to fahrenheit 
 

 
if(tempC > maxi) {maxi = tempC;}        // set max temperature 
 
if(tempC < mini) {mini = tempC;}        // set min temperature 
 
if(tempC >16.0) 
 

 
                   // Send Results to Serial Monitor 
 
Serial.println("New measurement:"); 
 
Serial.print(" Average Temperature in Celcius is ");   // Print text to Serial monitor 
 
Serial.println(tempC);//send the data to the computer   // Send the data to the computer 
 
Serial.print(" Average Temperature in Farenait is ");   // Print text to Serial monitor 
 
Serial.println(tempf);//send the data to the computer   // Send the data to the computer 
 
Serial.print(" MAX Temperature in Celcius is ");    // Print text to Serial monitor 
 
Serial.println(maxi);//send the data to the computer   // Send the data to the computer 
 
Serial.print(" MIN Temperature in Celcius is ");    // Print text to Serial monitor 
 
Serial.println(mini);//send the data to the computer   // Send the data to the computer 
 
                  // Send results to LCD. 
 
lcd.setCursor(0, 1);           // Set LCD cursor position (column 0, line 1) 
 
lcd.print(" Fahrenheit ");          // Print text to LCD 
 
lcd.setCursor(12, 1);           // Set LCD cursor position (column 12, line 1) 
 
lcd.print(tempf);            // Send the data to the LCD 
 

 
delay(6000);             // Wait 3 seconds to display the Fahrenheit temp and 3 seconds to display results to LCD screen befor starting the loop again = 6 seconds. 
 
tempC = 0;              // Set tempC to 0 so calculations can be done again 
 
}

回答

0

你的溫碼有最會這樣把增加在那裏。

通常你會從設置中轉移所有。在這種情況下:

pinMode(9, OUTPUT); 

是唯一需要的代碼。如果你想讓它在啓動時發出蜂鳴聲,你可以放入其餘的。

通過整個「void beep()」函數進行傳輸。所以你可以像調用「void loop()」函數一樣調用它。

然後在您的臨時代碼tempf在計算行後:

tempf = (tempC * 9)/ 5 + 32; 

把一些代碼,如:

if (tempf <= 65){ 

    //low temp beep code here 

} 
else if (tempf >= 75) { 

    //high temp beep code here 

}