Why can I not use break;

This is my code:

#include <LiquidCrystal_I2C.h> // Use I2C library

const int address = 0x27; // Replace with your LCD's I2C address if different
LiquidCrystal_I2C lcd(address, 16, 2); // Define LCD dimensions

// Define the number of samples and curves
const int numSamples = 50;
const int numCurves = 4;

// Arrays to store collected data and existing curves
float collectedData[numSamples];
float existingCurves[numCurves][numSamples];
float voltage[numSamples];
// Names of the existing curves
const char* curveNames[] = {"low MIC", "CEF MIC", "CIP MIC", "GEN MIC"};

void setup() {
  Wire.begin(); // Initialize I2C communication
  lcd.init(); // Initialize the LCD
  lcd.backlight(); // Turn on the backlight (optional)
  Serial.begin(9600);
  // Initialize the existing curves (no need for loading here)
  float existingCurves[numCurves][numSamples] = {
    {8.5338, 7.7862, 3.8893, 2.0185, 1.1925, 0.7812, 0.55199, 0.41257, 0.32192, 0.25984, 0.21554, 0.18285, 0.15807, 0.13884, 0.12362, 0.11138, 0.10138, 0.093122, 0.08621, 0.080376, 0.075397, 0.071127, 0.067434, 0.064208, 0.061389, 0.058898, 0.056694, 0.054737, 0.052977, 0.051408, 0.049988, 0.048709, 0.04755, 0.046495, 0.04553, 0.044646, 0.043841, 0.043096, 0.042412, 0.041777, 0.04119, 0.040648, 0.040143, 0.03967, 0.039233, 0.038829, 0.038444, 0.038089, 0.03775, 0.03743
}, // Curve 1
    {8.534, 8.2735, 6.9479, 5.6951, 4.6544, 3.8372, 3.2085, 2.7273, 2.3588, 2.0768, 1.862, 1.7003, 1.5813, 1.4971, 1.4414, 1.4089, 1.3954, 1.3969, 1.4103, 1.4326, 1.4611, 1.4936, 1.5281, 1.5631, 1.5973, 1.6295, 1.6592, 1.6858, 1.7091, 1.7289, 1.7454, 1.7587, 1.7691, 1.7769, 1.7825, 1.7861, 1.7881, 1.7887, 1.7884, 1.7873, 1.7855, 1.7834, 1.781, 1.7784, 1.7757, 1.773, 1.7703, 1.7677, 1.7651, 1.7627
}, // Curve 2
    {8.3963, 8.3099, 7.784, 7.2455, 6.7251, 6.2413, 5.803, 5.4127, 5.0688, 4.7677, 4.5049, 4.2758, 4.0759, 3.9011, 3.748, 3.6135, 3.495, 3.3903, 3.2973, 3.2146, 3.1408, 3.0747, 3.0153, 2.9618, 2.9134, 2.8696, 2.8297, 2.7934, 2.7603, 2.7299, 2.7021, 2.6765, 2.6529, 2.6312, 2.611, 2.5924, 2.575, 2.5589, 2.5439, 2.5299, 2.5169, 2.5047, 2.4932, 2.4825, 2.4724, 2.463, 2.4539, 2.4454, 2.4368
}, // Curve 3
    {8.4942, 7.972, 5.7531, 4.3652, 3.6053, 3.1739, 2.9129, 2.7453, 2.6321, 2.5524, 2.4943, 2.4508, 2.4173, 2.391, 2.3701, 2.3531, 2.3392, 2.3276, 2.3178, 2.3095, 2.3025, 2.2964, 2.2911, 2.2865, 2.2824, 2.2788, 2.2757, 2.2728, 2.2703, 2.268, 2.2659, 2.2641, 2.2624, 2.2608, 2.2594, 2.2581, 2.257, 2.2559, 2.2549, 2.2539, 2.2531, 2.2523, 2.2515, 2.2508, 2.2502, 2.2496, 2.249, 2.2485, 2.248
}  // Curve 4
  };
}

void loop() {
  // Display "loading" message initially
  lcd.clear();
  lcd.print("Loading...");
  
   // Collect samples evenly for 1 hour
  unsigned long startTime = millis();
  unsigned long interval = 3600000 / numSamples;  // Calculate interval for evenly spaced samples
  for (int sampleIndex = 0; sampleIndex < numSamples; sampleIndex++) {
    unsigned long targetTime = startTime + interval * sampleIndex;
    while (millis() < targetTime) {  // Wait until the target time for this sample
      ;  // Do nothing, just wait
    }
    collectedData[sampleIndex] = analogRead(A0);
    voltage[sampleIndex] = (collectedData[sampleIndex] * 5.0) / 1023.0;
    Serial.println(voltage[sampleIndex]);  // Print the specific voltage value

  }
  // Clear the loading message and display results
  lcd.clear();

  // Find the curve with the highest correlation
  int highestCorrelationIndex = 0;
  float highestCorrelation = 0;
  for (int i = 0; i < numCurves; i++) {
    float correlation = calculateCorrelation(voltage, existingCurves[i]);
    if (correlation > highestCorrelation) {
      highestCorrelation = correlation;
      highestCorrelationIndex = i;
    }
    
  }
  
  // Display the name of the curve with the highest correlation
  lcd.clear();
  lcd.print("Highest Correlation:");
  lcd.setCursor(0, 1);
  lcd.print(curveNames[highestCorrelationIndex]);
  break;
}

