Show Posts
Pages: 1 2 [3] 4 5 ... 146
31  Using Arduino / Microcontrollers / Re: Security of Software on: August 06, 2014, 01:48:43 am
You can use the fuse settings to activate lock bits.
http://electronics.stackexchange.com/a/53293

The chip datasheet and google will have loads more results.
32  Using Arduino / Programming Questions / Re: USE OF Functions with libraries on: August 05, 2014, 08:43:42 pm
To declare a function as a member of a class... outside of the class, you must use explicit scoping ( just like the constructor ),
also when you define a function as opposed to declaring it, you do not use a semicolon before the curly brackets '{}'

Quote
Motor::Motor(int northpin, int westpin,int eastpin, int southpin){

}

void Motor::initMotor(){

}

As mentioned, you can create the servo variables as part of the class. If you declare them in the .cpp it would also work, however the raw servo objects will not be visible elsewhere ( which may be what you want, but similar to declared private in the class ). However get practice with a class as you're using one already.

Code:
class Motor{
public:
  Motor(int northpin, int westpin,int eastpin, int southpin);
  void initMotor();
  void setNMotorspeed(int spd);
private:
  Servo servos[ 4 ]; //0 north, 1 east, 2 south, 3 west
};

//IN CPP FILE
Motor::Motor(int northpin, int westpin,int eastpin, int southpin){
  servos[ 0 ].attach( northpin );
  //... same for 1,2,3
}

void Motor::setNMotorspeed(int spd){
  servos[ 0 ].writeMicroseconds( spd );
}

If you assign the pins to the servos in the constructor, you may find you do not need the variables "int _northpin, _westpin , _eastpin , _southpin;"
33  Using Arduino / Programming Questions / Re: Permanent Saving to EEPROM on: August 05, 2014, 06:43:04 am
I've written a little script to do a similar function, you may find it useful: http://forum.arduino.cc/index.php?topic=229222.0
34  Using Arduino / Programming Questions / Re: How to copy a buffer into a struct? on: August 05, 2014, 06:07:35 am
You do not want a pointer to the array, but a pointer to the first element:
Code:
radio.read(&myRXbuffer, 32);

//Needs to be
radio.read(myRXbuffer, 32);

But, you'll need the address of the structure.
Code:
memcpy( &myVoltsMessage, myRXbuffer, sizeof( myVoltsMessage ) );

Or you can add  a constructor to the objects which can parse the data themselves.
35  Using Arduino / Programming Questions / Re: Posting errors on: August 04, 2014, 08:39:13 am
Try using an incognito window ( google chrome ), or delete the cache and cookies.
36  Using Arduino / Programming Questions / Re: Store multiple data to EEPROM on: August 04, 2014, 08:32:37 am

eeprom_is_ready() is defined in avr/eeprom.h, you do need to include it.

Instead of using your own loop, the macro eeprom_busy_wait() does this for you.
37  Using Arduino / Programming Questions / Re: Return bidimensional array on: August 04, 2014, 08:05:25 am
Did anyone solve this without using struct?
Like in how can I give a 2D array to a function which then returns a (changed) 2D array as a return value?

I'm sure many people are curious how to do this, so if you are willing to go through some details, I can show you how.

First off, lets cover a few points.
Quote
A function can not return an array. A function can return a single value. That single value need not be a simple type. It can be a pointer, a class instance, or a struct. But, it can not be an array.

This is a way to sum it up, however its not quite right.

A function by definition can return any complete type. An array is a type, so why can't we return one?...
Code:
typedef int arr_t[ 10 ];
arr_t allLoco();

This results in: "error: 'allLoco' declared as function returning an array"

What is happening here is the array type arr_t is being returned "by value", similar to int allLoco();. And the standard forbids array assignments. This is no different to passing arrays to functions, they cannot be passed by value.

So how is it fixed: Use a reference or pointer.

To declare allLoco() as a  "function returning a reference to a two dimensional array of Strings" the syntax becomes a little strange.

However this is how to return a static array from a function, without using a struct.
Code:
String (&allLoco())[ numLoco ][ 3 ]{
  static String localVar[ numLoco ][ 3 ] = { { "StrA", "StrB", "StrC" }, {}, {} };
  return localVar;
}

If you want pass the array to the function then return it, the declaration looks like this:
Code:
String (&allLoco(String (&param)[numLoco][3]))[numLoco][3]{
  return param;
}

