Hello,
Working on a project where one Arduino Nano measures the RPM from a hall sensor and sends that value to a Mega via the TX and RX pins. The code used to measure the RPM outputs the value as a float. I cant figure out how to Serial.write a float value to be read on the Mega via the Serial Communication. I've tried to convert the float to a integer and then send it, but the values once converted are incorrect. Im pretty new to coding, and keep seeing the use of char arrays and atoi in other forums but its all a bit confusing and nothing has worked so far. I concerned about write and read speeds within the loops, so if there a simple and easy way to transmit these values, that would be great. Heres the code I have this far:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Sending data as text makes debugging much easier and I would only send binary data if it was essential for project performance.
The Arduino millis() function produces an unsigned long value. I suspect you will make life much easier or yourself if you stick with those except when you need to produce a value for display to a human. In other words, send unsigned-longs rather than floats.
Check if the following sketches work (untested): Setup:
DPin-2 of NANO (SRX) <------------- DPin-18 of MEGA (TX1)
DPin-3 of NANO (STX) --------------> DPin-19 of MEGA (RX1)
GND of NANO <---------------------> GND of MEGA
cjr7315:
Just figured it out rn, used a if statement to print the rpm above the filtering threshold
Thanks for all of your guys help!
Have you noted down the comment of Post#5 on micros()? Please, give some time to sort it out -- why the data type of this variable: end_time should be unsigned long rather than float though float also assigns 32-bit space for the said variable.
Seems I'm late to the party, but you should consider using a robust serial transfer library to packetize and parse your data automatically. You can even send floats if you do some messing around with pointers and such.
You can find the library in the Arduino IDE's "Libraries Manager" under "SerialTransfer".
P.S. I know several people who posted here have already seen this library but seem to refuse to suggest it to people who might benefit from it. If there is some constructive criticism of the library, I would be happy to hear it. Just not sure why such a useful library is being largely ignored...
Power_Broker:
Seems I'm late to the party, but you should consider using a [...]
We may address the data type issue of OP:
end_time variable should be declared as unsigned long instead of float. Now, the OP complains that such declaration provides him incorrect RPM reading.