Show Posts
Pages: 1 ... 51 52 [53] 54 55 ... 120
781  Using Arduino / Programming Questions / Re: Delays and unsigned long on: March 01, 2013, 08:27:52 am
volume is promoted to int, and the result overflows 16-bits

100 * 9250 = 11100001110101001000

shortened to 16 bits is 0001110101001000
which is 7496

782  Using Arduino / Project Guidance / Re: PROGMEM Efficiency/wastage/overhead on: March 01, 2013, 07:55:46 am
Also if you are storing it in progmem, there is no need for the 12/13 entry number bits,
you can take the size of the entry and use that to traverse a block of data from the start pgm address.

Code:
//Read first byte of entry.
pgm_read_byte( startAddress + ( sizeof( entry ) * index ) );
783  Using Arduino / Programming Questions / Re: how to call a class from my own library, inside another own library. Help on: March 01, 2013, 07:16:10 am
Cpu.cpp does not include Cpu.h, this may cause problems.

Code:
CpuOutput outputObject;

It should be in the cpp or declared extern, if you want it in the header, the compiler may also need CpuOutput.h to be included in the header.

Code:
CpuOutput outputObject();

This is incorrect as it is a function prototype, taking no parameters and returning a CpuOutput object.

784  Using Arduino / Programming Questions / Re: Some *possibly* useful String functions. on: March 01, 2013, 06:38:53 am
Ensure you have the string library fix ( for function free(), search for it in the forum ) as you may have memory leaks after calling those methods multiple times.

You could reduce the number of temporary objects and reserve more memory to speed things along though.

From your example:
Code:
String StringTrimRight(String InputString, int CharCount) {
    String OutputString;
    String StringBuffer = InputString;
    for (int CharIndex = 0; CharIndex < (StringBuffer.length() - CharCount); CharIndex++) {
        OutputString += StringBuffer.charAt(CharIndex);
    }
    return OutputString;
}
The line 'OutputString += StringBuffer.charAt(CharIndex);' will generate many temporary variables, each one with an allocation.


It can be slightly tweaked:

Code:
String StringTrimRight(String InputString, int CharCount) {
  const int Steps = InputString.length() - CharCount;
  if( Steps < 1 ) return( "" ); //Check for invalid or complete trim
  InputString[ Steps - 1 ] = '\0';
  return( &InputString[ 0 ] );
}

This leaves the String constructor on exit to do all the looping. And only 1 temporary String object.
And as you do not pass InputString by reference, you can modify its contents without harm.

Passing String objects by reference will boost performance, however you have to copy the data to a buffer if the reference cannot change.

This construct below can help you design code that can use variable sized temporary arrays. So no large global working buffer, or oversized local buffer.
Code:
void foo( const int len ){
  char temp[ len ];
}
785  Using Arduino / Displays / Re: how to use bitmap? on: March 01, 2013, 03:05:23 am
Yes, it is the far easier option, to use the SD card would be far more complex. What ever library you are using most probably has a tutorial/example of bit map use.
786  Using Arduino / Programming Questions / Re: PROGMEM question on: March 01, 2013, 02:21:30 am
It all depends on the scenario its used in. For your example:

The compiler for arduino is set to optimisations favouring small code, so in this case it might not be compiled away due to it having to unroll the loop. If the loop was unrolled, then yes it could be a compile time constant, depending on its use elsewhere.

All of this depends on weather the variable is marked external, used by non-constant reference to any of the data, or something looks at the address of the array, and sometimes the compiler cannot guarantee a variable is a 'compile time constant', even when you clearly program it as one.

If you are going to expect/want the compiler to unroll the loop, then just do it in code yourself. Templates will help you achieve unrolling of complex loops ( not for the loop above ). And if you do unroll, you can then use an enum to organise the data, instead of an array, then you can guarantee the values are compile time constants.

Then you will also have to weigh the result of having unrolled code ( can get large ), vs an array in progmem used in a loop.
787  Community / Exhibition / Gallery / Re: Twitter Feed Machine on: February 28, 2013, 09:40:20 am
Nice one, maybe you could use a secret twitter account to add/remove new feeds by having it read instructions in the tweet.
788  Using Arduino / Displays / Re: how to use bitmap? on: February 26, 2013, 10:41:45 am
If you wish to use a .c file it will have to be compiled into the sketch.
You could store a binary file of the data, then using the SD library, read it into your sketch first then pass it to the drawBitmap function.

