Show Posts
Pages: 1 ... 48 49 [50] 51 52 ... 122
736  Using Arduino / Programming Questions / Re: length of multidimensional array? on: April 17, 2013, 12:19:20 am
Never needed to know that value, but here is how I would do it in C++:
Code:
template< typename T, size_t A, size_t N >
  inline const size_t sizeofMArray( T (&)[A][N], const char item ) { return item ? A : N; }

Tested with this code:
Code:
    Serial.begin( 9600 );
    Serial.println( sizeofMArray( zone1, 0 ) );
    Serial.println( sizeofMArray( zone1, 1 ) );
    Serial.println( sizeofMArray( zone2, 0 ) );
    Serial.println( sizeofMArray( zone2, 1 ) );
    Serial.println( sizeofMArray( zone3, 0 ) );
    Serial.println( sizeofMArray( zone3, 1 ) );

Gives this output.
Quote
3
6
3
7
3
4

Do what WizenedEE said and use the global variable, then this code in your function.
737  Using Arduino / Programming Questions / Re: can you see what I am doing wrong? on: April 16, 2013, 07:40:31 pm
What pin are you setting high, http://arduino.cc/forum/index.php/topic,160932.msg1204291.html#msg1204291

There is a difference between pin numbers and pin values You are comparing a pin number with HIGH, completely illogical,
just remove the 'if' dang nabbit, the pin is high cos you set it high, there is no need to assume it has changed unless you typed the code below inside an interrupt.

Code:
digitalWrite(b1yel1, LOW);

738  Using Arduino / Programming Questions / Re: can you see what I am doing wrong? on: April 16, 2013, 07:04:25 pm
It appears b1yel1 is a pin number not a LOW/HIGH value, so the if statement will fail on any pin number that isn't 1 and skip line 7

Code:
5      digitalWrite(b1yel1, HIGH);   //  I have put this line in to prove b1yel1 is high
6      if (b1yel1 == HIGH)  
7        digitalWrite(b1yel2, HIGH);

You could check the pin is high, but you just set it high, so only an interrupt could change it, which you probably aren't using.
739  Using Arduino / Programming Questions / Re: Undefined reference compilation error on: April 16, 2013, 04:14:08 am
The sketch is compiled as C++, your other.c is C.
rename the file to .cpp or look at this: http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c
740  Using Arduino / Programming Questions / Re: Issue with variable declaration on: April 16, 2013, 01:06:31 am
Ah, I see whats happening, not used scanf at all really.
I just had a look at the documentation for scanf:

Quote
format specifier for scanf follows this prototype:

%(*)[width][length]specifier

Seeing as you use 'u', which is integer ( 2 bytes ), do you need to specify a width/length of 1 for one byte. like '%1u' instead of '%u'
741  Using Arduino / Programming Questions / Re: Issue with variable declaration on: April 15, 2013, 08:42:44 pm
Drop the '&' character.

If 'mydns' is an array:
Code:
byte mydns[4] = {1,1,1,1};
//or any other type
int mydns[4] = {1,1,1,1};

You access its elements like:
Code:
mydns[ index ]

Not:

Code:
&mydns[ index ]

But IPAddress supports using '[]' operators so this code below is fine:

Code:
IPAddress mydns;

//No '&' infront of element.
mydns[0] = 192;
mydns[1] = 168;
mydns[2] = 0;
mydns[3] = 1;
742  Using Arduino / Programming Questions / Re: EEPROM read and write on: April 15, 2013, 10:48:11 am
That library is fairly easy to use.

Code:
unsigned long int hiscore[8];

//Write
for( int index = 0 ; index < 8 ; ++index ){
  EEPROM_writeAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
}

//Read
for( int index = 0 ; index < 8 ; ++index ){
  EEPROM_readAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
}

Edit: you could extend the life of your eeprom when writing by using the method below:
Code:
for( int index = 0 ; index < 8 ; ++index ){

  unsigned long int temp;
  EEPROM_readAnything( index * sizeof( unsigned long int ), temp );

  if( temp != hiscore[ index ] ){

    EEPROM_writeAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
  }
}
743  Using Arduino / Programming Questions / Re: Instruction set reference for Leonardo's ATmega32U4? on: April 15, 2013, 08:06:34 am
It's a standard assembly language instruction. You do have an assembly language programming manual, right?

