Go Down

Topic: Serial Communication (Read 2 times) previous topic - next topic


The reason is for the output of an understanding of one value from the incoming bytes. I understand that functions in a float structure can be used but I could not get a code to run perfoming this conversion to produce a value of -1g for example.

I will do that. I was not sure how you posted code-i checked off the box near the bottom of the posting but I never got a popup box to do so. I understand now

Thank you


You do not need to get bogged down in the maths of the floating point number.  My code example shows how to change the 4 bytes received to a float.  Its very similar to the example from the website, which you also found.  The technique is to put the 4 received bytes at the same addresses as the float variable, then ask the computer to interpret the float value.  The arduino already 'knows' that if it is given a float, how to manipulate it.  You don't have to know.

look at this code, from your example:
Code: [Select]

for (i=1; i<5; i++) {
incomingByte = (float)Serial.read();

i = 1.
Serial.read() returns a byte - 8 bits, say 00101001.  That example is hex2A, or decimal 42 (32 + 10).   If you cast that to a float - the (float) operation - you get a floating number with a value of 42.0  It is represented as 4 bytes, and using the www.h-schmidt.net/FloatApplet/IEEE754.html website, the values are probably 42280000.
That number, 42.0 is assigned to incomingByte overwriting any previous value of incomingByte.
i = 2.
Serial.read() returns a byte - 8 bits, say 00000001.    If you cast that to a float - the (float) operation - you get a floating number with a value of 1.0  It is represented as 4 bytes, and you can work out what they are but it doesn't matter.  That number, 1.0 is assigned to incomingByte - IT DOES NOT ADD TO IT.
much the same
much the same
exits loop

It will end up with the (floating) value of the last of the 4 bytes read.

You need to write the 4 bytes into a byte array and then use those 4 bytes as a float, as per my code.  A cast - which is what the (float) operation is called - will not work.  The sketch I wrote a few nights back tries to do that.

Please also answer the question of how the device is connected, because I am interested in how the single serial input pin is used for the device but the serial out goes both to the computer AND the device.  That is not a way of connecting hardware that I have met before.

The reason I tried to write the last sketch with a loop, is that you can  shake the device up and down, and see if the values are changing.  With a one shot, its going to be difficult to alter the acceleration at the exact time you want the reading.  At least for testing, try using a loop to read the values repeatedly.  Then convert to your one-shot code.


I was just confused since I received 4 zeros in the output since I was expecting one final value. Would you say the last code posted with your code incorporated was done correctly? I think I am getting the float part but was just not sure with what I was receiving. Is there a way to extend the decimals to more than 2 places to see if the data is 0.005 for example?

The setup is through the usage of pins 0-1 on the Duemilanove. Using the USB cable to upload the coding to the board, then once the code is running, the command byte is transmitted on pin 1 and received on pin 0. To my understanding of the board, the transmission pin works in correspondance to the USB cable to print the information on the computer screen to validate the data.

Thank you


Well I am finished now. I was successfully able to get the unit to convert the byte to a float value which I can confirm and makes sense.

Thank you all so very much for progress and promptness the whole way through. Our project isn't fully complete but this portion is ready for integration in my perspective.

Thank you again. Especially Shelleycat for all your coding (I dont have any idea how long those took to write up) but it all really helped me progress and learn what I needed to do along the way.


One last concern is since we have an Arduino Duemilanove which contains a USB port, can we print the serial data (currently written to a computer screen via usb) to a a USB thumb drive through a USB type A to type B cable. Are there certain write functions that are needed or must you use a ftdi viniculum chip?


Go Up