SOLVED - UCGlib colour relative to temperature print problem

Update: my code is compiling now thanks to John Wasser, but the temperature is not displayed as I want: relative to the temperature. I have just posted my code at the bottom as post #8 Any help much appreciated!

Hi,
I am trying to display temperatures with the colour related to the temperature: red=hot, blue=cold. I have this working with another 2.2"display and the ILI9225 library with some help from this forum, I am not a great programmer.

I now want to do the same with a 1.8" screen for which I need the UCGLib library. The screen works fine but I am stuck with the "colour code".

For ILI I use:

void drawTempInColour(byte x, byte y,float temp,unsigned int colour)
{
char buf[16];
dtostrf(temp,6,1,buf);
tft.drawText(x, y, buf,colour);
}

which I changed into:

void drawTempInColour(byte x, byte y,float temp,unsigned int colour)

{
char buf[16];
dtostrf(temp,6,1,buf);
ucg.print(x, y, buf,colour);
}

for the UCG libs. I get the following error message from the Arduino IDE:

no matching function for call to 'Ucglib_ST7735_18x128x160_HWSPI::print(byte&, byte&, char [16], unsigned int&)'

I suspect that I need to add something to the line

Ucglib_ST7735_18x128x160_HWSPI ucg(/cd=/ 9 , /cs=/ 10, /reset=/ 8);

on top of the sketch but do not have the slightest clue what.

full sketch:

// Peter van de Pol, Wild Bill & many others :)

#include <SPI.h>
#include "Ucglib.h"
#include <OneWire.h>
#include <DallasTemperature.h>

Ucglib_ST7735_18x128x160_HWSPI ucg(/*cd=*/ 9 , /*cs=*/ 10, /*reset=*/ 8);

// Data Sensors to Arduino pin 2
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/* Display wiring including resistors
1. RST >1KΩ> 8
2. CE >1KΩ> 10 
3. D/C >1KΩ> 9
4. DIN >1KΩ> 11
5. Clk >1KΩ> 13
6. VCC +5V
7. BL >100Ω> +5V
8. GND GND
*/

void setup(void)
{
delay(1000);
ucg.begin(UCG_FONT_MODE_TRANSPARENT);

//ucg.setFont(ucg_font_ncenR12_tr);
//ucg.setColor(255, 0, 0);
//ucg.setPrintPos(10,25);
//ucg.print("Starting 1-2-3");

ucg.clearScreen();
  // Start up the Sensors library
  sensors.begin();
  // set the Sensor resolution to 9 bit - Valid values are 9, 10, or 11 bit.
  sensors.setResolution(9);
  delay(500);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  sensors.requestTemperatures(); // Get temperatures
  // String sensor4 = String(sensors.getTempCByIndex(1),DEC); //DEC returns 10 decimals Change to 1 below
  float temp1 = sensors.getTempCByIndex(0);
  float temp2 = sensors.getTempCByIndex(1);
  float temp3 = sensors.getTempCByIndex(2);
  float temp4 = sensors.getTempCByIndex(3);
  Serial.println(temp1);
  Serial.println(temp2);
  Serial.println(temp3);
  Serial.println(temp4);
  Serial.println(   );
  delay(100);

  ucg.clearScreen();


ucg.setFont(ucg_font_helvB12_tr);
ucg.setColor(255, 255, 255);
ucg.setPrintPos(10,25);
ucg.print("Buffervat");

ucg.setFont(ucg_font_helvB10_tr);
ucg.setColor(0, 0, 145);
ucg.setPrintPos(5,60);
ucg.print("Boven");
ucg.setPrintPos(70,60);
ucg.print(temp1);
drawTempInColour(50, 60, temp1, GetColourForTemp(temp1));

ucg.setColor(20, 255, 20);
ucg.setPrintPos(5,80);
ucg.print("midden");
ucg.setPrintPos(70,80);
ucg.print(temp2);

ucg.setColor(20, 255, 20);
ucg.setPrintPos(5,100);
ucg.print("midden");
ucg.setPrintPos(70,100);
ucg.print(temp3);

ucg.setColor(20, 255, 20);
ucg.setPrintPos(5,120);
ucg.print("Onder");
ucg.setPrintPos(70,120);
ucg.print(temp4);
delay(500);
}


void drawTempInColour(byte x, byte y,float temp,unsigned int colour)

{
char buf[16];
dtostrf(temp,6,1,buf);
ucg.print(x, y, buf,colour);  
}

unsigned int GetColourForTemp(float temp)
{
unsigned int Colour = 0, 0, 255; //dark blue
if (temp > 75)
  {
    Colour = 255, 0, 0; //red
  }
else if (temp > 60)
  {
    Colour = 255, 140, 0; //dark orange
  }
else if (temp > 40)
  {
    Colour = 255, 255, 0; //yellow
  }
else if (temp > 30)
  {
    Colour = 65,105, 225; //royal blue
  }
return Colour;
}