You can use the returned reference:
Code:
  allLoco()[ 0 ][ 1 ] = 1;

  //...
  String allMyLoco[numLoco][3];
  allLoco( allMyLoco )[ 0 ][ 1 ] = 2;

You can reference the result somewhere else:
Code:
String (&ref)[numLoco][3] = allLoco();

You can also use the result with sizeof to make more flexible code ( this example uses the reference declared above ):
Code:
#define ARRAY_SIZE( x ) ( sizeof( x ) / sizeof( x[ 0 ] ) )

  for( int inner = 0 ; inner < ARRAY_SIZE( ref ) ; ++inner ){ 
    for( int outer = 0 ; outer < ARRAY_SIZE( ref[ inner ] ) ; ++outer ){
      Serial.println( ref[ inner ][ outer ] );
    }
  }

And as it is a real array, not a decayed pointer, you can use it in C++11 for each statements:
Code:
  for( auto &inner : allLoco() ){
    for( auto &outer : inner ){
      Serial.println( outer );
    }
  }

Hope you learn something. BTW there is more to this solution for the Arduino environment. If someone encounters the error referring to 'atexit' I can show you how to fix it.
38  Using Arduino / Programming Questions / Re: Problem with static const byte FontLookup [][5] and PROGMEM - please help! on: August 04, 2014, 07:11:07 am
Did you change the way you read the data when you added PROGMEM?
The data cannot be read directly and needs  a helper function such as pgm_read_byte, or memcpy_P.

http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html
http://arduino.cc/en/Reference/PROGMEM
http://playground.arduino.cc/Learning/Memory

Search "Arduino PROGMEM" in the forums and google, there are many articles/posts explaining how to do this.

Post your code and we can give a direct example.
39  Using Arduino / Programming Questions / Re: Low memory available on: August 04, 2014, 05:53:20 am
No.
There is no difference in memory usage between using a #define and actually writing the constant in where you need it.

You have no argument from me there, I said: "It'd be the declaration after them that uses the extra memory." Which is referring to the declaration of the variable 'display', not the defines.

40  Using Arduino / Programming Questions / Re: Low memory available on: August 04, 2014, 05:35:27 am
No hash defines do not use any extra memory.

It'd be the declaration after them that uses the extra memory.
41  Using Arduino / Programming Questions / Re: Low memory available on: August 03, 2014, 09:26:39 pm
As you have no code of your own there, you can only modify the libraries in an attempt to reduce the ram usage.

Looks like your collection of libraries are best suited for a Mega2560, with 8k of ram.
42  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 10:26:10 am
Quote
I don't think you *would* have seen the issue, as there was only the one global declaration for the pointer.

Uh huh,

Its not an issue, you created a local variable.
If you use the same name in a nested scope, it replaces the ancestor declaration ( function scope is nested in file scope ). You must explicitly access any version replaced ( :: is your friend ).

Code:
PARAMETERS * ArrayPointer;

void setup(){
  ArrayPointer[ 0 ] = 1; //Global variable.
}

void Foo( void ){

ArrayPointer[ 0 ] = 1; //Global variable.

PARAMETERS * ArrayPointer; //Local variable declared with the same name.

ArrayPointer[ 0 ] = 2; //local variable.

::ArrayPointer[ 0 ] = 3; //Global variable.
}

void Foo2( PARAMETERS * ArrayPointer ){
  ArrayPointer[ 0 ] = 1; //formal parameter has automatic storage ( like local variables ) and replaces global name.
  ::ArrayPointer[ 0 ] = 3; //Global variable.
}

To sum it up, your issue would have been spotted by anybody comfortable with C or C++.
43  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 09:53:13 am
Quote
The fact that it is preceded by "PARAMETERS *"  creates another instance of my pointer.  The functions that refer to the globally-defined pointer are using a pointer that points nowhere...

That's what you get for not posting your code, would have been resolved some time ago.
44  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 07:19:34 am
So, how about referencing the array contents ?
Code:
TparmsA[index].p_type = p_char;
TparmsA[index].parms[0] = p_value;
Is that right ?

Yep.

If your code is expecting the data to be zero/in a default state, add a constructor, or default initialize the POD like the code below.
 
Code:
T *ptr = new T[ N ]();
45  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 06:00:33 am
If T is your type/structure, and N is a constant:

Code:
T *ptr = new T[ N ];
//...
delete [] ptr;

If you use 1.5.7, you can enable C++11 and simply use auto:
Code:
auto ptr = new T[ N ];
//...
delete [] ptr;
Pages: 1 2 [3] 4 5 ... 146