Strange compile behavior, please help

Need your help with really strange Spark behavior.

Sorry for the link to G+ but I’m restricted to 4 links since I’m a noob. See the full post with links here: https://plus.google.com/116265788926162401547/posts/5evzg4NfWkT

So I’ve been porting over some of the visualizations for my L3D Cube from Arduino to the Spark and I’ve found a some really strange behavior that I can’t explain. The specific viz takes the twinkle that Mark Kriegsman (FastLED) created and add’s support for the built-in mic to make it reactive to sound. I have it working well. You can see the original code here: http://pastebin.com/a5FKiXU3 and the viz here: https://www.youtube.com/watch?v=KVhQlGqAREU.

Here’s where it gets weird. I was making some seemingly innocent modifications to the code and I got very different behaviors.

1 - If you change line 93 to mic /=4; you get this: https://www.youtube.com/watch?v=ByfmYqot0SY. There are no twinkles and it is mildly reactive to sound and I’ve lost the palette.

2 - If you add a third call to dim8_raw after line 95, you get twinkles and reactive but no palette.

3 - If you take the code from line 92 to line 110 and move it to a separate function, you lose the smooth dimming.

Here’s another case of Spark strangeness. I start with Mark’s standard twinkle and I try to add in a rotating palette. So I’ve simplified the problem to just making a call to millis() in the loop and I get the same kind of jerky behavior as #3 above.

Anyone have any ideas?

So if you really want to twist your brain, I started diffing code from a viz that that didn’t compile to the one that did and started moving the non-working one toward the working one since it was mostly placement of the code and not a logical difference. Here is the diff:
https://www.diffchecker.com/fujdxbsp. The one on the right compiles and the one on the left gives the following:
twinkle.o: In function beat88': /spark/compile_service/shared/workspace/3_compile-server2/core-firmware/build/FastLED/lib8tion.h:1893: undefined reference toNSFastLED::get_millisecond_timer()'
collect2: error: ld returned 1 exit status
make: * [950147282ddcbec5a4340d26dcec74a91353017d2ee6f5044ece43c80918.elf] Error 1

Hi @PeteChestna

How are you compiling this? In the web IDE?

The web IDE has trouble with classes etc. in the .INO main sketch file since it runs the Spark preprocessor on that file. This preprocessor reorders your code to remove the need for forward references, function prototypes, etc. but it does not understand C++ and will create hard to figure out problems since it has completely rearranged your code.

If your .h and .cpp files are in separate tabs in the web IDE, that should be fine, but you cannot put headers and C++ classes in the main .ino file.

Yes, I’m using the web IDE. Your comment is very interesting but I don’t grok how identical code in one sketch compiles and the other doesn’t. Shouldn’t the re-order be deterministic? Also, I added the function prototypes to make it compile in the Web IDE. I didn’t have the problem with the Arduino IDE.

And yet more strangeness…

I took the working twinkle and removed the hard-coded palette value from line 125 and went from working to non-working. WTF?

Here’s a diff:
https://www.diffchecker.com/cng4blqf
And here’s the video. The first 5 seconds are the working sketch and the end of the video is the now broken sketch:

You are not going to get C++ .h and .cpp files work in the web IDE unless you move them to other tabs.

The difference between your two files in the diff tool is that one has a newline after the final #endif and the other does not and yes that does appear to make a difference since the preprocessor is regular expression based.

I wouldn’t bet on it :wink:
To stop any preprocessor oddities you can use

#pragma SPARK_NO_PREPROCESSOR

Then you’ll need to #include "application.h" and add function prototypes (where required).

I can’t believe that it gets better. So I did as you suggested and added the pragma, app.h and prototypes and I went full circle to the same exact compile error…

Here’s the diff:
https://www.diffchecker.com/ezwdyuek

Here’s the compile error:
twinkle.o: In function beat88': /spark/compile_service/shared/workspace/3_compile-server2/core-firmware/build/FastLED/lib8tion.h:1893: undefined reference toNSFastLED::get_millisecond_timer()'
collect2: error: ld returned 1 exit status
make: *** [fca13314714b92d06d5b97063e03625a371a8c6f280ac631adcc603878dd.elf] Error 1

In the strangest twist of all, I created another app, added the library and then copied the code from the non-compiling sketch and pasted it into a new sketch. The old one still doesn’t compile but the new one does. WTF!?