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);
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