Can someone help me out here?

Thank you!

Peter

Can someone help me out here?

Without seeing a link to the "UCG lib"? Not likely.

PaulS:
Without seeing a link to the "UCG lib"? Not likely.

Sorry about that. Here is the link: GitHub - olikraus/ucglib: Arduino True Color Library for TFTs and OLEDs

It looks like that library doesn't have an equivalent function. You'll need to use the setColor, setPrintPos and print functions to achieve the same thing, much as you have in your loop function.

Note: unsigned int Colour = 0, 0, 255; //dark blue
probably doesn't do what you expect. You can't asign three values to a single integer. I believe the first two number in this comma expression are ignored.

Look at the examples for your library to see how colors are stored and declared.

johnwasser:
Note: unsigned int Colour = 0, 0, 255; //dark blue
probably doesn't do what you expect. You can't asign three values to a single integer. I believe the first two number in this comma expression are ignored.

Look at the examples for your library to see how colors are stored and declared.

okay, that may be a new issue then. However: if I block the code below

dtostrf(temp,6,1,buf);
ucg.print(x, y, buf,colour);  
}

I get the same compile error

This compiles without error:

// Peter van de Pol, Wild Bill & many others :)

#include <SPI.h>
#include "Ucglib.h"
#include <OneWire.h>
#include <DallasTemperature.h>

Ucglib_ST7735_18x128x160_HWSPI ucg(/*cd=*/ 9 , /*cs=*/ 10, /*reset=*/ 8);

enum {DARK_BLUE, RED, DARK_ORANGE, YELLOW, ROYAL_BLUE} Colour = DARK_BLUE;

byte colors[][3] =
{
  {0, 0, 255},   //dark blue
  {255, 0, 0},   //red
  {255, 140, 0}, //dark orange
  {255, 255, 0}, //yellow
  {65, 105, 225} //royal blue
};

// Data Sensors to Arduino pin 2
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/* Display wiring including resistors
  1. RST >1KΩ> 8
  2. CE >1KΩ> 10
  3. D/C >1KΩ> 9
  4. DIN >1KΩ> 11
  5. Clk >1KΩ> 13
  6. VCC +5V
  7. BL >100Ω> +5V
  8. GND GND
*/

void setup(void)
{
  delay(1000);
  ucg.begin(UCG_FONT_MODE_TRANSPARENT);

  //ucg.setFont(ucg_font_ncenR12_tr);
  //ucg.setColor(255, 0, 0);
  //ucg.setPrintPos(10,25);
  //ucg.print("Starting 1-2-3");

  ucg.clearScreen();
  // Start up the Sensors library
  sensors.begin();
  // set the Sensor resolution to 9 bit - Valid values are 9, 10, or 11 bit.
  sensors.setResolution(9);
  delay(500);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  sensors.requestTemperatures(); // Get temperatures
  // String sensor4 = String(sensors.getTempCByIndex(1),DEC); //DEC returns 10 decimals Change to 1 below
  float temp1 = sensors.getTempCByIndex(0);
  float temp2 = sensors.getTempCByIndex(1);
  float temp3 = sensors.getTempCByIndex(2);
  float temp4 = sensors.getTempCByIndex(3);
  Serial.println(temp1);
  Serial.println(temp2);
  Serial.println(temp3);
  Serial.println(temp4);
  Serial.println(   );
  delay(100);

  ucg.clearScreen();


  ucg.setFont(ucg_font_helvB12_tr);
  ucg.setColor(255, 255, 255);
  ucg.setPrintPos(10, 25);
  ucg.print("Buffervat");

  ucg.setFont(ucg_font_helvB10_tr);
  ucg.setColor(0, 0, 145);
  ucg.setPrintPos(5, 60);
  ucg.print("Boven");
  ucg.setPrintPos(70, 60);
  ucg.print(temp1);
  drawTempInColour(50, 60, temp1, GetColourForTemp(temp1));

  ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 80);
  ucg.print("midden");
  ucg.setPrintPos(70, 80);
  ucg.print(temp2);

  ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 100);
  ucg.print("midden");
  ucg.setPrintPos(70, 100);
  ucg.print(temp3);

  ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 120);
  ucg.print("Onder");
  ucg.setPrintPos(70, 120);
  ucg.print(temp4);
  delay(500);
}

void drawTempInColour(byte x, byte y, float temp, unsigned int colour)
{
  char buf[16];
  dtostrf(temp, 6, 1, buf);
  ucg.setColor(colors[colour][0], colors[colour][1], colors[colour][2]);
  ucg.setPrintPos(x, y);
  ucg.print(buf);
}

unsigned int GetColourForTemp(float temp)
{
  unsigned int Colour = DARK_BLUE;
  if (temp > 75)
  {
    Colour = RED;
  }
  else if (temp > 60)
  {
    Colour = DARK_ORANGE;
  }
  else if (temp > 40)
  {
    Colour = YELLOW;
  }
  else if (temp > 30)
  {
    Colour = ROYAL_BLUE;
  }
  return Colour;
}

johnwasser:
This compiles without error:

// Peter van de Pol, Wild Bill & many others :)

#include <SPI.h>
#include "Ucglib.h"
#include <OneWire.h>
#include <DallasTemperature.h>

Ucglib_ST7735_18x128x160_HWSPI ucg(/cd=/ 9 , /cs=/ 10, /reset=/ 8);

enum {DARK_BLUE, RED, DARK_ORANGE, YELLOW, ROYAL_BLUE} Colour = DARK_BLUE;

byte colors[][3] =
{
  {0, 0, 255},  //dark blue
  {255, 0, 0},  //red
  {255, 140, 0}, //dark orange
  {255, 255, 0}, //yellow
  {65, 105, 225} //royal blue
};

// Data Sensors to Arduino pin 2
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/* Display wiring including resistors
  1. RST >1KΩ> 8
  2. CE >1KΩ> 10
  3. D/C >1KΩ> 9
  4. DIN >1KΩ> 11
  5. Clk >1KΩ> 13
  6. VCC +5V
  7. BL >100Ω> +5V
  8. GND GND
*/

void setup(void)
{
  delay(1000);
  ucg.begin(UCG_FONT_MODE_TRANSPARENT);

//ucg.setFont(ucg_font_ncenR12_tr);
  //ucg.setColor(255, 0, 0);
  //ucg.setPrintPos(10,25);
  //ucg.print("Starting 1-2-3");

ucg.clearScreen();
  // Start up the Sensors library
  sensors.begin();
  // set the Sensor resolution to 9 bit - Valid values are 9, 10, or 11 bit.
  sensors.setResolution(9);
  delay(500);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  sensors.requestTemperatures(); // Get temperatures
  // String sensor4 = String(sensors.getTempCByIndex(1),DEC); //DEC returns 10 decimals Change to 1 below
  float temp1 = sensors.getTempCByIndex(0);
  float temp2 = sensors.getTempCByIndex(1);
  float temp3 = sensors.getTempCByIndex(2);
  float temp4 = sensors.getTempCByIndex(3);
  Serial.println(temp1);
  Serial.println(temp2);
  Serial.println(temp3);
  Serial.println(temp4);
  Serial.println(  );
  delay(100);

ucg.clearScreen();

ucg.setFont(ucg_font_helvB12_tr);
  ucg.setColor(255, 255, 255);
  ucg.setPrintPos(10, 25);
  ucg.print("Buffervat");

ucg.setFont(ucg_font_helvB10_tr);
  ucg.setColor(0, 0, 145);
  ucg.setPrintPos(5, 60);
  ucg.print("Boven");
  ucg.setPrintPos(70, 60);
  ucg.print(temp1);
  drawTempInColour(50, 60, temp1, GetColourForTemp(temp1));

ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 80);
  ucg.print("midden");
  ucg.setPrintPos(70, 80);
  ucg.print(temp2);

ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 100);
  ucg.print("midden");
  ucg.setPrintPos(70, 100);
  ucg.print(temp3);

ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 120);
  ucg.print("Onder");
  ucg.setPrintPos(70, 120);
  ucg.print(temp4);
  delay(500);
}

void drawTempInColour(byte x, byte y, float temp, unsigned int colour)
{
  char buf[16];
  dtostrf(temp, 6, 1, buf);
  ucg.setColor(colors[colour][0], colors[colour][1], colors[colour][2]);
  ucg.setPrintPos(x, y);
  ucg.print(buf);
}

unsigned int GetColourForTemp(float temp)
{
  unsigned int Colour = DARK_BLUE;
  if (temp > 75)
  {
    Colour = RED;
  }
  else if (temp > 60)
  {
    Colour = DARK_ORANGE;
  }
  else if (temp > 40)
  {
    Colour = YELLOW;
  }
  else if (temp > 30)
  {
    Colour = ROYAL_BLUE;
  }
  return Colour;
}

Hero! Will check with sensors later.

Update: My code is compiling fine nowbut the colour of the text does not change yet to to colour relative to the temperature. How do I tackle this?

My sketch now:

// Peter van de Pol, Wild Bill John Wasser & many others :)

#include <SPI.h>
#include "Ucglib.h"
#include <OneWire.h>
#include <DallasTemperature.h>

