Show Posts
Pages: 1 ... 50 51 [52] 53 54 ... 121
766  Using Arduino / Programming Questions / Re: Not sure how to handle this incoming string on: March 13, 2013, 08:18:13 am
This seemed to work. With a little tidy up, it should do.

Code:
int InStr( char c, char *str ){

  char *cursor = str;
  while( *cursor ){
    if( *cursor == c ) return cursor - str;
    ++cursor;
  }
return -1;
}

void setup()
  {
    Serial.begin( 9600 );

    char *str = "X:0.00Y:0.00Z:450.20E:0.00";
    float f_X = atof( str + InStr( 'X', str ) + 2 );
    float f_Y = atof( str + InStr( 'Y', str ) + 2 );
    float f_Z = atof( str + InStr( 'Z', str ) + 2 );
    float f_E = atof( str + InStr( 'E', str ) + 2 );
    
    Serial.print( "X: " );
    Serial.println( f_X );
    Serial.print( "Y: " );
    Serial.println( f_Y );
    Serial.print( "Z: " );
    Serial.println( f_Z );
    Serial.print( "E: " );
    Serial.println( f_E );
}

Output:
Quote
X: 0.00
Y: 0.00
Z: 450.20
E: 0.00
767  Using Arduino / Programming Questions / Re: Not sure how to handle this incoming string on: March 13, 2013, 07:58:36 am
You could find the start index of each letter ( X,Y,Z,E ) and then use atof at the ( start + 1 ) locations to get each float value.

atof: The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see here), and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.

768  Using Arduino / Installation & Troubleshooting / Re: code related uploading problem.. on: March 13, 2013, 01:25:36 am
Not that I know of, search the forums though. This particular issue has arisen many times, the answer is probably in one of the threads.

Or you could have a scan through the bootloader code.
769  Using Arduino / Installation & Troubleshooting / Re: Uploading problem on: March 13, 2013, 01:12:44 am
Don't use !!! it is a code captured by the boot loader or some other intermediate process.

Try printing !! then a separate ! to avoid the problem.
770  Using Arduino / Programming Questions / Re: A function to get the number of variables in an array? on: March 12, 2013, 11:16:24 pm
Quote
And tell me, why does the struct have a size of 1?

Because you took the sizeof the ArraySize struct. Not something passed to it.

The standard does not allow objects (and classes thereof) of size 0, since that would make it possible for two distinct objects to have the same memory address. That's why even empty classes must have a size of (at least) 1.

Hence:
Code:
struct Empty{};
struct Full{char a;}

Will be the same if the sizeof is taken.

Seeing as you do not create an instance of the object, you do not pay the price, it is a global interface.
771  Using Arduino / Programming Questions / Re: A function to get the number of variables in an array? on: March 12, 2013, 10:40:33 pm
Creating a static array, while requiring a static for initialisation is just plain ridiculous, how about just typing the number Nick?. The template is not for nonsensical reasons obviously.

I don't think it's ridiculous. Suppose I wanted to define an array of pin numbers, and an array of state data associated with the pins - I'd want the second array to be the same size as the first one without having to hard-code the size of either array.

This is a mildly flawed approach in C++ ( not so much C ), considering structs will remove the data separation and provide encapsulation, and a hard coded value provides safety against silly mistakes. The defines are not specifically for an array, so it will happily compile broken code.

Quote
while I stare at those templates and think WTF is going on here?

Something new to learn maybe?? 
C++ provides these type-safe mechanisms because of the short falling's encountered in C. Using unsafe practices because they are 'easier', in my opinion is not the best way to develop software.


772  Using Arduino / Programming Questions / Re: A function to get the number of variables in an array? on: March 12, 2013, 08:54:58 pm
And just in case there is a strong aversion to remembering the number, here is a compile time version, which is still safer than the macros provided.

Code:
template< typename T > struct ArraySize;
template< typename T, size_t N > struct ArraySize< T[N] >{ enum{ Size = N }; };

Serial.println( ArraySize< typeof( ledPins ) >::Size );
773  Using Arduino / Programming Questions / Re: A function to get the number of variables in an array? on: March 12, 2013, 08:10:11 pm
Here is a method that causes the compiler to provide the answer, rather than computing the value.

