Hello
I have taken parts of different sketches and tried to stitch them together to some extent.
the thing I need help with is a rainbow face script. I have a menu widget in the app with which I select a mode that I want my lights to be in. and one of them is to cycle through a rainbow. and I can’t figure out how to make the cycle to loop. because it is in a “case” I cant use a void loop (or so I think). and I also realized that if I suddenly want to change the led mode. I need to wait for the cycle to end fore anything to happen, I saw that this is possible by making the script pause and check if something else is selected, but that goes beyond my code knowledge. I didn’t understand how to implement that into my code.
and the code. it may not be the pritiest but it kinda works
int S;
BLYNK_WRITE(V2) // Widget WRITEs to Virtual Pin V2
{
S = param.asInt(); // the S will be used to change the brightness of the led
}
int red = 4;
int green = 0;
int blue = 2;
int redValue;
int greenValue;
int blueValue;
//**************************************************************
BLYNK_WRITE(V1) {
switch (param.asInt())
{
case 1: // white
analogWrite(4, 0);
analogWrite(0, 0);
analogWrite(2, 0);
analogWrite(4, (1023/S));
analogWrite(0, (1023/S));
analogWrite(2, (1025/S));
break;
case 2: // Item 2
analogWrite(4, 1023); // its here where the rainbow cykle starts
analogWrite(0, 0);
analogWrite(2, 0);
{
#define delayTime 10
for(int i = 0; i < 1023; i += 1)
{
redValue -= 1;
greenValue+= 1;
analogWrite(red, (redValue/S));
analogWrite(green, (greenValue/S));
delay(delayTime);
}
redValue = 0;
greenValue= 1023;
blueValue=0;
for(int i = 0; i < 1023; i += 1)
{
greenValue -= 1;
blueValue += 1;
analogWrite(green, (greenValue/S));
analogWrite(blue, (blueValue/S));
delay(delayTime);
}
redValue = 0;
greenValue= 0;
blueValue=1023;
for(int i = 0; i < 1023; i += 1)
{
blueValue -= 1;
redValue += 1;
analogWrite(blue, (blueValue/S));
analogWrite(red, (redValue/S));
delay(delayTime);
}
}
break;
case 3: // Item 3
analogWrite(4, 0);
analogWrite(0, 0);
analogWrite(2, 0);
Serial.println("Item 3 selected");
break;
default:
analogWrite(4, 0);
analogWrite(0, 0);
analogWrite(2, 0);
}
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
BLYNK_CONNECTED();
{
analogWrite(2, LOW);
analogWrite(3, LOW);
analogWrite(4, LOW);
}
}
void loop()
{
Blynk.run();
}