For now, include the .c file in your sketch and try that way.
789  Using Arduino / Programming Questions / Re: ERROR on: February 25, 2013, 07:13:29 pm
String.toCharArray(num, 11);

That is wrong, stop using it, delete it.

'd' has only read one character, which 'following number' do you expect to store in num.

You are looping as long as there is data:

Code:
Serial1.available() > 0

But you do not do anything with it unless it is 'a'

and there is no end '}' on your loop.
790  Using Arduino / Programming Questions / Re: ERROR on: February 25, 2013, 06:42:14 pm
Did you see the posts in your last thread: http://arduino.cc/forum/index.php/topic,150807.0.html

Quote
if the start of the data is d

there is only 1 character, and yes it is d but you check weather it is 'a'

String.toCharArray(num, 11);

String is not a variable, this line is not valid.
791  Using Arduino / Programming Questions / Re: moving function map into library on: February 25, 2013, 05:56:02 pm
If you want the idea encapsulated in a class, and only one serial device is communicating at once, then things are easy using classes.

Imagine your base class is something like this.
Code:
struct BaseType{
  /*
    Pure virtual as you will not be creating instances of this class, only instances of
    classes that derive this base class.
  */
  virtual void RunCommand( const char *c_Data ) = 0;
};

Code:
struct DerivedA : BaseType{

  void RunCommand( const char *c_Data ){

    if( strcmp( "status", c_Data ) ){

    }else if( strcmp( "update", c_Data ) ){

    }
  }
};

struct DerivedB : BaseType{

  void RunCommand( const char *c_Data ){

    if( strcmp( "config", c_Data ) ){

    }else if( strcmp( "reset", c_Data ) ){

    }    
  }
};


Code:
DerivedA objA;
DerivedB objB;

BaseType *currentObj = &objA;

void loop(){

char c_Buffer[ x ];
//...  fill c_buffer with command.

//Run command based on current object.
currentObj->RunCommand( c_Buffer );
}
792  Community / Exhibition / Gallery / Re: Imperial March on a fast switching valve on: February 25, 2013, 09:22:06 am
Nice job, just went and had a read on those valves. I liked the ones marked as useful for 'fast sorting'.
793  Using Arduino / Programming Questions / Re: moving function map into library on: February 25, 2013, 08:04:50 am
Quote
Is it possible for the processStatusLine, function_map_struct, and call_function to all be within the scope of a HwClassSerial class instance instead of in the global scope?

Yes, using the correct working code I provided, you should be able to put them in. However not for the way you want.
To keep with ease of use, processStatusLine and other functions to be called via a pointer need to be non-member functions( static or friends ).

But really, you are mixing two different systems when one will do. call_function is a lookup method, whereas using an inherited/base type should imply that the derived type provides the missing information, which completes the base type.
794  Using Arduino / Displays / Re: how to use bitmap? on: February 25, 2013, 07:45:06 am
The type is an 'unsigned int*' so it would be an array of 16 bit data.
You want to put the name of the array.
795  Using Arduino / Programming Questions / Re: Help with multiple millis() on: February 25, 2013, 06:58:20 am
Quote
Initial timing meaning, more than 1000UL, or how should i do that, i will try with if else statement in the meanwhile.
Thanks . .

If you used different intervals for each of the three variables:
Code:
const unsigned long timerRateX = 1000UL;
const unsigned long timerRateY = 1500UL;
const unsigned long timerRateZ = 2000UL;

This will cause each section to expire 500ms apart.

A better approach would be to use steps and a single timeout.

Here is a pseudo layout:

Code:
const unsigned long timerRate = 1000;
unsigned long lastCount = 0;
char itemIdx = 0;

void loop(){

  unsigned long thisCount = millis();

  if( thisCount >= ( lastCount + timerRate ) ){

    lastCount = thisCount;

    switch( ++itemIdx ){
      case 3:
        itemIdx = 0;

      //First action
      case 0:
     
        digitalWrite(voltageFlipPin01, HIGH);
        digitalWrite(voltageFlipPin02, LOW);
        break;

      //Second action
      case1:
     
        digitalWrite(voltageFlipPin01, LOW);
        digitalWrite(voltageFlipPin02, HIGH);
        break;

      //Third action
      case 2:
     
        digitalWrite(voltageFlipPin01, LOW);
        digitalWrite(voltageFlipPin02, LOW);
        break;
    }
  }
 

}
Pages: 1 ... 51 52 [53] 54 55 ... 120