Hi Everyone,
What I'm trying to accomplish is to turn on my LEDs with fading in effect by serial data from my Keyboard. I got it to work the way I want it to but how do I stop it from repeating the fading in effect from the beginning when the LEDs are at its maximum brightness?
Example: When I pressed the "H" key on my keyboard the LEDs starting to fade from 0 to maximum brightness, but when I pressed "H" again its repeat the same action, I don't want that. Is there a way to keep its at the full brightness even if I press the "H" key on my keyboard again. Thanks Everyone.
int LED = 9; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup()
{
Serial.begin(9600); // initialize serial communication:
pinMode(LED, OUTPUT); // initialize the LED pin as an output:
}
void loop()
{
if (Serial.available() > 0) // see if there's incoming serial data:
{
incomingByte = Serial.read(); // read the oldest byte in the serial buffer:
if (incomingByte == 'H') // if it's a capital H (ASCII 72), turn on the LED:
{
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) // fade in from min to max in increments of 1 points:
{
analogWrite(LED, fadeValue); // sets the value (range from 0 to 255):
delay(30); // wait for 30 milliseconds to see the dimming effect
}
}
if (incomingByte == 'L') // if it's an L (ASCII 76) turn off the LED:
{
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) // fade out from max to min in increments of 1 points:
{
analogWrite(LED, fadeValue); // sets the value (range from 0 to 255):
delay(30); // wait for 30 milliseconds to see the dimming effect
}
}
}
}