Bluetooth on Arduino Uno

Hello, I've been struggling with this for too long. I just can't figure out how to do it. I have the Arduino Uno.

I'm using my Arduino to constantly drive an array of LEDs (1000 individual LEDs to be exact). I wrote a bunch of different functions that each create some cool animations that take minutes to run through. The thing is I want to use bluetooth and pair with my android (which I can do no problem) and switch the animations on the fly from my phone.

The problem is that I can't figure out how to listen for serial input while the ATmega328 is executing other code. It's almost like I need multiple threads (that's how'd I'd do it in C#).

I need the code that is running, to stop when serial communication is received. However, I can't just sit in a while loop listening for for serial communication. So I've been messing around with attachInterrupt() but it doesn't look like that will work since delay()'s won't work in the called function, not to mention that serial() communication work work either since IT is interrupt driven....

Do I need to use the Arudino Mega? Do I need to use two Arduinos, one for constantly listening for serial communication and the other to drive my LED array?

I've looked at http://arduiniana.org/libraries/newsoftserial/ but that doesn't seem to do what I need. I dunno, Is there a way to passively fill a buffer, and then interrupt when something is in that buffer?

Any ideas?

You need to restructure your program so there are no hard coded delays and everything is a state machine. So instead of doing all of the animations sequentially, and then pausing for input, you do one step, and check if input is available giving a new command is available via bluetooth (via the available class function that returns true if data is available to be read). In the normal case, there is no command, and you just go to the next step in your animation. The example Blink without Delay is the usual place people are pointed to go study on how to eliminate the delay function.

An alternative is to attach an interrupt to the input (on the Uno, I believe you can attach an interrupt if there is a state change on pin 2 and/or 3). Then either set a flag, that you check periodically in your loop, or you raise an exception to abort the current action.

I think you're right. I appreciate the response. I might just have to go through and redo a ton of code. Oh well.

For anyone else looking into interrupts on the arduino I got pretty deep into setting specific registers and found this page very helpful https://sites.google.com/site/qeewiki/books/avr-guide/usart