Hot answers tagged

6

I maintain an Arduino plugin that's a port of uClibc++ (just like the defunct StandardCplusplus library). It's available in the Arduino library manager for Arduino versions 1.6.10 and higher. It's called ArduinoSTL.


4

Derived derivedObj; is a local variable. It's deleted when the function exits. You have saved a pointer to it, but with that object now gone your pointer points to nothing of any interest - so doing anything with it is doomed to failure. Instead you need to create a new object on the heap, which will already be a pointer: Derived *derivedObj = new Derived(); ...


4

Even if it would be possible to use dynamic arrays, I would not recommend it, especially not on an Arduino Uno/Mega with only 2 or 8 KB memory. Instead, create a static data element (e.g. array) that has a (reasonable) maximum number of created elements. This also has the benefit that the constructor will not be called after initialization, thus no variance ...


3

Yes, it is possible. See the documentation on memory sections, from the avr-libc manual. For example: // Run this after initializing the stack pointer and zero_reg, but // before initializing the RAM. void __attribute__((naked, used, section(".init3"))) magic(void) { // Whatever... } Note that this runs after both a warm reset and a cold boot. See ...


3

You have to do it inside of function: Sensor *sensors[2]; UltraSonicSensor uSensor; void setup(){ Serial.begin(9600); sensors[0] = &uSensor; } Or directly: UltraSonicSensor uSensor; Sensor *sensors[] = {&uSensor}; Don't forget to make method Sense() virtual in the base class and in the inherited classes. Otherwise you'll have to also find ...


2

In your second constructor you start with the following code: if (this->__data) { // free data ptr if used delete this->__data; } // if This is deadly! You've not initialised __data, so it could hold any value under the Sun. And there is no way that it could possibly be a valid pointer to existing data - it's a brand-new uninitialized object. ...


2

I guess that what you want is a descending sort algorithm, but in the case of Fibonacci numbers you can take advantage that they are already sorted at the beginning. Here are different strategies that you can try : /* SORTING IN DESCENDING ORDER */ const int nbElem = 15; // 24 max int fibonacci[ nbElem ] = { 0, 1 }; void createFibonacciArray( int ...


2

Your code is very complex and running it in an environment where you have limited debug facilities is making life worse for yourself. When an Arduino reboots it is usually because it has thrown a memory error. This happens when you try and access memory that is no longer your program's to access (or never was yours). In this case I originally suspected, ...


1

You can use it using bubble sort algorithm (which is most simple but inefficient) int fibonacci[15]={0, 1}; int a=0; int arraysize=2; // variable to store size or array void setup() { Serial.begin(9600); for (int i=0; i<=14; i++) //loop to create the array { fibonacci[i+2]=(fibonacci[i])+(fibonacci[i+1]); arraysize++; } //Now ...


1

You can check the MCUSR register to see the cause of the reset. In your case you could use: if( bit_is_set(MCUSR, EXTRF) )// reset button was pressed { MCUSR = 0;// clear the reset-flag // "magic" routine here }


1

No, you don't want a vector, for a number of reasons: There is no "vector" unless you add extra (unsupported) code. Vectors use dynamic memory. Arduinos don't work well with dynamic memory. It's pointless anyway. Just allocate an array big enough to contain enough timestamps. In a 10 second period, if you get more than 100 beats, the person you are ...


1

The vector is empty after its default constructor. And nothing was ever added into it in your code. So you can't use anything like idexing by vect[index] or method vect.at(index). And as there are no exceptions available, who knows what it does after the out of range call. You should consider ring buffer instead, using dynamic memory is tricky on such ...


1

You had a lot of problems with your code so it's hard to know where to start. :) I agree with what John Burger said. I took your code onto my PC to save mucking around uploading it each time, and also so I could use valgrind on it. Certainly valgrind reported an error after you printed the vector. The reason for that is simple. You passed a copy of the ...


Only top voted, non community-wiki answers of a minimum length are eligible