Single pin sensor Vs OLED screen HELP!

Forum for posting topics and questions about anything.
Post Reply
AlanWooler
Posts: 10
Joined: Thu Dec 04, 2014 9:30 pm

Single pin sensor Vs OLED screen HELP!

Post by AlanWooler » Fri Dec 05, 2014 5:41 pm

Hi all,
I am new to Arduino and am throwing myself into the deep end. I have a one-wire temperature sensor and can get it to give me reading (connected to an Uno) to my serial port. I also have a "SSD1306 128x64 Pixel OLED Display Module" and have nice font and text placement.
The issue I am having is trying to get the temperature reading on the display. I am used to Visual Basic 6, and sadly not this. I have tried declaring a string and then calling it, but I now know I am doing it very wrong and need some help please:

All my code is below which is the sample code for the display (adapted by me) then the code for the 1-wire temperature sensor code (also adapted by me)

Code: Select all

#include <OneWire.h>
#include "U8glib.h"

#define CS_PIN 10

OneWire  ds(7);  // on pin 10 (a 4.7K resistor is necessary)

U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9);



void setup(void) {
  Serial.begin(9600);
  
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  char s[6] = "";
  int inputtemperature;
  
  
  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }
  
//  Serial.print("ROM =");
//  for( i = 0; i < 8; i++) {
//    Serial.write(' ');
//    Serial.print(addr[i], HEX);
//  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
//  Serial.println();
 
  switch (addr[0]) {
    case 0x10:
      type_s = 1;
      break;
    case 0x28:
      type_s = 0;
      break;
    case 0x22:
      type_s = 0;
      break;
    default:
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad


  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }

  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);

    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms

  }
  celsius = (float)raw / 16.0;
  inputtemperature=(float)raw / 16.0;
//  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
//  Serial.print(celsius);
  Serial.print(inputtemperature);
  Serial.println(" C");
//  Serial.println("");




u8g.firstPage();  
  do 
  {
    u8g.setFont(u8g_font_tpss);


                
//      u8g.drawStr( 1, 10, "22.9");
      u8g.drawStr( 1, 10, "#");
      u8g.drawStr( 1, 40, "C");
//      u8g.drawStr( 1, 39, "");
//      u8g.drawStr( 1, 53, "    Ran for: 3h 22m");
 
  }while(u8g.nextPage());

  

}



What I have an issue with so it says is my use of a variable/integer as I declare it, then tell it to store the value of celsius into this variable, display it to the serial port (which works fine) but then when I tell it to print it to the screen it fails. My code is:

Code: Select all

int inputtemperature;
inputtemperature=(float)raw / 16.0;
Serial.print(inputtemperature);
(the above is taken from different section of the main code)

So, the above works to serial port (as a whole figure rounded up, no decimal points for some reason) but when I use the code "u8g.drawStr( 1, 10, inputtemperature);" it comes up with the error "call of overloaded 'drawStr(int, int, int&)' is ambiguous".

Can anybody help me out with my string/temporary storage of the temperature value and then printing to my screen.
Sorry, asking to much I know, but I am such a noob at this.

Thanks all
Alan

andrew
Site Admin
Posts: 1376
Joined: Sun Aug 05, 2012 4:15 pm

Re: Single pin sensor Vs OLED screen HELP!

Post by andrew » Sat Dec 06, 2014 10:11 am

Hi Alan, welcome to the forum. You are indeed jumping into the deep end here with a lot of things going on in your sketch for you to understand. Before I attempt to answer your question I do have a comment to make about your main loop. As you have already figured out the loop() function is where you put all your code that you want to be repeatedly executed. This function is repeatedly called by the Arduino bootloader. But noticed I called it a function, and like every other function any variables you define in it will get created and then destroyed each time the loop is run. So if you have any variables in your main loop that you don't want to lose the contents of each time the loop is exited then you should move the definitions for them out of the loop() function and place them at the top of your sketch. That way they will not get constantly destroyed and recreated. It also means that they will become globally accessible anywhere in your sketch. As a result it will allow you to move any bits of code that don't need to be repeatedly run out of the main loop() function and into the setup() function. Have a go at moving them. You may still find you are having problems with your code just repost what you have.

To answer your question the problem is you are trying to pass an integer type variable to the drawStr() function which is expecting it to be of type string. Try using the built in Arduino function called String() to try and convert your integer to a string. I've not tested this but give this a go:

Code: Select all

u8g.drawStr( 1, 10, String(inputtemperature));
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

AlanWooler
Posts: 10
Joined: Thu Dec 04, 2014 9:30 pm

Re: Single pin sensor Vs OLED screen HELP!

Post by AlanWooler » Mon Dec 08, 2014 9:09 pm

