Hello,
i have a problem with my Arduino sketch an i need prof help ;-).
What i want do to: control a RGB-LED stripe over bluetooth I receive a char over bluetooth modul RN42-5428. It works.
Is the char = 'r' then color red goes high (255) --> It works.
Is the char = 'b' then color blue goes high --> it works.
With the char 'f' i want to fade the color. In this example only red goes high and down. When i send char 'f' it will only run one time. If i send 'f' again and again the color red goes step for step higher. But it should fade until a new char is received. Can anybody help me. Here is the code:
define GRN_PIN 10
define RED_PIN 9
define BLU_PIN 11
char input;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
unsigned long currentTime;
unsigned long loopTime;
void setup()
{
analogWrite(GRN_PIN, 0);
analogWrite(RED_PIN, 0);
analogWrite(BLU_PIN, 0);
currentTime = millis();
loopTime = currentTime;
Serial.begin(9600);
//Bluetooth Serial
Serial2.begin(38400);
}
void loop()
{
if(Serial2.available() )
{
input = (char)Serial2.read();
if(input == 'r')
{
red();
}
else if (input == 'b')
{
blue();
}
else if (input == 'f')
{
fade();
}
else if (input == 'a')
{
aus();
}
}
}
void red()
{
digitalWrite(RED_PIN, HIGH); //LED an
digitalWrite(GRN_PIN, LOW);
digitalWrite(BLU_PIN, LOW);
}
void blue()
{
digitalWrite(RED_PIN, LOW); //LED an
digitalWrite(GRN_PIN, LOW);
digitalWrite(BLU_PIN, HIGH);
}
void aus()
{
digitalWrite(RED_PIN, LOW); //LED an
digitalWrite(GRN_PIN, LOW);
digitalWrite(BLU_PIN, LOW);
}
void fade()
{
while(!Serial2.available() )
{
currentTime = millis();
if (currentTime >= (loopTime + 40)) {
//set the brightness of pin 9:
analogWrite (RED_PIN, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount;
// Serial.println(brightness);
}
loopTime = currentTime; // Updates loopTime
}
}
}