Ucglib_ST7735_18x128x160_HWSPI ucg(/*cd=*/ 9 , /*cs=*/ 10, /*reset=*/ 8);

enum {DARK_BLUE, RED, DARK_ORANGE, YELLOW, ROYAL_BLUE} Colour = DARK_BLUE;

byte colors[][3] =
{
  {0, 0, 255},   //dark blue
  {255, 0, 0},   //red
  {255, 140, 0}, //dark orange
  {255, 255, 0}, //yellow
  {65, 105, 225} //royal blue
};

// Data Sensors to Arduino pin 2
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/* Display wiring including resistors
  1. RST >1KΩ> 8
  2. CE >1KΩ> 10
  3. D/C >1KΩ> 9
  4. DIN >1KΩ> 11
  5. Clk >1KΩ> 13
  6. VCC +5V
  7. BL >100Ω> +5V
  8. GND GND
*/

void setup(void)
{
  //delay(1000);
 
 ucg.begin(UCG_FONT_MODE_TRANSPARENT);
 ucg.setFont(ucg_font_ncenR12_tr);
 ucg.setColor(255, 0, 0);
 ucg.setPrintPos(10,25);
 ucg.print("Starting 1-2-3");
// delay(500);
 
  ucg.clearScreen();
  // Start up the Sensors library
  sensors.begin();
  // set the Sensor resolution to 9 bit - Valid values are 9, 10, or 11 bit.
  sensors.setResolution(9);
  delay(500);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  sensors.requestTemperatures(); // Get temperatures
  // String sensor4 = String(sensors.getTempCByIndex(1),DEC); //DEC returns 10 decimals Change to 1 below
  float temp1 = sensors.getTempCByIndex(0);
  float temp2 = sensors.getTempCByIndex(1);
  float temp3 = sensors.getTempCByIndex(2);
  float temp4 = sensors.getTempCByIndex(3);
  Serial.println(temp1);
  Serial.println(temp2);
  Serial.println(temp3);
  Serial.println(temp4);
  Serial.println(   );
 // delay(500);

  ucg.clearScreen();


  ucg.setFont(ucg_font_helvB12_tr);
  ucg.setColor(255, 255, 255);
  ucg.setPrintPos(10, 25);
  ucg.print("Buffervat");

  ucg.setFont(ucg_font_helvB10_tr);
   ucg.setPrintPos(5, 60);
  ucg.print("Boven");
  ucg.setPrintPos(70, 60);
  ucg.print(temp1);
 
//  ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 80);
  ucg.print("midden");
  ucg.setPrintPos(70, 80);
  ucg.print(temp2);

 // ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 100);
  ucg.print("midden");
  ucg.setPrintPos(70, 100);
  ucg.print(temp3);

 // ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 120);
  ucg.print("Onder");
  ucg.setPrintPos(70, 120);
  ucg.print(temp4);
  delay(5000);
}

void drawTempInColour(byte x, byte y, float temp, unsigned int colour)
{
  char buf[16];
  dtostrf(temp, 6, 1, buf);
  ucg.setColor(colors[colour][0], colors[colour][1], colors[colour][2]);
  ucg.setPrintPos(x, y);
  ucg.print(buf);
}

unsigned int GetColourForTemp(float temp)
{
  unsigned int Colour = DARK_BLUE;
  if (temp > 75)
  {
    Colour = RED;
  }
  else if (temp > 60)
  {
    Colour = DARK_ORANGE;
  }
  else if (temp > 40)
  {
    Colour = YELLOW;
  }
  else if (temp > 30)
  {
    Colour = ROYAL_BLUE;
  }
  return Colour;
}

You've dropped the critical line from John's sketch:

  ucg.print("Boven");
  ucg.setPrintPos(70, 60);
  ucg.print(temp1);
  drawTempInColour(50, 60, temp1, GetColourForTemp(temp1));

That drawTempInColour function is what renders the temperature in colour.

I suspect though that it writes the temp twice. Try:

  ucg.print("Boven");
  drawTempInColour(50, 60, temp1, GetColourForTemp(temp1));

You are right:

I solved the problem like this (Boven untouched more or less, Midden etc. changed)

  ucg.setFont(ucg_font_helvB10_tr);
  ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 60);
  ucg.print("Boven");
  drawTempInColour(70, 60, temp1, GetColourForTemp(temp1));
  ucg.setPrintPos(70, 60);
  ucg.print(temp1);
  
  
  ucg.setColor(20, 255, 20);
  ucg.setPrintPos(5, 80);
  ucg.print("midden");
  drawTempInColour(70, 80, temp2, GetColourForTemp(temp2));
 // ucg.setPrintPos(70, 80);
//  ucg.print(temp2);

It is all working now. Thank you so much!!