Thank you Andrew for your help, it really is appreciated.
I moved the definition to the top of the code as you suggested, so now it is:

Code: Select all

#include <OneWire.h>
#include "U8glib.h"

#define CS_PIN 10

OneWire  ds(7);  // on pin 10 (a 4.7K resistor is necessary)

U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9);


int inputtemperature;


void setup(void) {
  Serial.begin(9600);
  
}


void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  char s[6] = "";
  
  
  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }
  
//  Serial.print("ROM =");
//  for( i = 0; i < 8; i++) {
//    Serial.write(' ');
//    Serial.print(addr[i], HEX);
//  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
//  Serial.println();
 
  switch (addr[0]) {
    case 0x10:
      type_s = 1;
      break;
    case 0x28:
      type_s = 0;
      break;
    case 0x22:
      type_s = 0;
      break;
    default:
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad


  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }

  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);

    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms

  }
  celsius = (float)raw / 16.0;
  inputtemperature=(float)raw / 16.0;
//  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
//  Serial.print(celsius);
  Serial.print(inputtemperature);
  Serial.println("C");
//  Serial.println("");




u8g.firstPage();  
  do 
  {
    u8g.setFont(u8g_font_tpss);


                
//      u8g.drawStr( 1, 10, "22.9");
//      u8g.drawStr( 1, 10, "#");
      u8g.drawStr( 1, 10, String(inputtemperature));
      u8g.drawStr( 1, 40, "C");
//      u8g.drawStr( 1, 39, "");
//      u8g.drawStr( 1, 53, "    Ran for: 3h 22m");
 
  }while(u8g.nextPage());

  

}

It now though comes up with the below error:
no matching function for call to 'U8GLIB_SSD1306_128x64::drawStr(int, int, String'

I am going to do a little research to try and learn a bit more about declaring strings and integers, but if anybody can offer me help, I would be very greatful.

Thanks
Alan

andrew
Site Admin
Posts: 1376
Joined: Sun Aug 05, 2012 4:15 pm

Re: Single pin sensor Vs OLED screen HELP!

Post by andrew » Tue Dec 09, 2014 10:21 am

I've just taken a look at the u8glib manual and it does seem to support a print function. Can you try replacing that line with the following:

Code: Select all

u8g.setPrintPos(1, 10);    //Set x,y location for print function
u8g.print(inputtemperature);    //Print the temperature 
I don't currently have one set up so again I can't check it at the moment but the above does at least compile without errors.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

AlanWooler
Posts: 10
Joined: Thu Dec 04, 2014 9:30 pm

Re: Single pin sensor Vs OLED screen HELP!

Post by AlanWooler » Tue Dec 09, 2014 11:58 pm

You are a star Andrew, that worked and worked a dream:

[YouTube]https://www.youtube.com/watch?v=PY_Xf3Zozfs&feature[/YouTube]

I have also added 2 buttons that adjust a desired temperature 1 degree at a time. The idea is that the user can set a minimum temperature and when the actual temperature goes lower than the set one, it will switch a relay.

Image

The program is almost ready, also I think the declaration of the input temperature is not an integer so when I try to see if it is equal, lower o higher than the required temperature it will make pin 4 high which will trigger a relay. This is not happening. Don't worry, I am not asking for your help as I am sure I can Google it and find an answer, but wanted to let you know how it was going.

Code: Select all

 if (inputtemperature >= 19) {        
      u8g.setFont(u8g_font_timB24);
      u8g.drawStr( 10, 10, "########");
  } 
  else {
    // do nothing
  }
Thank you again. I love this screen so much no doubt I will be buying more from you as well as more of the one wire temperature probes.

andrew
Site Admin
Posts: 1376
Joined: Sun Aug 05, 2012 4:15 pm

Re: Single pin sensor Vs OLED screen HELP!

Post by andrew » Wed Dec 10, 2014 12:18 pm

That's great. You project is looking good already. I can't seen anything wrong with the snippet of code that you have posted. If you have no luck figuring out the problem post your complete sketch. The problem maybe somewhere else in your code.

Don't forget we still owe you a DHT11 ;)
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

AlanWooler
Posts: 10
Joined: Thu Dec 04, 2014 9:30 pm

Re: Single pin sensor Vs OLED screen HELP!

Post by AlanWooler » Wed Dec 10, 2014 12:33 pm

Thanks Andrew,
I will hopefully be placing an order in your shop for some supplies and will collect from you tomorrow afternoon.
Having fun as am currently working on this. Then the next project is working with one of your Real Time Clocks, then SD card reading (and writing if possible). Got a few projects to work on, but not enough time, money or experience. What joys. I do love this little OLE screen though, so easy to use & well worth the money.

Post Reply

Return to “General Discussion”