I've not used assembly too much, but from what I gather an 'assembly language' is architecture defined. As in: there are no standard assembly instructions, they are specific to a CPU's machine codes.
744  Using Arduino / Programming Questions / Re: Communication "Packet" design by using "union" (Suggestion required) on: April 15, 2013, 07:42:43 am
Quote
In short, I just wanted to make sure that use of union is not harmful for embedded systems in this case.
It depends in what way you intend to use the members.
What you are really doing by defining data as one type and aliasing it as another is type-punning.

As GCC supports its use ( mostly ) you can use it here in Arduino land, however:
* The C99 and C11 standards allow it.
* The C++99  and C++11 standards do not.

As you are using a char type as the second member things are different. C++ explicitly allows type-punning to a char type, whereas the other way around is undefined behaviour.

So if you write to the struct member, then read/write the array member its fine. But writing the array, then reading the struct can break strict-aliasing rules.

This makes your union usage more or less implementation defined, and therefore cannot really be considered portable: Even through future revisions. GCC currently says this:

Quote
*A member of a union object is accessed using a member of a different type.
The relevant bytes of the representation of the object are treated as an object of the type used for the access. See Type-punning. This may be a trap representation.

What this means is you can use your union fine as you have it laid out there, but consider this example:

Code:
union a{
  uint16_t b;
  uint32_t c;
  char d[ 4  ];
};

a A = { 0xFFFF };

Initialising the smaller type of a union with unequal sized members means you can only safely access d upto d[sizeof(b)-1], and c cannot be touched at all.
Whereas if c or d is the active member, reading the others is fine.

The active member is the last written to member:
Quote from: C++99: 9.5 class.union
In a union, at most one of the non-static data members can be active at any time, that is, the value of at
most one of the non-static data members can be stored in a union at any time.

745  Using Arduino / Programming Questions / Re: "objdump --source" doesn't intermix source code on: April 15, 2013, 06:33:23 am
Hmm, I just tested my v1.5.1r2 and it does add the source, but my 1.0.4 does not. Very strange!
Maybe one of the new compiler/linker options prematurely removes symbols necessary to produce the output.
746  Using Arduino / Programming Questions / Re: which C++, and is there a specification of just how the IDE tries to "help" one? on: April 13, 2013, 09:45:28 pm
The arduino IDE uses old software, and C++11 is not really at all supported, just a few experimental tidbits: http://gcc.gnu.org/gcc-4.3/cxx0x_status.html

You can write your own main, then you don't need setup() and loop(). Just remember to call init() before you use certain Arduino specific features like delay() or millis().

The IDE adding stuff is the price we pay for ease of use. Atmel studio 6 supports gcc 4.6.x, might even be 4.7.x now; and has c++11 support.

If multiple declarations are causing errors ( declared in header, defined in sketch ), inside the sketch just wrap you function declarations in #if blocks that never get entered, then the IDE will see them, but the compiler won't.
747  Using Arduino / Programming Questions / Re: #define vs. static const on: April 13, 2013, 02:17:13 am
Remember, const isn't always a compile time constant.

If you really have constant values, and and want to guarantee they are compile time constants: simply use an enum.
Then the values aren't cast if the datatypes don't match, they will be natural type of the L-Value.

Code:
enum{
  EPROM = 0xB600,  //EEPROM Location
  EXTRAM2 = 0xB800,  //EXTRAM Location
  LCD_CTL = 0x3000,  //Control register of LCD
  LCD_DATA = 0x3001, //Data register of LCD
  HIRAM = 0xBFFF ,   //Highest address for 32k * 8 RAM
};
748  Using Arduino / Programming Questions / Re: TASKS on: April 12, 2013, 09:15:37 am
You keep asking the same questions, is there some more info you want to provide so more concise answers can be given?

TASKS
PLC C programming
Real Time programming
THE LINUX VERSION OF ARDUINO
...
749  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)
750  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];
Pages: 1 ... 48 49 [50] 51 52 ... 122