Keyboard Shortcuts
Likes
Search
Help!
I need a programming guru to look at my code and suggest the best way to convert tIhe?numeric data to ASCII. I am awaiting Dr Jack's book so I can learn the syntax. Right now I am in deepest Africa craving a beer and have no idea of the correct words to achieve my goal.
?/*
? A routine to convert from longitude and latitude to maidenhead grid squares
? Fred Hambrecht W4JLE
? */
?
void setup(){
Serial.begin(115200);
String grid (6);
String alpha = "ABCDEFGHIJLKMNOPQRSTUVWXYZ";
String num = "0123456789";
}
?
void loop()?
{
? int grid;
? int lon1, lon2, lon3;
? int lat1, lat2, lat3;
? double interim;
? double mylat;
? double mylon;
??
?
? //Example my location -81.401 33.904
? // longitude
? mylon = -81.401; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //static value for test
? interim = mylon + 180.0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// -81.401 + 180 = 98.599
? lon1 = (int)(interim / 20.0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 98/20 = 4 Field Letter A+4 = E |depending on method 1 must be added to the value
? interim = interim - (double)lon1 * 20.0; ? ? ? ? ? ?// 98.599 - 4*20 = 18.599 ? ? ? ? |if I can fine a way to convert number to ASCII just add 65
? lon2 = (int)(interim *.5); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 18/2 = 9 Square Number ? ? ? ? |48 or 97
? interim = interim - 2.0 * (double)lon2; ? ? ? ? ? ? ?// 18.599 - 2*9 = .599
? lon3 = (int)(12.0 * interim); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // .599 * 12 = 7 index to letter a+7 =h ?Sub Square letter
?
? // latitude
? mylat = 33.904; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //static value for test
? interim = mylat + 90.0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 33.904 + 90 = 123.903
? lat1 = (int)(interim / 10.0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 123/10 = 12 Field =M
? interim = interim - (double)mylat ?* 10.0; ? ? ? ? // 123.904 - 12*10 = 3.904
? lat2 = (int)(interim); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 3 Square =3
? interim = interim - (double)lat2; ? ? ? ? ? ? ? ? ? ? ?// 3.123 - 3 =.904
? lat3 = (int)(24.0 * interim); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// .904 * 24 = 21.696 = 21 sub square = v
// Awaiting Dr Jack's book to get learn syntex for arrays and pointers ?
?//Help! I can add 65 to lon1 and lon3 or 48 to lon2 ?and convert to ASCII or add one and point to a letter in the alpha string
// or open to any suggestion. Thanks much, I am tearing what little hair I have left out!
?
}
? |
Dr. Fred This may help: dtostrf is a function that converts a float or double into a character array using only one line of code dtostrf(float, minimum width, precision, character array); text or ASCII.? Thinking seems to be that the overhead was too large and slow if done in the Arduino library. Instead they left it up to using "dtostrf" to do the heavy lifting.? Arv? K7HKL _._ On Sun, Jun 25, 2017 at 2:49 PM, Dr Fred Hambrecht <AAR4MI@...> wrote: I need a programming guru to look at my code and suggest the best way to convert tIhe?numeric data to ASCII. I am awaiting Dr Jack's book so I can learn the syntax. Right now I am in deepest Africa craving a beer and have no idea of the correct words to achieve my goal. |
Hi Dr. Fred.
Arv mentions one specific way to convert. Another, more general way (at the expense of [I expect] more memory) is to use snprintf(destinationBuffer, bufferSize, formatString, data...); This is very similar to the normal "printf()" statement except it sends the "output" to a character buffer rather than the standard output device. "destinationBuffer" is a character string to hold the formatted data, "bufferSize" is the size of the destination buffer (using sizeof() is very useful for this variable), formatString is the normal "printf" style format string, and "data..." is the data to format and place in the buffer. This allows you to format integers, floating point, string, and just about any thing else. Very useful, unless you get really memory constrained. (Then there are other techniques which may be used.) BTW, you will also see "sprintf()" used, but it has the "problem" that is is all too easy to overwrite the destination buffer resulting in general corruption. Something which may be extremely difficult to diagnose. I hope this helps. 73 - Mark N1VQW |
Mark and Dr. Fred Unless it has been fixed in the past few months, the built-in Arduino.h does not implement sprintf(%f).? It does not block...it just does nothing.? This is a long- standing bug in the Arduino IDE that the IDE developers do not see as a problem. Example of the problem:
Arv? K7HKL _._ On Sun, Jun 25, 2017 at 5:41 PM, Mark Pilant <mark@...> wrote: Hi Dr. Fred. |
Jack Purdum
No, it's not a problem for several reasons. First, sprintf() is a very powerful function yet most programs use only a fraction of its power. Result: a lot of wasted memory. Instead, try: char temperature[10];
float temp = 10.55;
//sprintf(temperature,"%f F", temp); dtostrf(temp, 7, 2, temperature);
Serial.println(temperature); The 7 is the digit width of the field for the number while the 2 is the number of digits after the decimal point. The function also uses less memory than sprintf(). Keep in mind that the Arduino IDE does not support the double data type and that the best you can hope for with a float is about 7 digits of precision. Any digits after that it a guess. Jack, W8TEE From: Arv Evans <arvid.evans@...> To: [email protected] Sent: Sunday, June 25, 2017 9:28 PM Subject: Re: [BITX20] Help! Mark and Dr. Fred Unless it has been fixed in the past few months, the built-in Arduino.h does not implement sprintf(%f).? It does not block...it just does nothing.? This is a long- standing bug in the Arduino IDE that the IDE developers do not see as a problem. Example of the problem:
Arv? K7HKL _._ On Sun, Jun 25, 2017 at 5:41 PM, Mark Pilant <mark@...> wrote: Hi Dr. Fred. |
Vince Vielhaber
A year or so ago someone on the arduino forum told me that sprintf is there but turned off to conserve memory. They told me what to do to get it to work but I never could get it to.
toggle quoted message
Show quoted text
There's a package for formatting string buffers called PString, but I don't see it being as much use as a simple sprintf or snprintf. Vince. On 06/25/2017 09:27 PM, Arv Evans wrote:
Mark and Dr. Fred --
Michigan VHF Corp. |
Jack Purdum
Fred: Try this: it avoids using the String class, which is a memory hog. void setup() { ? // put your setup code here, to run once: ? char answer[15]; ? Serial.begin(115200); ? calcLocator(answer, 39.035344, -84.261335); // Enter lat, then lon ? Serial.println(answer); } void calcLocator(char *dst, float lat, float lon) { ? int o1, o2, o3; ? int a1, a2, a3; ? float remainder; ? // longitude ? remainder = lon + 180.0; ? o1 = (int)(remainder / 20.0); ? remainder = remainder - (float)o1 * 20.0; ? o2 = (int)(remainder / 2.0); ? remainder = remainder - 2.0 * (float)o2; ? o3 = (int)(12.0 * remainder); ? // latitude ? remainder = lat + 90.0; ? a1 = (int)(remainder / 10.0); ? remainder = remainder - (float)a1 * 10.0; ? a2 = (int)(remainder); ? remainder = remainder - (float)a2; ? a3 = (int)(24.0 * remainder); ? dst[0] = (char)o1 + 'A'; ? dst[1] = (char)a1 + 'A'; ? dst[2] = (char)o2 + '0'; ? dst[3] = (char)a2 + '0'; ? dst[4] = (char)o3 + 'A'; ? dst[5] = (char)a3 + 'A'; ? dst[6] = (char)0; } void loop() { } Jack, W8TEE From: Dr Fred Hambrecht <AAR4MI@...> To: [email protected] Sent: Sunday, June 25, 2017 4:49 PM Subject: [BITX20] Help! I need a programming guru to look at my code and suggest the best way to convert tIhe?numeric data to ASCII. I am awaiting Dr Jack's book so I can learn the syntax. Right now I am in deepest Africa craving a beer and have no idea of the correct words to achieve my goal. ?/*
? A routine to convert from longitude and latitude to maidenhead grid squares
? Fred Hambrecht W4JLE
? */
?
void setup(){
Serial.begin(115200);
String grid (6);
String alpha = "ABCDEFGHIJLKMNOPQRSTUVWXYZ";
String num = "0123456789";
}
?
void loop()?
{
? int grid;
? int lon1, lon2, lon3;
? int lat1, lat2, lat3;
? double interim;
? double mylat;
? double mylon;
??
?
? //Example my location -81.401 33.904
? // longitude
? mylon = -81.401; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //static value for test
? interim = mylon + 180.0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// -81.401 + 180 = 98.599
? lon1 = (int)(interim / 20.0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 98/20 = 4 Field Letter A+4 = E |depending on method 1 must be added to the value
? interim = interim - (double)lon1 * 20.0; ? ? ? ? ? ?// 98.599 - 4*20 = 18.599 ? ? ? ? |if I can fine a way to convert number to ASCII just add 65
? lon2 = (int)(interim *.5); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 18/2 = 9 Square Number ? ? ? ? |48 or 97
? interim = interim - 2.0 * (double)lon2; ? ? ? ? ? ? ?// 18.599 - 2*9 = .599
? lon3 = (int)(12.0 * interim); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // .599 * 12 = 7 index to letter a+7 =h ?Sub Square letter
?
? // latitude
? mylat = 33.904; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //static value for test
? interim = mylat + 90.0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 33.904 + 90 = 123.903
? lat1 = (int)(interim / 10.0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 123/10 = 12 Field =M
? interim = interim - (double)mylat ?* 10.0; ? ? ? ? // 123.904 - 12*10 = 3.904
? lat2 = (int)(interim); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 3 Square =3
? interim = interim - (double)lat2; ? ? ? ? ? ? ? ? ? ? ?// 3.123 - 3 =.904
? lat3 = (int)(24.0 * interim); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// .904 * 24 = 21.696 = 21 sub square = v
// Awaiting Dr Jack's book to get learn syntex for arrays and pointers ?
?//Help! I can add 65 to lon1 and lon3 or 48 to lon2 ?and convert to ASCII or add one and point to a letter in the alpha string
// or open to any suggestion. Thanks much, I am tearing what little hair I have left out!
?
}
?
|
¿ªÔÆÌåÓýThanks to you, Arv, and Mark for all the input. I should have your book on Monday. My kingdom for a BASIC CHR$( ) . Now if someone has developed an Arduino robot that smacks you in the head when you forget the semicolon¡ ? v/r Fred W4JLE ? From: [email protected] [mailto:[email protected]] On Behalf Of Jack Purdum via Groups.Io
Sent: Sunday, June 25, 2017 22:22 To: [email protected] Subject: Re: [BITX20] Help! ? Fred: ? Try this: it avoids using the String class, which is a memory hog. ? |
use the sprintf, luke On 26 Jun 2017 9:40 a.m., "Dr Fred Hambrecht" <AAR4MI@...> wrote:
|
Sprintf(%f) and printf(%f) are both non-operative in the Arduino IDE. Using them will not throw an error in compiles...they just don't do anything in run-time.? This seems to be a built-in trap for programmers who are used to having the full conversion capability of C and C++. Other things like decimal, octal, etc. do work as expected.? It is only the conversion of float to text that is broken. _._ On Sun, Jun 25, 2017 at 8:18 PM, Vince Vielhaber <vev@...> wrote: A year or so ago someone on the arduino forum told me that sprintf is there but turned off to conserve memory.? They told me what to do to get it to work but I never could get it to. |
Vince Vielhaber
Actually that's what I was referring to. It's not broken, it's turned
toggle quoted message
Show quoted text
off. Someone trying to use %f will consider it broken. As I stated, there is a way to turn it on but I never got it to work. Vince. Sprintf(%f) and printf(%f) are both non-operative in the Arduino IDE. --
Michigan VHF Corp. |
Jack Purdum
Don't be too hard on yourself. We all make those "flat forehead" mistakes...you know, the kind where you slam the heel of your hand into your forehead while saying "How could I make such a dumb mistake!" ?(All good programmers have flat foreheads.) You can do everything in the String class with about half the memory using the functions found here: Jack, W8TEE From: Dr Fred Hambrecht <AAR4MI@...> To: [email protected] Sent: Monday, June 26, 2017 12:10 AM Subject: Re: [BITX20] Help! Thanks to you, Arv, and Mark for all the input. I should have your book on Monday. My kingdom for a BASIC CHR$( ) . Now if someone has developed an Arduino robot that smacks you in the head when you forget the semicolon¡ ? v/r Fred W4JLE ? From: [email protected] [mailto:[email protected]] On Behalf Of Jack Purdum via Groups.Io Sent: Sunday, June 25, 2017 22:22 To: [email protected] Subject: Re: [BITX20] Help! ? Fred: ? Try this: it avoids using the String class, which is a memory hog. ? |