// Function to calculate correlation coefficient (Pearson's correlation)
float calculateCorrelation(float data1[], float data2[]) {
  float mean1 = 0, mean2 = 0;
  for (int i = 0; i < numSamples; i++) {
    mean1 += data1[i];
    mean2 += data2[i];
  }
  mean1 /= numSamples;
  mean2 /= numSamples;

  float numerator = 0, denominator1 = 0, denominator2 = 0;
  for (int i = 0; i < numSamples; i++) {
    numerator += (data1[i] - mean1) * (data2[i] - mean2);
    denominator1 += (data1[i] - mean1) * (data1[i] - mean1);
    denominator2 += (data2[i] - mean2) * (data2[i] - mean2);
  }

  return numerator / sqrt(denominator1 * denominator2);
}

I get an error saying the break is not in a loop or switch. is a Void loop, not a loop?

PAO1:73:3: error: break statement not within loop or switch
break;
^~~~~
exit status 1
break statement not within loop or switch

there's nothing to break from. the code isn't in any loop.

loop() is a function repeatedly called from the Arduino main().

loop is just the name of the function.

What is is that you're expecting the break; to do? Exit the loop function? That will happen all by itself if you just take that statement out.

In the context of the error message a loop is one of:

for (initialize expression; test expression; update expression) {
    // body of for loop
}
initialize expression;
while (test_expression) {
    // body of the while loop
    update_expression;
}
initialize expression;
do {
    // body of do-while loop
    update_expression;
} while (test_expression);
2 Likes

The compiler is telling the truth. The break; is in the loop() function, not inside a while loop, a do/while loop or a switch case

What were you expecting/hoping that the break; would do ?

1 Like

I would say the compiler message is pretty ambiguous ..

I want to exit the void loop because it is stuck at " Loading". The Loading is overwriting the printing of lcd.print("Highest Correlation:");. How can I prevent from going back to diplay "Loading"?

Move

  // Display "loading" message initially
  lcd.clear();
  lcd.print("Loading...");

from the start of the loop() function to the end of the setup() function.

Or, if you simply want all that code to be run just once, move everything from loop() to the end of setup() and leave loop() empty. Like so:

#include <LiquidCrystal_I2C.h> // Use I2C library

const int address = 0x27; // Replace with your LCD's I2C address if different
LiquidCrystal_I2C lcd(address, 16, 2); // Define LCD dimensions

// Define the number of samples and curves
const int numSamples = 50;
const int numCurves = 4;

// Arrays to store collected data and existing curves
float collectedData[numSamples];
float existingCurves[numCurves][numSamples];
float voltage[numSamples];
// Names of the existing curves
const char* curveNames[] = {"low MIC", "CEF MIC", "CIP MIC", "GEN MIC"};

