Project 03 - An Analog Thermometer

<< Click to Display Table of Contents >>

Navigation:  Projects >

Project 03 - An Analog Thermometer

This project shows how to create analog thermometer using the CleO primitives. It uses HDC1080 custom library to interact with the HDC1080 component.

 

 

Component(s)

 

This project uses the following component(s) -

 

HDC1080

 

 

Wiring Diagram

 

 

Project 03-Analog Thermometer

 

 

Code

 

The loop() Arduino routine starts with reading the temperature and humidity. It builds the screen frame between Start() and Show() routines.

 

Default values of temperature (10-degree Celsius) and humidity (70%) will be used if there is any reading error.

 

The following code snippet shows how to draw various components:

 

void loop() {

 touchControl();

 

  int ManufacturerId = hdc1080.readManufacturerId();

  int DeviceId = hdc1080.readDeviceId();

 

 /* Read temperature from HDC1080 */

 float tempC = hdc1080.readTemperature();

 float humidity = hdc1080.readHumidity();

 

/* Start drawing thermometer */

 CleO.Start();

 

/* Draw rectangle cover whole screen */

 CleO.RectangleExt(CENTRE_X, CENTRE_Y, SCREEN_WIDTH, SCREEN_HEIGHT, WHITE, MM, 0, 0);

 

/* Draws toggle switch to set display unit of temperature */

 drawReading(tempC, humidity);

 drawReadingToggle();

 

/* Draws measurement, units and shows tempC as an indicator */

 drawThermometer(tempC);

 

 if (!(ManufacturerId == MANUFACTURER_ID && DeviceId == DEVICE_ID))

   CleO.StringExt(FONT_SANS_2, 10, SCREEN_HEIGHT - 10, RED, BL, 0, 0, "No HDC1080 Detected");

 

 CleO.Show();

 

 /* slow down the rate of reading from thermometer */

 delay(500);

}

 

void touchControl() {

 int16_t x, y, dur, tag;

 CleO.TouchCoordinates(x, y, dur, tag);

 

/* pen down */

 if (previousDur == 0 && dur != 0) {

   previousTag = tag;

   previousDur = 1;

 }

/* pen up */

 else if (previousDur == 1 && dur == 0) {

 

  /* If unit toggle is touched, alter display unit for temperature */

   if (previousTag == UNIT_TAG)

     showCelsius = !showCelsius;

 

   previousDur = 0;

 }

}

 

/* Displays current temperature and humidity readings */

void drawReading(float tempC, float humidity) {

 float reading = tempC;

 

 if (showCelsius == false)

   reading = (1.8 * tempC) + 32;

 

 String displayText = String(reading);

 displayText.toCharArray(buffer, 10);

 CleO.StringExt(FONT_SANS_4, 225, (SCREEN_HEIGHT - 90), BLACK, TL, 0, 0, buffer);

 

 displayText = String(humidity);

 displayText += " %";

 displayText.toCharArray(buffer, 10);

 CleO.StringExt(FONT_SANS_3, 215, (SCREEN_HEIGHT - 200), BLACK, TL, 0, 0, "Humidity");

 CleO.StringExt(FONT_SANS_3, 225, (SCREEN_HEIGHT - 170), BLACK, TL, 0, 0, buffer);

}

 

/* Displays toggle button to select Celsius or Fahrenheit unit */

void drawReadingToggle() {

 CleO.eve_bgcolor(YELLOW);

 

/* Assign tag and based on current unit status, displays toggle switch */

 CleO.Tag(UNIT_TAG);

 if (showCelsius == true)

   CleO.eve_toggle(235, (SCREEN_HEIGHT - 45), 50, FONT_MEDIUM, OPT_FLAT, 0, "Cel" "\xff" "Fer");

 else

   CleO.eve_toggle(235, (SCREEN_HEIGHT - 45), 50, FONT_MEDIUM, OPT_FLAT, 65535, "Cel" "\xff" "Fer");

}

 

void drawReading(int tempC) {

 CleO.PointExt(110, 445, 25, YELLOW, 0, 0);

 

 int negativeReadingY = 0, positiveReadingY = 0;

 if (tempC >= 0) {

   positiveReadingY = 25 - (4 * (DIGITS_MIN)) + (4 * tempC);

   CleO.RectangleExt(110, 445, 12, positiveReadingY, YELLOW, BM, 0, 0);

 } else {

   negativeReadingY = 25 + ((tempC - DIGITS_MIN) * 4);

   CleO.RectangleExt(110, 445, 12, negativeReadingY, YELLOW, BM, 0, 0);

 }

}

 

void drawMeasurement() {

 int lineLength, lineY = 420;

 CleO.LineColor(BLACK);

 CleO.LineWidth(1);

 

/* For each temperature reading on thermometer line */

 for (int startTemp = DIGITS_MIN;

      startTemp <= DIGITS_MAX;

      startTemp += 1) {

   if (startTemp % 10 == 0)

     lineLength = 30;

   else if (startTemp % 5 == 0)

     lineLength = 20;

   else

     lineLength = 10;

 

   int temFLineX1 = 130 - lineLength - (2 * 20);;

 

 /* Draw line for celsius and fahrenheit unit */

   CleO.Line(130, lineY, 130 + lineLength, lineY);

   CleO.Line(temFLineX1, lineY, 90, lineY);

 

   if (startTemp % 10 == 0) {

     String displayText = String(startTemp);

     displayText.toCharArray(buffer, 6);

 

    /* Draw Celsius unit text */

     CleO.StringExt(FONT_SANS_1, 170, lineY, BLACK, ML, 0, 0, buffer);

 

     displayText = String((1.8 * startTemp) + 32);

     displayText.toCharArray(buffer, 6);

 

    /* Draw Fahrenheit unit text */

     CleO.StringExt(FONT_SANS_1, temFLineX1 - 10, lineY, BLACK, MR, 0, 0, buffer);

   }

 

   lineY -= 4;

 }

}

 

void drawThermometer(float tempC) {

/* Draws base circle of thermometer using point */

 CleO.PointExt(110, 445, 25, DEEPSKYBLUE, 0, 0);

 

/* Draws thermometer vertical line which dis        plays reading level */

 CleO.RectangleExt(110, 445, 12, 430, DEEPSKYBLUE, BM, 0, 0);

 CleO.PointExt(110, 415, 6, DEEPSKYBLUE, 0, 0);

 

/* Draws unit lines on vertical bar of thermometer */

 drawMeasurement();

 

/* Draws units (text) on vertical bar of thermometer */

 CleO.StringExt(FONT_SANS_3, 90, 15, BLACK, TR, 0, 0, "F");

 CleO.StringExt(FONT_SANS_3, 130, 15, BLACK, TL, 0, 0, "C");

 

/* Draws reading level on measurement line */

 drawReading(tempC);

}

 

 

Note: If no hardware is detected, then an appropriate error message is displayed.

 

 

Output

 

 

Project 3 - An Analog Thermometer