using servo in library - servo stops working after 5 times

Hi there,

I’m trying to make a library which controls 2 servo’s.

When I use my library it works fine exactly 5 times, then the servo stops working until a reset.

I’m wondering if i’m declaring/using variables incorrectly in my library, or not cleaning up.

I’ve attached the cpp and h files for my library. To use it, I #include <RemoteLib.h> at the top, then run:

RemoteLib remote(9,11);
remote.PressButton(6, false);
remote.TurnServoOff();

… in a function in my program.

Is it possible that the object is not being cleaned up or something, even through it’s declared within the scope of a function? Or, would the Servo objects in the library not be cleaned up??

Any ideas guys?

Thanks,
Mick

RemoteLib.cpp (2.29 KB)

RemoteLib.h (913 Bytes)

There is not much point posting the code for a library without also posting the code that has the problem when it uses the library.

...R

Robin2:
There is not much point posting the code for a library without also posting the code that has the problem when it uses the library.

…R

… good point. Here is a bare-bones example - it stops after 6 iterations of the loop:

#include <Servo.h>
#include <RemoteLib.h>

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

while (true) {
DoSomething();
delay(2000);
}
}

void DoSomething() {
RemoteLib remote(9,11);
remote.PressButton(1, true);
remote.TurnServoOff();
}

… but, if i dump the DoSomething() function and just have one RemoteLib object (not re-create it in a function) it runs forever like expected …

void loop() {
// put your main code here, to run repeatedly:

RemoteLib remote(9,11);

while (true) {
remote.TurnServoOn();
remote.PressButton(1, true);
remote.TurnServoOff();
delay(2000);
}
}

… so, there must be a memory leak or something in my library i’m guessing

I've mucked around in my code.

Basically, there's a problem with creating an object of the library (RemoteLib) multiple times in a function that's called from loop.

To get around this, I just create the object once only at the start of loop, then put everything in a while(true) so I just user the same object.

My theory is that as the object is created/destroyed it creates fragmentation in memory

Slow down a bit. Do your thinking in your own time and post your comments when you have the thinking complete.

Your code suggests a very poor knowledge of programming before I make any attempt to look at your library.

Why would you bother to have a while(true) inside loop() when that is what loop() is there for.
And the Arduino can do nothing while a delay() is in progress
And why would you even think of creating instances of your library anywhere except in setup() - where they are just created once.

And please use the code button </> so your code looks like this

…R

in the loop you declare the function and the pins, that should be in setup

Robin2:
Slow down a bit. Do your thinking in your own time and post your comments when you have the thinking complete.

Your code suggests a very poor knowledge of programming before I make any attempt to look at your library.

Why would you bother to have a while(true) inside loop() when that is what loop() is there for.
And the Arduino can do nothing while a delay() is in progress
And why would you even think of creating instances of your library anywhere except in setup() - where they are just created once.

And please use the code button </>

so your code looks like this

…R

Wow. Please don’t bother looking at my code - your “help” is just criticism and cynicism which you clearly get some kind of satisfaction from - i.e. you’re a troll.

P.S. That code I posted was just a bare bones basic example that I put together to demonstrate when the issue occurs and when it doesn’t. The while(true) is there so the RemoteLib object is instantiated just once (to demonstrate that it works fine in that situation)

P.P.S. “Do your thinking in your own time …” wtf? I didn’t know I was your employee lol

mikgol: Wow. Please don't bother looking at my code - your "help" is just criticism and cynicism which you clearly get some kind of satisfaction from - i.e. you're a troll.

P.S. That code I posted was just a bare bones basic example that I put together to demonstrate when the issue occurs and when it doesn't. The while(true) is there so the RemoteLib object is instantiated just once (to demonstrate that it works fine in that situation)

P.P.S. "Do your thinking in your own time ..." wtf? I didn't know I was your employee lol

If you cannot take criticism, don't ask questions.

Thicken your Skin.

As Robin2 said: Slow Down. Ask a question, expect responses.

Robin2: Slow down a bit. Do your thinking in your own time and post your comments when you have the thinking complete.

When you ask a chain of questions it seems you really don't want an answer, you just want to vent.

like:

What if I do this? (insert question here>) But what if I do this? (insert different question here>) But I really want to do it this way? (insert example of non working code here>)

And when someone calls out 'bad coding' practices you call them a troll. a troll is someone that calls out mistakes to belittle a poster. Robin2 has valid critiques. The Arduino environment calls the loop() function continously. But every time around the loop() it can do some house keeping. when you put that:

void loop(){
while(true){
  //stay here fore ever!
 }
}

Some of the Arduino environment will not funtion as expected.

also, the setup() function is by definition called only once after reset, all of your one time initialization code should be called from it.

Chuck.

p.s. no one called you stupid(incapable of learning), you are ignorant (lacking knowledge) get over it and learn.

Any ideas guys?

Most new-bees make the mistake of trying to power the servo from the arduino or from an adequate power supply.

zoomkat: or from an adequate power supply.

I presume you meant "an inadequate".

I also presume you will not respond with "wtf?" :)

...R

Inadequate is correct. late night posting!