some curious problems

EDIT: seems to be fixed by using Arduino 1.5.2 instead of 1.0.1

Hi,

I was trying to enhance a RGB led library I wrote some time ago as a first library I made.

When using it I get some curious results.
For some reason the sketch seems to reset itself but I can't figure out why...

When I run this sketch all is well but as soon as I uncomment the Serial.println(" "); in debugMyRGBvalues():

void debugMyRGBvalues() {
	//Serial.print(myLEDstrip.RedVal);Serial.print(",");
	//Serial.print(myLEDstrip.GreenVal);Serial.print(",");
	//Serial.print(myLEDstrip.BlueVal);
	Serial.println(" ");
}

the code will run upto else if (i==4) { then starts over again in void setup()

void loop()
{
	curMillis = millis();
	if ((curMillis - prevMillis) > 1000) {
		
		Serial.print("i=");Serial.print(i);Serial.print(" ");
		
		if (i==0) {
			myLEDstrip.SetRedLight(255);
			Serial.print("Red ");
			debugMyRGBvalues();
		}
		else if (i==1) {
			myLEDstrip.SetGreenLight(255);
			Serial.print("Green ");
			debugMyRGBvalues();
		}
		 else if (i==2) {
			myLEDstrip.SetBlueLight(255);
			Serial.print("Blue ");
			debugMyRGBvalues();
		}
		 else if (i==3) {
			myLEDstrip.SetWhiteLight(255);
			Serial.print("White ");
			debugMyRGBvalues();
		}
		 else if (i==4) {
			myLEDstrip.LEDstripOFF();
			Serial.print("Off ");
			debugMyRGBvalues();
		}
		
		 else if (i<10) {
			myLEDstrip.SetRandomColour();
			Serial.print("Random ");
			debugMyRGBvalues();
		}
		 else if (i<15) {
			 int RR = 111;
			myLEDstrip.SetRGBColour(RR,50,200);
			 Serial.print("Set RGB ");
			debugMyRGBvalues();
		}
		 else if (i<20) {
			//myLEDstrip.SetHSLColour(200,100,50);
			Serial.print("Set HSL ");
			debugMyRGBvalues();
		
	}
		else  {
			i=-1;
			Serial.println("");
		}
		

	
	
	i++;
	
	prevMillis = curMillis;
	
	}

}

Same happens if I change int RR = 111; to int RR = random(255);

And If I uncomment myLEDstrip.SetHSLColour(200,100,50);

To make a long story short: the sketch becomes very unpredictable and unreliable :frowning:

It is probably a minor thing that causes this but I have no clue where to look...
I hope someone can shed some RGB light on this :blush:

LEDstrip.cpp (5.49 KB)

keywords.txt (184 Bytes)

LEDstrip.h (1.79 KB)

RGB_LEDstrip.ino (1.73 KB)

The library files are here: https://www.box.com/s/bmwbxdtsihuxnb9oc7ez
And the sketch is here: https://www.box.com/shared/static/jpzk5cjsf58pscappb9l.ino

Maybe, but I'm not going there. The proper way to provide code is to use the Additional Options link and attach the code to your post.

Your symptoms suggest that you are running out of memory. Using the F() macro around literal strings may help, if you have a lot of them.

PaulS:
Maybe, but I'm not going there. The proper way to provide code is to use the Additional Options link and attach the code to your post.

If you prefer it that way I'll add the code as attachment as well.... see original post.

Hmmm, running out of memory.. Can't imagine why.
The code used to convert between HSL and RGB and vice versa has quite some floats but those commands are commented out.

A switch statement, instead of a bazillion if/else if statements would make for easier to read, and faster, code.

		int RedVal;
		int GreenVal;
		int BlueVal;
		int HueVal;
		int SaturationVal;
		int LightnessVal;

Why are these public? You should have get methods to get the values, or a Dump() method to dump them.

The code used to convert between HSL and RGB and vice versa has quite some floats but those commands are commented out.

No. The call to the function is commented out. Does commenting that call out significantly reduce the flash memory usage?

You can see if running out of SRAM is a problem. Arduino Playground - AvailableMemory

Alban:
When I run this sketch all is well but as soon as I uncomment the Serial.println(" "); in debugMyRGBvalues():

the code will run upto else if (i==4) { then starts over again in void setup()

Not for me it doesn't (on a Uno).

?RGB LEDstrip
i=0 Red 255,0,0 
i=1 Green 0,255,0 
i=2 Blue 0,0,255 
i=3 White 255,255,255 
i=4 Off 0,0,0 
i=5 Random 232,19,158 
i=6 Random 38,175,197 
i=7 Random 114,68,188 
i=8 Random 109,120,80 
i=9 Random 102,47,102 
i=10 Set RGB 111,50,200 
i=11 Set RGB 111,50,200 
i=12 Set RGB 111,50,200 
i=13 Set RGB 111,50,200 
i=14 Set RGB 111,50,200 
i=15 Set HSL 111,50,200 
i=16 Set HSL 111,50,200 
i=17 Set HSL 111,50,200 
i=18 Set HSL 111,50,200 
i=19 Set HSL 111,50,200 
i=20 
i=0 Red 255,0,0 
i=1 Green 0,255,0 
i=2 Blue 0,0,255 
i=3 White 255,255,255 
i=4 Off 0,0,0 
i=5 Random 143,142,79 
i=6 Random 160,52,3 
i=7 Random 124,114,32 
i=8 Random 70,18,189 
i=9 Random 123,116,190 
i=10 Set RGB 111,50,200 
i=11 Set RGB 111,50,200 
i=12 Set RGB 111,50,200 
i=13 Set RGB 111,50,200 
i=14 Set RGB 111,50,200 
i=15 Set HSL 111,50,200 
i=16 Set HSL 111,50,200

Therefore it's probably something electrical.

Weird, but I think I found it.

@Nick: I guess you're using arduino IDE 1.5.2!? I noticed that that version works for me while it was Arduino IDE 1.0.1 that gave me problems...

.@PaulS: thanks for making the memory usage a bit more clear to me. Maybe you can give me an example on how I could make a nice Get method to retrieve the data? Like I say in my signature, my background is more mechanical than software...

Maybe you can give me an example on how I could make a nice Get method to retrieve the data?

public:
    int getRadVal() { return RedVal; }
    void setRedVal(int newRedVal) { RedVal = newRedVal; }

private:
    int RedVal;

Normally, I don't like code in the header file, but for simple setters and getters, it's OK.

With some const:

public:
    int getRadVal() const { return RedVal; }
    void setRedVal(const int newRedVal) { RedVal = newRedVal; }

What is the purpose of the const? In the setter case, the argument is not modified, and since it is passed by value, anyway, I don't see the purpose of making it const. Can you explain why you would?

In the getter, I don't understand what is being made const.

In the setter case, the argument is not modified, and since it is passed by value, anyway, I don't see the purpose of making it const. Can you explain why you would?

Documentation. As a general principle to make arguments you don't plan to change, const. Although admittedly since they are passed by value there isn't an enormous amount of point in this case. If you happened to be passing in a struct or something like that, by reference, marking it const makes it clear that although it might be possible to change it, you won't.

In the getter, I don't understand what is being made const.

You are promising to the compiler that this function does not modify anything in the underlying class (which a getter should not do, obviously). This may conceivably generate better code, and also catches errors (perhaps in a more complex function) where you have a brain fade, and the getter actually changes something.

OK. Thanks.