void setup() {
  Wire.begin(); // Initialize I2C communication
  lcd.init(); // Initialize the LCD
  lcd.backlight(); // Turn on the backlight (optional)
  Serial.begin(9600);
  // Initialize the existing curves (no need for loading here)
  float existingCurves[numCurves][numSamples] = {
    {8.5338, 7.7862, 3.8893, 2.0185, 1.1925, 0.7812, 0.55199, 0.41257, 0.32192, 0.25984, 0.21554, 0.18285, 0.15807, 0.13884, 0.12362, 0.11138, 0.10138, 0.093122, 0.08621, 0.080376, 0.075397, 0.071127, 0.067434, 0.064208, 0.061389, 0.058898, 0.056694, 0.054737, 0.052977, 0.051408, 0.049988, 0.048709, 0.04755, 0.046495, 0.04553, 0.044646, 0.043841, 0.043096, 0.042412, 0.041777, 0.04119, 0.040648, 0.040143, 0.03967, 0.039233, 0.038829, 0.038444, 0.038089, 0.03775, 0.03743
}, // Curve 1
    {8.534, 8.2735, 6.9479, 5.6951, 4.6544, 3.8372, 3.2085, 2.7273, 2.3588, 2.0768, 1.862, 1.7003, 1.5813, 1.4971, 1.4414, 1.4089, 1.3954, 1.3969, 1.4103, 1.4326, 1.4611, 1.4936, 1.5281, 1.5631, 1.5973, 1.6295, 1.6592, 1.6858, 1.7091, 1.7289, 1.7454, 1.7587, 1.7691, 1.7769, 1.7825, 1.7861, 1.7881, 1.7887, 1.7884, 1.7873, 1.7855, 1.7834, 1.781, 1.7784, 1.7757, 1.773, 1.7703, 1.7677, 1.7651, 1.7627
}, // Curve 2
    {8.3963, 8.3099, 7.784, 7.2455, 6.7251, 6.2413, 5.803, 5.4127, 5.0688, 4.7677, 4.5049, 4.2758, 4.0759, 3.9011, 3.748, 3.6135, 3.495, 3.3903, 3.2973, 3.2146, 3.1408, 3.0747, 3.0153, 2.9618, 2.9134, 2.8696, 2.8297, 2.7934, 2.7603, 2.7299, 2.7021, 2.6765, 2.6529, 2.6312, 2.611, 2.5924, 2.575, 2.5589, 2.5439, 2.5299, 2.5169, 2.5047, 2.4932, 2.4825, 2.4724, 2.463, 2.4539, 2.4454, 2.4368
}, // Curve 3
    {8.4942, 7.972, 5.7531, 4.3652, 3.6053, 3.1739, 2.9129, 2.7453, 2.6321, 2.5524, 2.4943, 2.4508, 2.4173, 2.391, 2.3701, 2.3531, 2.3392, 2.3276, 2.3178, 2.3095, 2.3025, 2.2964, 2.2911, 2.2865, 2.2824, 2.2788, 2.2757, 2.2728, 2.2703, 2.268, 2.2659, 2.2641, 2.2624, 2.2608, 2.2594, 2.2581, 2.257, 2.2559, 2.2549, 2.2539, 2.2531, 2.2523, 2.2515, 2.2508, 2.2502, 2.2496, 2.249, 2.2485, 2.248
}  // Curve 4
  };
  // Display "loading" message initially
  lcd.clear();
  lcd.print("Loading...");
  
   // Collect samples evenly for 1 hour
  unsigned long startTime = millis();
  unsigned long interval = 3600000 / numSamples;  // Calculate interval for evenly spaced samples
  for (int sampleIndex = 0; sampleIndex < numSamples; sampleIndex++) {
    unsigned long targetTime = startTime + interval * sampleIndex;
    while (millis() < targetTime) {  // Wait until the target time for this sample
      ;  // Do nothing, just wait
    }
    collectedData[sampleIndex] = analogRead(A0);
    voltage[sampleIndex] = (collectedData[sampleIndex] * 5.0) / 1023.0;
    Serial.println(voltage[sampleIndex]);  // Print the specific voltage value

  }
  // Clear the loading message and display results
  lcd.clear();

  // Find the curve with the highest correlation
  int highestCorrelationIndex = 0;
  float highestCorrelation = 0;
  for (int i = 0; i < numCurves; i++) {
    float correlation = calculateCorrelation(voltage, existingCurves[i]);
    if (correlation > highestCorrelation) {
      highestCorrelation = correlation;
      highestCorrelationIndex = i;
    }
    
  }
  
  // Display the name of the curve with the highest correlation
  lcd.clear();
  lcd.print("Highest Correlation:");
  lcd.setCursor(0, 1);
  lcd.print(curveNames[highestCorrelationIndex]);
}

void loop() {
}

// Function to calculate correlation coefficient (Pearson's correlation)
float calculateCorrelation(float data1[], float data2[]) {
  float mean1 = 0, mean2 = 0;
  for (int i = 0; i < numSamples; i++) {
    mean1 += data1[i];
    mean2 += data2[i];
  }
  mean1 /= numSamples;
  mean2 /= numSamples;

  float numerator = 0, denominator1 = 0, denominator2 = 0;
  for (int i = 0; i < numSamples; i++) {
    numerator += (data1[i] - mean1) * (data2[i] - mean2);
    denominator1 += (data1[i] - mean1) * (data1[i] - mean1);
    denominator2 += (data2[i] - mean2) * (data2[i] - mean2);
  }

  return numerator / sqrt(denominator1 * denominator2);
}
1 Like

break is not within a loop
i think you're confused because you think the loop() function is a type of loop

The loop() function will exit when it has executed all of the code within it or when it encounters a return statement

If the code is stuck at some point then you need to find out where and why it is happening

        unsigned long targetTime = startTime + interval * sampleIndex;
        while (millis() < targetTime)
        {      // Wait until the target time for this sample
            ;  // Do nothing, just wait
        }

How long have you waited for this while loop to end ? How long do you expect it to run for ?

By the way, you have a subtle bug in your code. You've declared existingCurves as both a global and local variable.

I need to collect voltage values for 1 hour, store them in an array, and compare them to other arrays. Therefore I thought I needed to put it in the void loop.

You are right. It Worked.
thx

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