#include #include #include "Adafruit_LEDBackpack.h" #include "Adafruit_GFX.h" #include Adafruit_7segment matrix = Adafruit_7segment(); // Data wire is plugged into pin 2 on the Arduino #define ONE_WIRE_BUS 2 // Setup a oneWire instance to communicate with any OneWire devices // (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); void setup(void) { // start serial port - for debugging Serial.begin(9600); Serial.println("Temperature"); // Start up the library for 7-seg and set max brightness matrix.begin(0x70); matrix.setBrightness(15); // Start up the library for temperature sensor sensors.begin(); } void loop(void) { // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus sensors.requestTemperatures(); // Send the command to get temperatures // You can have more than one IC on the same bus. // 0 refers to the first IC on the wire int temp = round(sensors.getTempCByIndex(0)); // We need to convert the absolute value of temperature to string String t = String(abs(temp)); if (t.length() > 1) { // If the temerature is between 10 and 99 degrees or -10 and -99... String t1 = t.substring(0, 1); // ...we need the first String t2 = t.substring(1, 2); // and second digit matrix.writeDigitRaw(0,0); matrix.writeDigitNum(1, t1.toInt()); // which we plot to second and third place of 7-seg matrix.writeDigitNum(3, t2.toInt()); if (temp<0) {matrix.writeDigitRaw(0,64);}// and if the temperature is negative, we plot the minus sign to first place. } else { // Other case is temperature between 0 and 9 or -1 to -9 String t1 = t.substring(0, 1); // Then we need only the first digit matrix.writeDigitRaw(0,0); matrix.writeDigitRaw(1,0); matrix.writeDigitNum(3, t1.toInt()); // And we plot it to third place of 7-seg if (temp<0) {matrix.writeDigitRaw(1,64);} // and if the temerature is negative, we plot the minus sign to second place. } matrix.writeDigitRaw(2, 0x10); // light up dot as degree sign matrix.writeDigitRaw(4,57); // and the letter C to fourth place matrix.writeDisplay(); delay(500); }