How can I solve the error: break statement not within loop or switch

Hi :slight_smile:

I doing a project about detect light intensity from ISL29125 RGB light sensor with esp32.

I want to control that when I measure black colour, it will show text 'black' in serial monitor but when I measure the other colours, it will show light intensity of those colours in serial monitor. But I want the light intensity data shows only 20 values.

#include <Wire.h>
#include "SparkFunISL29125.h"

SFE_ISL29125 RGB_sensor;

void setup()
{
  
  Serial.begin(115200);

  if (!RGB_sensor.init())
  
  {
    Serial.println("Error");
  }
}

void loop()
{
  //Read light intensity of blue channel
  unsigned int blue = RGB_sensor.readBlue();
  int num = 10;
  int count = 0;
  //Detect black colour
  if((unsigned int) blue < 10000){
      Serial.println("black");
      delay(2000);
    }
  else {
  count++;
  //Show the light intensity of other colours
  Serial.print(blue,DEC);
  Serial.println();
  if (count>10){
  break
  }
  delay(2000);
  }
}

Then, I write this code but it shows this error ' break statement not within loop or switch' How can I solve this? Please help me

Thank you in advance.

A break would also have a semicolon.

What are you trying to do?
Break from what?

Thank you.
I trying to limit the data from RGB light sensor to be show only 10 values.

You'd have to make "count" static (or at worst, global) to do that.

Can you explain more? I don't understand. Please

Every time loop () is called, you set "count" to zero

How are you ever going to reach ten?

(Hint: by making "count" static. Or global)

is not required... remove it any the program will compile.. but as others have said... there are lots of other problems with your code that you need to fix to get it to do anything useful.

@chayapitcha_sae , your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

#include <Wire.h>
#include "SparkFunISL29125.h"

SFE_ISL29125 RGB_sensor;

void setup()
{
  Serial.begin(115200);

  if (!RGB_sensor.init())
  {
    Serial.println("Error");
  }
}

void loop()
{
  //Read light intensity of blue channel
  unsigned int blue = RGB_sensor.readBlue();
  int num = 10;
  static int count = 0;
  //Detect black colour
  if (blue < 10000u)
  {
    Serial.println("black");
    delay(2000);
  }
  else
  {
    count++;
    //Show the light intensity of other colours
    Serial.println(blue);
    if (count > 10)  // After 10 times...
    {
      while (1) ;  // ... Do nothing forever
    }
    delay(2000);
  }
}

Thank you so much

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.