Jump to content

Arduino Help - convert to hex


blinky

Recommended Posts

I'm trying to output the temp from my DS18B20 sensor to the serial output but also convert it to hex and also add # to the end.  So if the sensor outputs 10 degrees as a number I want it to print 0A# to the serial device.

I have been playing around for the past couple of hours and cannot figure it out!  I thought printf might be the way to go but no joy getting that working!  Whats confusing me at the moment is that the first line prints the correct temp but the 2nd line prints nothing.  Can somebody please help?

 

  
     

void loop() {
  sensors.requestTemperatures();
  tempC = sensors.getTempCByIndex(0);
  delay(1000);
  
  Serial.print("C: ");
  Serial.print(tempC);

Serial.println(printf("%2X", tempC ) );

}

 

Link to comment
Share on other sites

This is the full sketch:

 

#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal.h>


// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Data wire is plugged into pin 10 on the Arduino
#define ONE_WIRE_BUS 10

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

float tempC = 0;
float tempF = 0;

void setup(void) {
  Serial.begin(9600);
    lcd.begin(16, 2);
    // declare pin 9 to be an output for buzzer:
    pinMode(9, OUTPUT);
    beep(50);
    beep(50);
    beep(50);
    delay(1000);
    Serial.println("Dallas Temperature IC Control Library Demo");
    // Start up the library
    sensors.begin();
}


 void loop(void)
 {
     // call sensors.requestTemperatures() to issue a global temperature
     // request to all devices on the bus
     /********************************************************************/
     Serial.print(" Requesting temperatures...");
     sensors.requestTemperatures(); // Send the command to get temperature readings
     Serial.println("DONE");
     /********************************************************************/
     //Serial.print("Temperature is: ");
     //Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
     // You can have more than one DS18B20 on the same bus.
     // 0 refers to the first IC on the wire
     delay(1000);
     //beep(10);
     
sensors.requestTemperatures();
tempC = sensors.getTempCByIndex(0);
tempF = sensors.toFahrenheit(tempC);
delay(1000);

//Serial.println(tempC);

char buffer[4];

snprintf(buffer, 4, "%02x", tempC);
Serial.println(buffer);

Serial.print(sensors.getTempCByIndex(0));


lcd.setCursor(0,0);
lcd.print("C: ");
lcd.print(tempC);
lcd.print(" degrees");
lcd.setCursor(0,1);
lcd.print("F: ");
lcd.print(tempF);
lcd.print(" degrees");
 
}

void beep(unsigned char delayms){
  analogWrite(9, 20);      // Almost any value can be used except 0 and 255
                           // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(9, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms   
}
 

Link to comment
Share on other sites

So if I do this:

int testTemp = tempC;
Serial.println(testTemp, HEX );

It will output 1D for 29 degrees celcius which is what I want :-) THe only problem left is it needs to send # at the end but it auto adds a new line so if I do a Serial.Println("#")  I get it on the next line

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.