Show Posts
Pages: 1 ... 69 70 [71] 72 73 ... 142
1051  Using Arduino / Programming Questions / Re: Update LCD display when potentiometer changes on: April 11, 2013, 07:35:52 pm
A trick a lot of people use with various displays is,
You need to display a max of 3 chars ( 100? ) so if the number is less than 100 add a space at the end, if less than 10 add two spaces. Might save tracking numbers.

This is easy to do if you concatenate your string before printing. (itoa)
1052  Using Arduino / Programming Questions / Re: Set Contents of Struct After Initialization on: April 10, 2013, 09:22:31 pm
It is fine how you have it there. Seems there is some conflicting code you haven't posted.

Do you have this line below anywhere?
Code:
String options[50];
1053  Using Arduino / Programming Questions / Re: Compile err: not declared in this scope on: April 10, 2013, 08:43:42 pm
An arduino IDE quirk requires the sketch to include all additional libraries that any library may include.

Just add #include <I2C.h> to the sketch also.
1054  Using Arduino / Programming Questions / Re: Function fails with passed in variable, but works if set manually. on: April 10, 2013, 08:34:52 pm
I really believe it's a compiler issue.
Nope,

Because your flasharray is an array of progmem pointers, not data. You must use pgm_read_word to retrieve the pointer.

Code:
Serial.println((__FlashStringHelper*)flashArray[i]);

This is reading the i'th value in sram, you placed the data in PROGMEM. I can see that it works on occasion for you ( and my test ), but it is still wrong.

Use this:
Code:
Serial.println((__FlashStringHelper*) pgm_read_word( &flashArray[i] ) );

Or change your flasharray to this ( no PROGMEM ):

Code:
const char * flashArray[] = { string1, string2, string3 };
1055  Using Arduino / Programming Questions / Re: Function fails with passed in variable, but works if set manually. on: April 10, 2013, 07:26:10 pm
Code:
Serial.println((__FlashStringHelper*)flashArray[i]);

needs to be: ( your pointers to the strings are also in flash, get them first ):

Code:
Serial.println((__FlashStringHelper*) pgm_read_word( &flashArray[i] ) );

Also keep your vars const for fun ( pointer and var ):

Code:
const char * const flashArray[] PROGMEM = { string1, string2, string3 };

Finally if all else fails, replace PROGMEM to '__attribute__((section(".progmem.data.mysection")))'
volatile data can mess the sections up if mixed in the same file.
1056  Using Arduino / Programming Questions / Re: Passing and returning Arrays into functions on: April 09, 2013, 10:22:27 am
You can't return an actual array, however you can return a pointer to an array defined in appropriate memory ( returning local array pointer = bad ). The pointers don't carry size info either. char, char*, char[5], char[10] are all different types, the compiler uses its smarts to deduce which types are compatible at compile time ( it knows what size you declared your arrays to ).

You could pass in a pointer and length to the function.
void func( type *var, int len );

Or return a temporary object containing an array ( which gets copied ).
Code:
struct mytype{
  char arr[64];
};

mytype func( void ){
  mytype _return;
   //populate array.
  _return.arr[ 0 ] = 'a';
  return _return; //Client can inspect or copy object.
}

The copy can be avoided, but that's a whole 'nudder' story.



1057  Using Arduino / Programming Questions / Re: Bitwise operations, again... on: April 08, 2013, 05:26:34 pm
Quote
Code:
const uint8_t   pins[]  = { AD, AC, AB, AA };
uint8_t         digit   = /* 0 - 255 */;

for ( int mask = 0b00001000, i = 0; mask; mask >>= 1, i++ )
{
    digitalWrite(pins[--i], digit & mask ? HIGH : LOW);
}

Is 'i' not set right? it starts at zero, is decremented to -1 before use, then reset to zero, and repeated.
1058  Using Arduino / Programming Questions / Re: count character on: April 07, 2013, 11:04:34 pm
A homework question?

Code:
byte CountString( const char *c_Str )
  {
    byte b_Return = 0;
    while( c_Str[ b_Return ] != '\0' ) ++b_Return;
    return b_Return;
  }

char char1[20] = "love";
byte countchar = CountString( char1 ) + 1;
1059  Using Arduino / Programming Questions / Re: PROGMEM usage on: April 07, 2013, 12:38:31 am
Yeah, moving the pointer could be bad, however using the pointer value is fine:

Code:
strcpy_P( buff, inProgmem + 5 );
 Serial.print( "Read inProgmem[ 5 ] from flash: " );
 Serial.println( buff );

it uses the same principle involved in using struct members to offset a pointer:

Code:
struct RGB{
  int red;
  int green;
  int blue;
};

RGB Colours[] PROGMEM = {
     { 0, 0, 0 },
     { 255, 0, 0 },
     { 2, 255, 0 },
     { 0, 0, 255 },
     { 255, 0, 255 },
     { 255, 255, 255 },     
  };

void GetColour( RGB &r_Out, int i_Index )
  {
    const RGB &r_Colour = *(Colours + i_Index);
   
    r_Out.red = pgm_read_word( &r_Colour.red );
    r_Out.green = pgm_read_word( &r_Colour.green );
    r_Out.blue = pgm_read_word( &r_Colour.blue );
    return;
  }
1060  Using Arduino / Programming Questions / Re: PROGMEM usage on: April 06, 2013, 11:10:31 pm
Just try printing the array as normal, it should do some undefined bahaviour/nothing/everything.

Code:
char inRam[] = "String in SRAM";

char inProgmem[] PROGMEM = "String in FLASH";

