Arduino代码组合 - 在最小临时温度代码下组合温度传感器和蜂鸣器

时间:2014-11-02 01:04:15

标签: arduino sensor arduino-uno

我喜欢这个网站。谢谢你们所做的一切!

我终于开始用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
}

1 个答案:

答案 0 :(得分: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

}