Go Down

Topic: Printing Char from DEC in serial (Read 983 times) previous topic - next topic

taswanj

Jul 10, 2012, 05:16 pm Last Edit: Jul 10, 2012, 09:04 pm by taswanj Reason: 1
Hello all, I'm betting this is an easy one but there's something I'm not grasping. 1st off, I'm sending data over XBee in the form of a variable. Data is coming from a DS18B20 Temperature sensor to an Arduino. I'm using the Dallas Temperature sensor library. I named the device grabbing temperature in direct sunlight 'suntemp', I will later incorporate another temp sensor in the shade to grab 'shade temp'...first things first though. Here's the code grabbing the variable 'suntemp':

Code: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

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

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress shadeTemp = { 0x28, 0x94, 0xE2, 0xDF, 0x02, 0x00, 0x00, 0xFE };
DeviceAddress sunTemp = { 0x28, 0x9D, 0x56, 0xEB, 0x03, 0x00, 0x00, 0xD5 };


void setup(void)
{
 // start serial port
 Serial.begin(9600);
 // Start up the library
 sensors.begin();
 // set the resolution to 10 bit (good enough?)
 sensors.setResolution(sunTemp, 10);
 sensors.setResolution(shadeTemp, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
 float tempC = sensors.getTempC(deviceAddress);
 if (tempC == -127.00) {
   Serial.print("Error getting temperature");
 } else {
       
   Serial.println(DallasTemperature::toFahrenheit(tempC));
   
 }
}

void loop(void)
{
 delay(1000);
 sensors.requestTemperatures();
 //Serial.println("SunTemp: ");
 printTemperature(sunTemp);
 //Serial.print("\n\r\n\r");
}


So far on the send side in the serial monitor, the temp is reading and printing correctly and it's 75.20  
Here's the code on the receiver side:

Code: [Select]
void setup() {
       Serial.begin(9600);
       Serial.print("testing Xbees");

                            // opens serial port, sets data rate to 9600 bps
}

void loop() {

       // send data only when you receive data:
       if (Serial.available() > 0)
              {
               //val = Serial.read();                
               Serial.println("is this correct?   ");
               Serial.println(Serial.read());  
             }
}


Serial gives me 55,53,46,50,48,13,10
which is 75.20 in ASCII, but I want to display this to an LCD as Characters. I'm not sure of how to alter the code so it prints the data in Character form.
Can someone help. Thanks much.

Grumpy_Mike

ASCII is the correct form to send to an LCD so there is nothing to do, just send these values to the LCD as an LCD.write()

taswanj

One million apologies. I am wrong in my description of what's happening.

Correction. I am getting the values back in DEC in the 'receiver' sides serial monitor that I would like to display as normal Characters.

'Send' sides serial monitor is displaying the characters. 75.20


taswanj

So I guess the real question is how to print a variable as Char over serial. I apologize if I'm missing something very elementary here, I'm a bit new to programming in Arduino.

wd40bomber7

You are using
Code: [Select]

Serial.println(Serial.read());

To print your characters right? The issue is that Serial.read() returns an int (I'm betting). The overload for println reads the int, and assumes you're doing something like
Code: [Select]

Serial.println(1337);

In this situation you would obviously want to print the number 1337, not a character.

So to make sure println understands what you're trying to give it try this
Code: [Select]

Serial.println((char)Serial.read());

Then a character should be printed correctly. The (char) is called a typecast and forces the integer to become a character.

I'd test this but I don't have an Arduino on hand unfortunately.

Grumpy_Mike

Quote
The issue is that Serial.read() returns an int (I'm betting).

You loose your bet, it returns the byte that was sent, normally an ASCII value but it depends what was sent.

PaulS


Quote
The issue is that Serial.read() returns an int (I'm betting).

You loose your bet, it returns the byte that was sent, normally an ASCII value but it depends what was sent.

No, he wouldn't. The return type for Serial.read() is an int. In general, the data is in the low order byte. The error code, if any, is in the high order byte.

The Serial.print() overload for int does not know that only the low order byte contains data of interest, as a char, so it doesn't print the value that way.

You can do several things. One is to store the low order byte only in a char variable, then print the char variable. Another is to cast the Serial.read() output as a char, when printing.

taswanj

To: wd40bomber7, that was it! Thanks for teaching me something. That forced the 'receiver' side to print out the data in the correct CHAR format. Thanks a ton, I can think of many other ways to use this casting now.

Thanks a ton.

Go Up