void setup() {

 char buff[ 20 ];
 
 Serial.begin(9600);
 
 Serial.print( "Read inRam from ram: " );
 Serial.println( inRam );
 
 strcpy_P( buff, inProgmem );
 
 Serial.print( "Read inProgmem from flash: " );
 Serial.println( buff );
 
 Serial.print( "Read inProgmem from ram: " );
 Serial.println( inProgmem );
}

void loop() {}
1061  Using Arduino / Programming Questions / Re: PROGMEM usage on: April 06, 2013, 10:46:57 pm
strcpy_P is the PROGMEM version of strcpy.

If you pass it cmd_hello, it does the  'pgm_read_XX' handling for you.

cmd_hello is a pointer stored in ram pointing to its data stored in flash, you only need special functions to access the data, not the pointer.

1062  Using Arduino / Programming Questions / Re: variable declaration on: April 06, 2013, 07:23:16 pm
I've always wished for -

Code:
typedef const uint8_t   pin_t;


What's that ?

typedef creates an alias to a decorated type.
This one says, alias pin_t with a type 'constant uint8_t'

So later on you can use the type in your code:

Code:
pin_t pin13 = 13; //This is a const type, it cannot be changed.

Similar to:

Code:
const unsigned char pin13 = 13;
//or
const byte pin13 = 13;
//or
const uint8_t pin13 = 13;

Actually, uint8_t is a typedef provided by the standard:
Code:
typedef unsigned char uint8_t;

You can also use a type which is guaranteed to be constant:

Code:
enum{ pin13 = 13 };
//...
digitalWrite( pin13, HIGH );

1063  Using Arduino / Programming Questions / Re: How to generate a piano sound using SPI on: April 04, 2013, 11:00:19 pm
I would suggest finding the model of your DAC, then locate the data sheet and search for examples on the forums and google, there is a good chance someone has done a write up on it.
Once you have a working example you can then focus on producing specific sounds.
1064  Using Arduino / Programming Questions / Re: PROGMEM and F() macro as parameters for function on: April 04, 2013, 09:35:05 pm
There are a couple of things you can do.

the macro F is defined as
Code:
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

So it already uses PSTR(), and was designed to prevent the ambiguities. The macro casts the progmem pointer to a type __FlashStringHelper* which allows functions to overload based on a type that is incompatible with normal char*.

So instead of PSTR(), just use F().

To solve the problem for global data, i.e. prog_uchar test2[] PROGMEM = { "Test 2" } ; you could use a macro as well:
Code:
#define PGMT( pgm_ptr ) ( reinterpret_cast< const __FlashStringHelper * >( pgm_ptr ) )

Here is a test case:
Code:
char test2[] PROGMEM = { "Test 2" } ;

#define PGMT( pgm_ptr ) ( reinterpret_cast< const __FlashStringHelper * >( pgm_ptr ) )

void setup()
{
  Serial.begin( 9600);
  Serial.println( "Start");
 
  Serial.print( "1: ");
  WriteSlave( "Test 1");
  Serial.print( "2: ");
  WriteSlave( PGMT( test2 ), 6);
  Serial.print( "3: ");
  WriteSlave( F("Test 3"), 6);
  Serial.println("");
  delay(5000); 
}

void loop(){}

void WriteSlave( const char *pData )
{
  Serial.write( pData );
  Serial.println("");
}

void WriteSlave( const __FlashStringHelper* pData, const int size)
{
  char buffer[ size + 1 ]; //Size array as needed.
  Serial.print( "-> __FlashStringHelper -> ");
  memcpy_P( buffer, pData, size);
  buffer[ size ] = '\0';  //protect from odd sized strings ( a short string will reveal old chars )
  WriteSlave( buffer, size);
}

As you can see, even more ambiguities are removed by having different counts of parameters ( "Test 1" is a null-terminated literal, so serial.print doesn't need a count.

But we can go one step further and remove the count from the progmem side of things:

Code:
char test2[] PROGMEM = { "Test 2" } ;

#define PGMT( pgm_ptr ) ( reinterpret_cast< const __FlashStringHelper * >( pgm_ptr ) )

void setup()
{
  Serial.begin( 9600);
  Serial.println( "Start");
 
  Serial.print( "1: ");
  WriteSlave( "Test 1");
  Serial.print( "2: ");
  WriteSlave( PGMT( test2 ));
  Serial.print( "3: ");
  WriteSlave( F("Test 3"));
  Serial.println("");
  delay(5000); 
}

void loop(){}

void WriteSlave( const char *pData )
{
  Serial.write( pData );
  Serial.println("");
}

void WriteSlave( const __FlashStringHelper* pData )
{
  char buffer[ 20 ]; //Size array as needed.
  int cursor = 0;
  prog_char *ptr = ( prog_char * ) pData;

  Serial.print( "-> __FlashStringHelper -> ");
  while( ( buffer[ cursor ] = pgm_read_byte_near( ptr + cursor ) ) != '\0' ) ++cursor;
  WriteSlave( buffer );
}
1065  Using Arduino / Microcontrollers / Re: AVR vs ARM on: April 04, 2013, 09:07:37 am
The Uno and Mega are minimal different because the internal counters are not equal clocked. (capacitor inaccuracy).

Maybe so, but the Mega pays a price for having more code space, some of the branching instructions take 1 additional cycle over smaller AVR's. You make use of these instructions too ( CALL, RET, RETI, ... ) so a difference in execution time is expected.

Pages: 1 ... 69 70 [71] 72 73 ... 142