for my project i am trying to make a 5050 LED strip fade through all the colors( i have achieved this)
but combine it with LEDs reacting to sound from a sound sensor (i have achieved this). However i am having trouble combining these two separate codes.
Code for fade colors:
// Assign LED Output PWM Pins
int Red = 5;
int Green = 6;
int Blue = 3;
//Set Initial Values
int RedVal = 0;
int GreenVal = 0;
int BlueVal = 0;
int fade = 10; // Delay Time
// Colour Value 1 as each colour intensity (0-255)
int RedVal1 = 186;
int GreenVal1 = 0;
int BlueVal1 = 255;
// Colour Value 2
int RedVal2 = 9;
int GreenVal2 = 239;
int BlueVal2 = 26;
//Colour Value 3
int RedVal3 = 255;
int GreenVal3 = 120;
int BlueVal3 = 0;
//Colour Value 4
int RedVal4 = 0;
int GreenVal4 = 255;
int BlueVal4 = 78;
//Set initial mode (Colour Value Mode) to Colour Value 1
int mode = 1;
void setup() {
//----------------------- Assign outputs
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Blue, OUTPUT);
//----------------------- Write Initial Values of 0 to outputs
analogWrite(Red, RedVal);
analogWrite(Green, GreenVal);
analogWrite(Blue, BlueVal);
}
void loop() {
while(mode == 1){
if(RedVal < RedVal1){ // If RedVal is less than desired RedVal1
RedVal ++; // increment RedVal
} else if(RedVal > RedVal1){ // If RedVal is more than desired RedVal1
RedVal --; // decrement RedVal
} else if(RedVal == RedVal1){ // If RedVal is equal to desired RedVal1
//Do Nothing
}
// Repeated as above for BlueVal
if(BlueVal < BlueVal1){
BlueVal ++;
} else if(BlueVal > BlueVal1){
BlueVal --;
} else if(BlueVal == BlueVal1){
//Do Nothing
}
// Repeated as above for GreenVal
if(GreenVal < GreenVal1){
GreenVal ++;
} else if (GreenVal > GreenVal1){
GreenVal --;
} else if (GreenVal == GreenVal1){
// Do Nothing
}
// Now we have a new set of values, we write them to the PWM Pins.
analogWrite(Red, RedVal);
analogWrite(Green, GreenVal);
analogWrite(Blue, BlueVal);
delay(fade); // Implement a bit of delay to slow the change of colour down a bit
}
if(RedVal == RedVal1 && GreenVal == GreenVal1 && BlueVal == BlueVal1){ // If RedVal & GreenVal & BlueVal are all at the desired values
delay(1500); // Delay to hold this colour for a little while
mode = 2; // Change the mode to the next colour. Exiting this while loop and into the next one
}
while(mode == 2){
if(RedVal < RedVal2){
RedVal ++;
} else if(RedVal > RedVal2){
RedVal --;
} else if(RedVal == RedVal2){
//Do Nothing
}
if(BlueVal < BlueVal2){
BlueVal ++;
} else if(BlueVal > BlueVal2){
BlueVal --;
} else if(BlueVal == BlueVal2){
//Do Nothing
}
if(GreenVal < GreenVal2){
GreenVal ++;
} else if (GreenVal > GreenVal2){
GreenVal --;
} else if (GreenVal == GreenVal2){
// Do Nothing
}
analogWrite(Red, RedVal);
analogWrite(Green, GreenVal);
analogWrite(Blue, BlueVal);
delay(fade);
if(RedVal == RedVal2 && GreenVal == GreenVal2 && BlueVal == BlueVal2){
delay(1500);
mode = 3;
//break;
}
}
while(mode == 3){
if(RedVal < RedVal3){
RedVal ++;
} else if(RedVal > RedVal3){
RedVal --;
} else if(RedVal == RedVal3){
//Do Nothing
}
if(BlueVal < BlueVal3){
BlueVal ++;
} else if(BlueVal > BlueVal3){
BlueVal --;
} else if(BlueVal == BlueVal3){
//Do Nothing
}
if(GreenVal < GreenVal3){
GreenVal ++;
} else if (GreenVal > GreenVal3){
GreenVal --;
} else if (GreenVal == GreenVal3){
// Do Nothing
}
analogWrite(Red, RedVal);
analogWrite(Green, GreenVal);
analogWrite(Blue, BlueVal);
delay(fade);
if(RedVal == RedVal3 && GreenVal == GreenVal3 && BlueVal == BlueVal3){
delay(1500);
mode = 4;
//break;
}
}
while(mode == 4){
if(RedVal < RedVal4){
RedVal ++;
} else if(RedVal > RedVal4){
RedVal --;
} else if(RedVal == RedVal4){
//Do Nothing
}
if(BlueVal < BlueVal4){
BlueVal ++;
} else if(BlueVal > BlueVal4){
BlueVal --;
} else if(BlueVal == BlueVal4){
//Do Nothing
}
if(GreenVal < GreenVal4){
GreenVal ++;
} else if (GreenVal > GreenVal4){
GreenVal --;
} else if (GreenVal == GreenVal4){
// Do Nothing
}
analogWrite(Red, RedVal);
analogWrite(Green, GreenVal);
analogWrite(Blue, BlueVal);
delay(fade);
if(RedVal == RedVal4 && GreenVal == GreenVal4 && BlueVal == BlueVal4){
delay(1500);
mode = 1; // Set mode back to 1 to return to the original colour.
//break;
}
}
}
code for LEDS turning on with sound:
int DO = 2; //Pin for Digital Output - DO
int DA = A0; // Pin for Analog Output - AO
int threshold = 237; //Set minimum threshold for LED lit
int sensorvalue = 0;
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;
void setup() {
//Serial.begin(9600);
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
redNow = random(255);
blueNow = random(255);
greenNow = random(255);
redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;
}
void loop() {
sensorvalue = analogRead(DA); //Read the analog value
//Serial.print("Analog: ");
//Serial.print(sensorvalue); //Print the analog value
//Serial.print(" ");
//Serial.print("Digital: ");
//Serial.println(digitalRead(DO)); //Print the digital value
if (sensorvalue >= threshold) { //Compare analog value with threshold
analogWrite(BLUEPIN, blueNow);
analogWrite(REDPIN, redNow);
analogWrite(GREENPIN, greenNow);
redNew = random(255);
blueNew = random(255);
greenNew = random(255);
analogWrite(BLUEPIN, blueNew);
analogWrite(REDPIN, redNew);
analogWrite(GREENPIN, greenNew);
delay(1);
}
}
has anyone done this before? or can guide me. I tried putting the sound code within the fade code withing every while mode 1,2,3,4 but it would just make the fade go from smoothly fading, to fading and flickering (but not very noticeable)
My goal is to: make the fade "stop" when the sound sensor reaches analog> threshold and continue when no sound is heard. OR make a random color pop out when the analog> threshold and then continue the fade when no sound is heard.