help required with serial communication

Hello, I am working on a music reactive LED system. I reffered to- http://missionduke.com/wordpress/arduino-projects/
Project Name: Arduino LED Light Show To Music.
In this the lighting pattern is sent by processing over serial communication to arduino. The arduino then lights the LEDs. But i also want the LEDs to stay high if there isnt a serial communication or simply when the usb isnt connected to the pc but say a 5v adaptor. I have edited the code i got from mission duke but the leds are always high even when the serial communication is active. Here is the arduino original code form missionduke:

const int vectledPin[]={3,5,6,9,10,11};

/*These are the values that will be given to the PWM ports on the arduino. The arduino will receive values between 0 and 30,
that measures the "volume" linearlly. However, the LED's don't bright linearlly, so I tested it a bit and put these values
as my own testing. I used the DIMMER sketch example on the arduino program to know what values to use.
Feel free to change them. One small change would be to make two vectors, as I used two different types of LEDs.*/

const int vectbright[]={0,1,2,3,4,6,8,19,14,18,22,27,32,38,47,56,66,76,86,96,105,114,122,130,138,148,162,190,222,255,255};
int i=0;

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  for (i=0;i<6;i++)
    pinMode(vectledPin[i], OUTPUT);
  i=0;
}

void loop() {
  byte brightness;
  byte led;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    led=(brightness>>5);
    //This reads what LED it is (it is stored on the 3 high bits)
    
    brightness=((brightness)&0x1F);
    //and this reads the 5 lower bits only. It puts a 0 in the higher ones.
    
   analogWrite(vectledPin[led], vectbright[brightness]);
   //and finally, using both vectors and the brightness index, the PWM value is sent
  }
}

And here is my edited one:

const int vectledPin[]={3,5,6,9,10,11};
const int vectbright[]={0,1,2,3,4,6,8,19,14,18,22,27,32,38,47,56,66,76,86,96,105,114,122,130,138,148,162,190,222,255,255};
int i=0;

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  for (i=0;i<6;i++)
    pinMode(vectledPin[i], OUTPUT);
  i=0;
}

void loop() {
  byte brightness;
  byte led;

  // check if data has been sent from the computer or there is no communication:
  if (Serial.available()> 0) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    led=(brightness>>5);
    //This reads what LED it is (it is stored on the 3 high bits)
    
    brightness=((brightness)&0x1F);
    //and this reads the 5 lower bits only. It puts a 0 in the higher ones.
    
   analogWrite(vectledPin[led], vectbright[brightness]);
   //and finally, using both vectors and the brightness index, the PWM value is sent
  }
  else {
for (i=0;i<6;i++) // if there is no communication
  {{ 
digitalWrite(vectledPin[i], HIGH);
}
digitalWrite(vectledPin[i], LOW);
}}
}

P.S: excuse me if this sounds noob isnt there an if (Serial.unavailable()) command? It will solve my problem immediately.

I don't know why your ELSE clause sets the Leds both HIGH and LOW - what you are seeing may be the average of the two. Take out the HIGH line.

If that has no effect then I think you need more sophisticated code to detect whether there is data arriving.

You must keep in mind that the Arduino loop()s very much faster than serial data arrives and even when connected Serial.avaialable() will probably be false most of the time.

You probably need code that detects the interval between characters and if it exceeds some value you can assume there is no connection.

...R

P.S: excuse me if this sounds noob isnt there an if (Serial.unavailable()) command? It will solve my problem immediately.

That is a silly question. It sounds like you don't understand what Serial.available() is telling you.

Even if you assume that Serial.available() tells you only whether there is, or is not, data waiting to be read (not the case), anyone can figure out how to write an else clause to deal with the no data situation.

@robin2: You are right. I tried everthing with else but the loop is running faster and therefore the leds are high even if there is a serial comm. As per your idea will try to do it with an interval check code.
@PaulS: Well this is my first time with serial comm. i just had an overview so it isnt very clear to me. Believe me while writing that i heared " you are a nooobb..." echo in my head :stuck_out_tongue:
Anyways thanks for the help.

I tried an interval check code on the serial communication but the code didnt work. So i have added an interrupt routine so that on recieving an interrupt it lights all the LEDs high. Arduino is working fine on my desktop pc, but if i run it from my laptop it starts acting funny. When i keep the ac adaptor connected to the laptop the LEDs light up brighter than expected but if i remove the adaptor it works normal.
Is this some grounding issue with the laptop?

const int vectledPin[]={3,5,6,9,10,11};

const int vectbright[]={0,1,2,3,4,6,8,19,14,18,22,27,32,38,47,56,66,76,86,96,105,114,122,130,138,148,162,190,222,255,255};
int i=0;

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  for (i=0;i<6;i++)
    pinMode(vectledPin[i], OUTPUT);
  i=0;
  attachInterrupt(0, allrise, RISING);
}

void loop() {
  byte brightness;
  byte led;
  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    led=(brightness>>5);
    //This reads what LED it is (it is stored on the 3 high bits)
    
    brightness=((brightness)&0x1F);
    //and this reads the 5 lower bits only. It puts a 0 in the higher ones.
    
   analogWrite(vectledPin[led], vectbright[brightness]);
   //and finally, using both vectors and the brightness index, the PWM value is sent
  }
}
void allrise() {
  for (i=0;i<6;i++)
    digitalWrite(vectledPin[i], HIGH);
}

I have never used a desktop PC with my Arduinos so I can't say if it different from my laptops - but I have never had a problem with them.

I can't see the point of your ISR because there will be many changes of pin0 for every character received. That would seem to completely swamp the logic within loop().

Why not record the value of millis() everytime a character is received and if no character is received for a period of time you can do whatever. Something like

  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    lastCharMillis = millis()
    brightness = Serial.read();

and, as the last thing in loop()

   if (millis() - lastCharMillis > interval) {  // interval might be 500 millisecs
      // turn all the LEDs on
   }

...R