We don’t do anything in this project that we haven’t done before, but we take a different approach. Here, we shift a lot of the work into functions. This makes the code easier to follow and easier to adapt for a new project. You can simply copy the functions and paste them into different projects. Upload the program, wait a moment, and then open the serial monitor. The temperature will be output to the serial monitor in °C. When the temperature goes above the value of the float variable tooHot (set to 32.2 °C (90 °F)), the piezo sounds an alarm. You can breathe gently on the sensor to raise its temperature and trigger the alarm.

Both the setup() and loop() blocks are simple in this program. Besides setting pinMode for the piezo and initializing the Serial object, the loop() block makes a call to the setupSensor function to set the sensor’s resolution and direct the pointer to register 1 (this code is explained in detail in Project 6.03): void setupSensor(){ Wire.begin(); Wire.beginTransmission(address); Wire.write(0x01); Wire.write(0x60); Wire.endTransmission(); Wire.beginTransmission(address); Wire.write( byte(0x00)), Wire.endTransmission(); } The loop() block contains only three lines. The first starts the output line to the serial monitor: Serial.print("TEMP = "); The next line makes a call to our function getTemp and prints the result to the serial monitor with one decimal place. This all happens in one line: Serial.println(getTemp(),1); The third line makes another call to getTemp and calls the piezoTone function if the returned values is greater than 32.2: if(getTemp() > 32.2) piezoTone(4000,10); The getTemp function contains the code to read the temperature sensor and return a float value. The code is explained in Project 6.03: float getTemp(){ Wire.requestFrom(address, byte(2)); tempreg = Wire.read(); tempreg= tempreg << 8; tempreg |= Wire.read(); tempreg = tempreg >> 4; temperature =( float ) tempreg / 16; return temperature;ke } We used the piezoTone function in a lot of our other projects. This is a good example of portable code. We only needed to paste it into this sketch to use it for our alarm.