BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Stopping if something else gets selected

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();

  
}

Using blocking delays like this will cause Blynk to disconnect from the server. It’s also what is causing the app to be unresponsive while the loop executes. Instead you should use a BlynkTimer in Timeout mode, probably in a Lambda function.

The code to be looped needs to be in a separate function which is either called with a BlynkTimer.

Is your other topic now solved?

Pete.

I’m not that good at Code
I tried to google the things you sed I needed to change but I couldn’t really understand the example codes. How wold a BlynkTimer in timeout mode look in my use case?
I guess that I need to change the
#define delayTime 10
line to a BlynkTimer in timeout mode. but the examples in the Blynk documentation wore really confusing.

and my other topic is kinda solved. it works now but the light doesn’t change brightness as I use the slider. i need to go into the manu widget and then select the the mode again for it to update the brightnes

I also don’t know what to tell the code to stop and look for. I understand that the delay thing stops it from taking in commands, and that i need to check if there is a new thing selected in the menu, But how does that look in code format?

Thanks for all the support so far!

I’m not going to write your code for you, and I doubt that other forum members will either.

I’d suggest that you do more research, experimentation and look at similar projects built by other people to get some inspiration.

Pete.

Ok :+1:
But Thanks for all the help