/* In the browser window, copy and paste this file into an arduino sketchbook. Then save this file as a file named "acquire_temperatures". This sketch, or computer program, allows us to collect temperature data and write the data to a monitor. Lines like this one (grayed out), are comments to help explain the code. Other explanatory comments are shown to the right of the // symbols in the code below. */ /*-----( First, we load necessary software libraries. Libraries are code written by someone else, but that you can and should use. )-----*/ #include // serial peripheral interface (SPI) is a data protocol used by microcontrollers to control peripherals. #include // this library includes software for the temperature sensor used in this exercise. #include // this library includes software that allows communication and power for one or more devices with a single wire. #include // this library includes software that allows communication with two-wire interface devices /*-----( Next, we tell the software where to find the temperature sensor--connected to Pin 2, which is where we connected it. )-----*/ #define ONE_WIRE_BUS 2 /*-----( Declare two "objects" to communicate with devices; ourWire and sensors)-----*/ /*-----( The result is to set up a way to communicate with the temperature sensor )-----*/ OneWire ourWire(ONE_WIRE_BUS); // allows the object ourWire to communicate with thermistor DallasTemperature sensors(&ourWire); // tells the object "sensors" to handle temperature sensor // /*-----( Perform tasks that only need to be done once. This is a typical requirement of all ARDUINO code.)-----*/ /*-----( Here we define a function called "setup". The function opens serial communications and waits for communications port to open. )-----*/ void setup() //"Void" is a special keyword associated with functions that have no output, like this one. { //Curly brackets are part of the required syntax for the ARDUINO language. Wire.begin(); Serial.begin(9600); { // wait for serial port to connect. Needed for native USB port only. } } // end of setup. /*-----( Perform tasks that will be repeated. Another typical requirement of sketches used with ARDUINO )-----*/ /*-----( Here we define a function to acquire data from our sensor every 1000 milliseconds )----------------- */ void loop() //Defines a function called loop that will acquire temperatures repeatedly every second { sensors.begin(); sensors.requestTemperatures(); // Send the command to get temperatures. Commands like requestTemperatures are from the libraries mentioned above. Serial.print(sensors.getTempCByIndex(0)); //Prints temperatures in degrees C in order ("by index") according to time to your serial monitor (computer screen). Serial.print("C"); //Prints the literal letter "C", for degrees Celsius. Serial.print(" "); //Prints a space; for readability. Serial.print(sensors.getTempFByIndex(0)); //Prints temperatures in degrees F in order ("by index") according to time. Serial.println("F"); //Prints the literal letter "F", for degrees Fahrenheit and provides a new line. delay(5000); //Delay is the number of milliseconds between temperature samples } /*-------( The loop will keep running until you turn off the power to the ARDUINO )----------*/