Pages: [1]   Go Down
Author Topic: some curious problems  (Read 496 times)
0 Members and 1 Guest are viewing this topic.
Near Rotterdam in Holland
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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():
Code:
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
Code:
else if (i==4) {
then starts over again in void setup()

Code:
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
Code:
int RR = 111;
to
Code:
int RR = random(255);

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

To make a long story short: the sketch becomes very unpredictable and unreliable smiley-sad

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  smiley-red

* LEDstrip.cpp (5.49 KB - downloaded 26 times.)
* keywords.txt (0.18 KB - downloaded 9 times.)
* LEDstrip.h (1.79 KB - downloaded 31 times.)
* RGB_LEDstrip.ino (1.73 KB - downloaded 11 times.)
« Last Edit: October 22, 2013, 12:06:51 pm by Alban » Logged

Best regards,

Alban

P.S. sorry for asking silly questions... I'm a mechanical engineer and not an electronics engineer smiley-wink


www.reptile-addict.nl

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Near Rotterdam in Holland
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 19, 2013, 06:56:53 am by Alban » Logged

Best regards,

Alban

P.S. sorry for asking silly questions... I'm a mechanical engineer and not an electronics engineer smiley-wink


www.reptile-addict.nl

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Quote
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. http://playground.arduino.cc/Code/AvailableMemory
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


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

Code:
…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.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Near Rotterdam in Holland
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Best regards,

Alban

P.S. sorry for asking silly questions... I'm a mechanical engineer and not an electronics engineer smiley-wink


www.reptile-addict.nl

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Maybe you can give me an example on how I could make a nice Get method to retrieve the data?
Code:
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.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

With some const:

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

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
« Last Edit: October 22, 2013, 06:15:06 pm by Nick Gammon » Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK. Thanks.
Logged

Pages: [1]   Go Up
Jump to: