Hi guys
Only second post on the forum, so bear with me.
My goal is to be able to turn on several LEDs on in a random order using the serial monitor and have them turned on for 30 seconds before switching off by themselves.
I have been trying to get the mills() function to work with one LED with no luck.
Here is my code:
const int ledBlue = 3;
const int ledRed = 4;
unsigned long interval = 30000;
bool ledState = true;
unsigned long previousMillis = 0;
int blueLedState = 0;
int redLedState = 0;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledBlue, OUTPUT);
pinMode(ledRed, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
unsigned long currentMillis = millis();
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
{
if( (readString.indexOf("blue") >= 0))
{
digitalWrite(ledBlue, HIGH);
previousMillis = currentMillis;
ledState = HIGH;
}
}
if((ledState == HIGH) && (currentMillis - previousMillis > interval))
{
digitalWrite(ledBlue, LOW);
ledState = LOW;
}
readString="";
}
}
Now, for some reason the LED doesn't turn off but when the 30 seconds of my millisecond interval has past I can type a random character in the serial monitor and then the light turns off. That is not intended at all.
I've been at it all day and I have been trying to understand both the blink without delay sketch and several other sketches that handles multitasking and timing with mills() and even though I think I understand a fair bit of it, I still can't seem to get this working.
Help would be dearly appreciated!
Your code formatting makes it very hard to see (tip, press ctrl+T in teh IDE and remove unnessecairy {} and ()) but at the end of if (readString.length() >0) { you always clear the string....
And the turning off part is in the if (readString.length() >0) { part 
I am fairly new myself, and only a few days ago had my own timer interval to figure out.
Try:
const int ledBlue = 3;
const int ledRed = 4;
unsigned long interval = 30000;
bool ledState = true;
unsigned long timestamp;
int blueLedState = 0;
int redLedState = 0;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledBlue, OUTPUT);
pinMode(ledRed, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
{
if( (readString.indexOf("blue") >= 0))
{
digitalWrite(ledBlue, HIGH);
timestamp = millis();
ledState = HIGH;
}
}
if((ledState == HIGH) && (millis() - timestamp > interval))
{
digitalWrite(ledBlue, LOW);
ledState = LOW;
}
readString="";
}
}
And some tips, switch to strings (which is different then Strings) See Robin2's Serial input basics for a better approach.
And go and use arrays
That will save you from typing all the code another time to do the same for another color 
Get rid of the useless delay() (although it's saving your ass now :p).
@Perehama, you still clear the string and have the turn off part inside the id 
As the code has to handle two identical objects (two pulsing LEDs), I would use a class to handle the LEDs.
Collecting serial data in a buffer (up to 30 chars) when it is available and to check the content of a line
when the end of line was received could be done like this:
class PulsePin {
byte pin;
unsigned long started;
unsigned long activeFor;
public:
PulsePin(byte iPin) : pin(iPin), started(0), activeFor(0) { }
void begin() {
pinMode(pin, OUTPUT);
}
void pulse(unsigned long duration) {
digitalWrite(pin, HIGH);
started = millis();
activeFor = duration;
}
void process() {
if (activeFor && (millis() - started >= activeFor)) {
activeFor = 0;
digitalWrite(pin, LOW);
}
}
};
const byte ledBluePin = 3;
const byte ledRedPin = 4;
const unsigned long interval = 30000;
PulsePin BlueLED(ledBluePin);
PulsePin RedLED(ledRedPin);
void setup() {
Serial.begin(250000);
BlueLED.begin();
RedLED.begin();
}
void loop() {
bool doCheck = false;
const byte sCBMax = 30;
static char sCBuffer[sCBMax];
static byte buffIndex = 0;
while (Serial.available()) {
byte inChar = Serial.read();
if (inChar == 10) {
continue;
} else if (inChar == 13) {
doCheck = buffIndex;
} else {
if ((buffIndex == 0) && isWhitespace(inChar)) {
continue;
}
sCBuffer[buffIndex++] = inChar;
doCheck = (buffIndex == (sCBMax - 1));
}
if (doCheck) {
sCBuffer[buffIndex] = 0;
if (strstr_P(sCBuffer, PSTR("blue"))) {
BlueLED.pulse(interval);
}
if (strstr_P(sCBuffer, PSTR("red"))) {
RedLED.pulse(interval);
}
buffIndex = 0;
doCheck = false;
}
}
BlueLED.process();
RedLED.process();
}