EDIT: The added bonus is it has protection against non array pointers, unlike the defines above.

Code:
template< typename T, size_t N > size_t ArraySize( T (&)[N] ){ return N; }

Yes, very cunning. Except it doesn't return a constant so it can't be used here:

Code:
template< typename T, size_t N > size_t ArraySize(T (&)[N] ){ return N; }
int foo [] = { 5, 6, 7, 8, 9 };
int bar [ ArraySize (foo) ];   // make another array the same size

Error:

Code:
sketch_mar12c:3: error: array bound is not an integer constant

The NUMITEMS method works there.


Creating a static array, while requiring a static for initialisation is just plain ridiculous, how about just typing the number Nick?. The template is not for nonsensical reasons obviously.
774  Using Arduino / Programming Questions / Re: A function to get the number of variables in an array? on: March 12, 2013, 09:29:29 am
Unless you use 'malloc' or 'new' to create dynamic memory, this information is readily available by the compiler state. It knows the size of arrays even when there is no clear cut way to grab the info.

Here is a method that causes the compiler to provide the answer, rather than computing the value.

EDIT: The added bonus is it has protection against non array pointers, unlike the defines above.

Code:
template< typename T, size_t N > size_t ArraySize( T (&)[N] ){ return N; }

int ledPins[] = {
  2, 7, 4, 6, 5, 3 };


void setup()
  {
    Serial.begin( 9600 );
    Serial.print( "Array Size: " );
    Serial.println( ArraySize( ledPins ) );
  }
775  Using Arduino / Programming Questions / Re: Why does the compiler spit out things like this? on: March 08, 2013, 10:30:32 pm
It seems like a common thing. I have had lists of errors pop up occasionally from version 0022 to 1.0.2, especially when cutting or pasting text. Haven't had random scrolling though.

Might be something to do with the IDE virtualizing the files to be compiled.

This is on windows XP / 7.
776  Using Arduino / Programming Questions / Re: String... need your help.. on: March 07, 2013, 11:56:07 pm
Maybe you are running out of memory or something else is causing a problem.
If I add 'Serial.println( stringTwo );' to the end of the code in reply #5, it prints out:
Quote
byte: 10
92845401834
777  Using Arduino / Programming Questions / Re: String... need your help.. on: March 07, 2013, 11:39:02 pm
I'm not sure what you are expecting.
The long number is too big for a char, and too big for atoi to turn to an int.

if you want a pointer to the char array, you can use:
Code:
char *c_array = &stringTwo[ 0 ];
778  Using Arduino / Programming Questions / Re: String... need your help.. on: March 07, 2013, 11:12:12 pm
atoi will do what you want for the byte:

Code:
    Serial.begin( 9600 );
   
    String stringOne = "10 92845401834";
   
    stringOne[2] = '\0';
   
    String stringTobyte = &stringOne[ 0 ];
    String stringTwo = &stringOne[ 3 ];
   
    //Fix up stringOne
    stringOne[2] = ' ';
   
    byte b_Data = atoi( &stringTobyte[ 0 ] );
    Serial.print( "byte: " );
    Serial.println( b_Data );
779  Using Arduino / Programming Questions / Re: String... need your help.. on: March 07, 2013, 10:52:27 pm
byte = unsigned char, maybe you want uint64_t ( unsigned long long ) as 92845401834 requires 37 bits of storage.

you need to look for the function atoi, fine for the '10' but it wont support 64 bit values ( also, atol is no good ), here is the first topic I found on google:
http://stackoverflow.com/questions/7502926/what-is-atoi-equivalent-for-64bit-integeruint64-t-in-c-that-works-on-both-unix
780  Using Arduino / Programming Questions / Re: String... need your help.. on: March 07, 2013, 10:26:22 pm
A cheap pointer abuse will work well. Avoid the String library once you are comfortable with arrays and pointers.

Code:
String stringOne = "10 92845401834";

stringOne[2] = '\0';

String stringTobyte = &stringOne[ 0 ];
String stringTwo = &stringOne[ 3 ];

//Fix up stringOne
stringOne[2] = ' ';
Pages: 1 ... 50 51 [52] 53 54 ... 121