I have an arduino Uno, and have 2 LED strips connected to it (one with White LEDS and the other strip with Red LEDs).
I want the WHITE LED strip to be on, then trigger a shock sensor to make the RED LEDs strobe a few times, and then the RED LEDs to fade out, finally the White LEDs to go back on. So far I have been succesfull!! except of the FADING part.
When it comes to the fading pat of the code, I created a separate code to Test the fading of the RED LEDs, and it works great by it self! only!, code as follows:
//----- LED FADE SAMPLE CODE (It works great!)-------
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
pinMode(led, OUTPUT); // declare pin 9 to be an output:
}
void loop() {
analogWrite(led, brightness); // set the brightness of pin 9:
brightness = brightness + fadeAmount; // change the brightness for next time through the loop:
if (brightness == 0 || brightness == 255) // reverse the direction of the fading at the ends of the fade:
{
fadeAmount = -fadeAmount ;
}
delay(30); // wait for 30 milliseconds to see the dimming effect
}
-----------------------------------------------------------------------------------------------------------
So I figured if I just copy and paste this code after my sequencing code, it would work just fine.
But it does not! :(, i'm trying my best to understand
Show me the light please!,for I have much to learn!, thank you!
My code with the fading part at the end as follows: (Everything works great.. EXEPT Fading NOT WORKING )
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
// initialize the LED pin as an output:
pinMode(led, OUTPUT);
pinMode(6, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(2, INPUT);
}
void loop()
{
buttonState = digitalRead(2); // read the state of the pushbutton value:
if (buttonState == LOW) // if it is, the buttonState is HIGH:
{
//------START!----------------------------------------------------------------
//------WHITE LED OFF---------------------------------------------------------
digitalWrite(6, LOW); // turn White LED off:
//-----RED LED Strobe START---------------------------------------------------
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(100); // wait
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(21); // wait
digitalWrite(9, LOW); // turn the Red LED off by making the voltage LOW
delay(15);
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(20); // wait
digitalWrite(9, LOW); // turn the Red LED off by making the voltage LOW
delay(16);
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(19); // wait
digitalWrite(9, LOW); // turn the Red LED off by making the voltage LOW
delay(17);
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(18); // wait
digitalWrite(9, LOW); // turn the Red LED off by making the voltage LOW
delay(18);
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(17); // wait
digitalWrite(9, LOW); // turn the Red LED off by making the voltage LOW
delay(19);
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(16); // wait
digitalWrite(9, LOW); // turn the Red LED off by making the voltage LOW
delay(20);
digitalWrite(9, HIGH); // turn the Red LED on (HIGH is the voltage level)
delay(15); // wait
digitalWrite(9, LOW); // turn the Red LED off by making the voltage LOW
delay(22);
//---------RED LED Strobe END-------------------------------------------------
//---------RED LED FADE START---------------------------------------------------------------------
//---------THIS IS THE FADE FOR THE RED LED--------- IT DOES NOT WORK & I DONT KNOW WHY, HELP!!!----
analogWrite(led, brightness); // set the brightness of pin 9:
brightness = brightness + fadeAmount; // change the brightness for next time through the loop:
if (brightness == 0 || brightness == 255) // reverse the direction of the fading at the ends of the fade:
{
fadeAmount = -fadeAmount ;
}
delay(30); // wait for 30 milliseconds to see the dimming effect
}
//-------RED LED FADE END-------------------------------------------------------------------------
digitalWrite(9, LOW); // turn RED LED off:
//-------White LED ON-------------------------------------------------------------------------------
digitalWrite(6, HIGH); // turn White LED on:
}
Moderator edit: Post rendered less shouty