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.