Trying to use 'switch cases' to operate 8 different outcomes but I can't get it to work

G'day, I need help to understand why my switch case part of the sketch doesn't work.
First is the whole sketch, the second is the entire part of the sketch that fails to work. I even tried using if, else if, else control statements instead of 'switch case' with the same fault. I am completely lost. Thanks in advance.

Using a Nano33BLE and Adafruit_VL53L0X laser measurment sensor.

#include <Arduino_LSM9DS1.h>


#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

const unsigned long StandingStillInterval = 5000;

const float MagnitudeThreshhold = 0.1;  // Determine by experiment

float x, y, z;
float oldX, oldY, oldZ;
long IsStill = false;  // board movement setting

static unsigned long timeLastMoved = 0;


float degreesX = 0;
float degreesY = 0;
float degreesZ = 0;

#define buzzerneg 12     //pin 12 buzzer negative GND 
#define buzzerpos A3     //A3 buzzer output


unsigned int LaserMeasurement = 0;

#define motorPin D6    //vibrator motor output
int SW1 = 0;



void setup()
{
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Started");

  if (!IMU.begin())
  {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println("Hz");

  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin())
  {
    Serial.println(F("Failed to boot VL53L0X"));
    while (1);
  }
  // power
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}
  
void loop()
{

  if (HasMoved())
  {
    timeLastMoved = millis();
    if (millis() - timeLastMoved < StandingStillInterval)
    {
      IsStill = false;
      Serial.print("Started moving again at ");
      Serial.println(timeLastMoved);
    }
  }

  if (millis() - timeLastMoved > StandingStillInterval)
  {
    if (!IsStill)
    {
      IsStill = true;
      Serial.print("Stopped moving at ");
      Serial.println(timeLastMoved);

      Serial.print("Has been still for 2 seconds at ");
      Serial.println(millis());

    }

  }


  // Move eyes whenever not still
  if (!IsStill)
  { Eyes();
  }
}
float square(const float f)
{
  return f * f;
}

boolean HasMoved()
{
  if (!IMU.accelerationAvailable())
    return false;  // Can't tell if it has moved because data isn't available

  IMU.readAcceleration(x, y, z);

  float magnitudeOfChange = square(x - oldX) + square(y - oldY) + square(z - oldZ);
  //{ Serial.print("magnitude of change: ");
  //Serial.println(magnitudeOfChange);
  //}
  oldX = x;
  oldY = y;
  oldZ = z;

  return magnitudeOfChange > MagnitudeThreshhold;      //measurement of board movement and comparing if greater than Magnitude Threshhold
}

/*==============================================
        Electronic eyes
===================================================*/
void Eyes()               
{
  if (HasMoved())
    ( IsStill == false);
  { Serial.println("  Started eyes scanning  ");
    Serial.println(timeLastMoved);
  }
  if (IMU.accelerationAvailable())

    IMU.readAcceleration( x, y, z );

  float fx, fy, fz;

/*==============================================
  //Angle Measurment code
===================================================*/
  
  {
    if (x > 0.1)          //activate angle measurement from the IMU if within set boundaries
    {
      fx = x * 100;
      degreesX = map(fx, 0, 97, 0, 90);   
      Serial.print("  Tilting up  ");
      Serial.print(degreesX);
      Serial.println("  degrees");
      delay(500);
    }

    if (x < -0.1)
    {
      fx = x * 100;
      degreesX = map(fx, 0, -100, 0, 90);
      Serial.print("Tilting down ");
      Serial.print(degreesX);
      Serial.println("  degrees");
      delay(500);
    }

    if (y > 0.1)
    {
      fy = y * 100;
      degreesY = map(fy, 0, 97, 0, 90);
      Serial.print("Tilting left ");
      Serial.print(degreesY);
      Serial.println("  degrees");
      delay(500);
    }

    if (y < -0.1)
    {
      fy = y * 100;
      degreesY = map(fy, 0, -100, 0, 90);
      Serial.print("Tilting right ");
      Serial.print(degreesY);
      Serial.println("  degrees");
      delay(500);
    }

    if (z > 0.1)
    {
      fz =  z * 100;
      degreesZ = map(fz, 0, 97, 0, 90);
      //   Serial.print("  Tilting up  ");
      //   Serial.print(degreesZ);
      //   Serial.println("  degrees");
      //   delay(500);
    }

    if (z < -0.1)
    {
      fz =  z * 100;
      degreesZ = map(fz, 0, -100, 0, 90);
      //    Serial.print("Tilting down ");
      //   Serial.print(degreesZ);
      //   Serial.println("  degrees");
      //   delay(500);
    }
  

/*==============================================
  VL53L0X  Laser measurement
===================================================*/
  

  VL53L0X_RangingMeasurementData_t measure;

  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout


  /*==============================================
        Print Laser measurment to Serial Monitor
    ===================================================*/

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter); 
    LaserMeasurement = measure.RangeMilliMeter;
  } else {
    Serial.println(" out of range ");
  }

  //delay(100);



  /*==========================================
        Control Buzzer and Vibrator Motor
   ================ ==================================*/

//if (IsStill == false)
{
 { Serial.print("  Moving and now measuring distance  ");

  digitalWrite(12, LOW);   //Buzzer GND is always low
 
  if (measure.RangeMilliMeter > 150 && measure.RangeMilliMeter < 250)   //these statements activate when true the related switch, 
  {SW1 = 1;} 
  else if (measure.RangeMilliMeter > 260 && measure.RangeMilliMeter < 350)   
  {SW1 = 2;}
  else if (measure.RangeMilliMeter > 360 && measure.RangeMilliMeter < 450)
  {SW1 = 3;}
  else if (measure.RangeMilliMeter > 460 && measure.RangeMilliMeter < 550)
  {SW1 = 4;}
  else if (measure.RangeMilliMeter > 560 && measure.RangeMilliMeter < 650)
  {SW1 = 5;} 
  else if (measure.RangeMilliMeter > 660 && measure.RangeMilliMeter < 850)
  {SW1 = 6;}
  else if (measure.RangeMilliMeter > 860 && measure.RangeMilliMeter < 1000)
  {SW1 = 7;}
  else {SW1 = 0;}
   //}
   switch (SW1){     
  case '1':                         
                        Serial.print(" Case 1 ");
                          analogWrite(A3,255);
                          tone(A3, 40000, 100); // tone on pin 8 at 40000 Hz for 0.1 second                                              
                          analogWrite(A3,0);
                          delay(100);     //sound buzzer every 0.1 second if obstacle distance is between 20-30cm.
                           digitalWrite(motorPin, HIGH);  //vibrator buzzer on
                            delay (100);
                            digitalWrite(motorPin, LOW);   //vibrator buzzer off                        
                           delay(100);
                          break;
                        

   
     
 case '2': 
                           Serial.print(" Case 2 ");
                            analogWrite(A3,255);
                            tone(A3, 35000, 100); // tone on pin 8 at 35000 Hz for 0.1 second
                            analogWrite(A3,0);
                            delay(200);   //sound buzzer every 0.2 seconds if obstacle distance is between 10-20cm.
                            digitalWrite(motorPin, HIGH);  
                            delay (100);
                            digitalWrite(motorPin, LOW);
                            delay(100);
                            break;
                          

                           

     
 case '3': 
                           Serial.print(" Case 3 ");
                            analogWrite(A3,255);
                            tone(A3, 30000, 200); // tone on pin 8 at 30000 Hz for 0.2 second
                            analogWrite(A3,0);
                            delay(300);    //sound buzzer every 0.3 seconds if obstacle distance is between 10-20cm.                                                         
                            digitalWrite(motorPin, HIGH);
                            delay (200);
                            digitalWrite(motorPin, LOW);
                             delay(100);
                             break;
                           

                           
    
    case '4': 
                           Serial.print(" Case 4 ");  
                           analogWrite(A3,255);
                           tone(A3, 25000, 300); // tone on pin 8 at 25000 Hz for 0.3 second
                           analogWrite(A3,0);
                           delay(300);     //sound buzzer every 0.3 seconds if obstacle distance is between 0-10cm.                            
                            digitalWrite(motorPin, HIGH);
                            delay (300);
                            digitalWrite(motorPin, LOW);
                            delay(200);
                            break;
                           

                          
     
   case '5':    
                          Serial.print(" Case 5 ");
                           analogWrite(A3,255);
                           tone(A3, 20000, 400); // tone on pin 8 at 20000 Hz for 0.4 second
                           analogWrite(A3,0);
                           delay(300);     //sound buzzer every 0.3 seconds if obstacle distance is between 0-10cm.                            
                            digitalWrite(motorPin, HIGH);
                            delay (400);
                            digitalWrite(motorPin, LOW);
                            delay(300);
                           break;
                           

                          
     
  case '6':   
                         Serial.print(" Case 6 ");
                            analogWrite(A3,255);
                           tone(A3, 15000, 500); // tone on pin 8 at 15000 Hz for 0.5 second
                            analogWrite(A3,0);                                                    
                           delay(400);   //sound buzzer every 0.4 seconds if obstacle distance is between 0-10cm.                                                    
                            digitalWrite(motorPin, HIGH);
                            delay (500);
                            digitalWrite(motorPin, LOW);
                            delay(400);
                           break;
                           

                          
 
     
  case '7': 
   
                           Serial.print(" Case 7 ");
                           analogWrite(A3,255);
                           tone(A3, 10000, 500); // tone on pin 8 at 10000 Hz for 0.5 second
                           analogWrite(A3,0);
                           delay(700);     //sound buzzer every 0.7 seconds if obstacle distance is between 0-10cm.                                                   
                            digitalWrite(motorPin, HIGH);
                            delay (500);
                            digitalWrite(motorPin, LOW);
                            delay(500);
                           break;
                                                     
  case '0':                 analogWrite(A3,0); //do not sound the buzzer    
                          break;
                          
  default:                               
                          analogWrite(A3,0); //do not sound the buzzer                          
                        break;
           
            }
      }
 
}

}}

Nothing works in this part of the sketch below, but all of the sketch above it works as it should.


  /*==========================================
        Control Buzzer and Vibrator Motor
   ================ ==================================*/

//if (IsStill == false)
{
 { Serial.print("  Moving and now measuring distance  ");

  digitalWrite(12, LOW);   //Buzzer GND is always low
 
  if (measure.RangeMilliMeter > 150 && measure.RangeMilliMeter < 250)   //these statements activate when true the related switch, 
  {SW1 = 1;} 
  else if (measure.RangeMilliMeter > 260 && measure.RangeMilliMeter < 350)   
  {SW1 = 2;}
  else if (measure.RangeMilliMeter > 360 && measure.RangeMilliMeter < 450)
  {SW1 = 3;}
  else if (measure.RangeMilliMeter > 460 && measure.RangeMilliMeter < 550)
  {SW1 = 4;}
  else if (measure.RangeMilliMeter > 560 && measure.RangeMilliMeter < 650)
  {SW1 = 5;} 
  else if (measure.RangeMilliMeter > 660 && measure.RangeMilliMeter < 850)
  {SW1 = 6;}
  else if (measure.RangeMilliMeter > 860 && measure.RangeMilliMeter < 1000)
  {SW1 = 7;}
  else {SW1 = 0;}
   //}
   switch (SW1){     
  case '1':                         
                        Serial.print(" Case 1 ");
                          analogWrite(A3,255);
                          tone(A3, 40000, 100); // tone on pin 8 at 40000 Hz for 0.1 second                                              
                          analogWrite(A3,0);
                          delay(100);     //sound buzzer every 0.1 second if obstacle distance is between 20-30cm.
                           digitalWrite(motorPin, HIGH);  //vibrator buzzer on
                            delay (100);
                            digitalWrite(motorPin, LOW);   //vibrator buzzer off                        
                           delay(100);
                          break;
                        

   
     
 case '2': 
                           Serial.print(" Case 2 ");
                            analogWrite(A3,255);
                            tone(A3, 35000, 100); // tone on pin 8 at 35000 Hz for 0.1 second
                            analogWrite(A3,0);
                            delay(200);   //sound buzzer every 0.2 seconds if obstacle distance is between 10-20cm.
                            digitalWrite(motorPin, HIGH);  
                            delay (100);
                            digitalWrite(motorPin, LOW);
                            delay(100);
                            break;
                          

                           

     
 case '3': 
                           Serial.print(" Case 3 ");
                            analogWrite(A3,255);
                            tone(A3, 30000, 200); // tone on pin 8 at 30000 Hz for 0.2 second
                            analogWrite(A3,0);
                            delay(300);    //sound buzzer every 0.3 seconds if obstacle distance is between 10-20cm.                                                         
                            digitalWrite(motorPin, HIGH);
                            delay (200);
                            digitalWrite(motorPin, LOW);
                             delay(100);
                             break;
                           

                           
    
    case '4': 
                           Serial.print(" Case 4 ");  
                           analogWrite(A3,255);
                           tone(A3, 25000, 300); // tone on pin 8 at 25000 Hz for 0.3 second
                           analogWrite(A3,0);
                           delay(300);     //sound buzzer every 0.3 seconds if obstacle distance is between 0-10cm.                            
                            digitalWrite(motorPin, HIGH);
                            delay (300);
                            digitalWrite(motorPin, LOW);
                            delay(200);
                            break;
                           

                          
     
   case '5':    
                          Serial.print(" Case 5 ");
                           analogWrite(A3,255);
                           tone(A3, 20000, 400); // tone on pin 8 at 20000 Hz for 0.4 second
                           analogWrite(A3,0);
                           delay(300);     //sound buzzer every 0.3 seconds if obstacle distance is between 0-10cm.                            
                            digitalWrite(motorPin, HIGH);
                            delay (400);
                            digitalWrite(motorPin, LOW);
                            delay(300);
                           break;
                           

                          
     
  case '6':   
                         Serial.print(" Case 6 ");
                            analogWrite(A3,255);
                           tone(A3, 15000, 500); // tone on pin 8 at 15000 Hz for 0.5 second
                            analogWrite(A3,0);                                                    
                           delay(400);   //sound buzzer every 0.4 seconds if obstacle distance is between 0-10cm.                                                    
                            digitalWrite(motorPin, HIGH);
                            delay (500);
                            digitalWrite(motorPin, LOW);
                            delay(400);
                           break;
                           

                          
 
     
  case '7': 
   
                           Serial.print(" Case 7 ");
                           analogWrite(A3,255);
                           tone(A3, 10000, 500); // tone on pin 8 at 10000 Hz for 0.5 second
                           analogWrite(A3,0);
                           delay(700);     //sound buzzer every 0.7 seconds if obstacle distance is between 0-10cm.                                                   
                            digitalWrite(motorPin, HIGH);
                            delay (500);
                            digitalWrite(motorPin, LOW);
                            delay(500);
                           break;
                                                     
  case '0':                 analogWrite(A3,0); //do not sound the buzzer    
                          break;
                          
  default:                               
                          analogWrite(A3,0); //do not sound the buzzer                          
                        break;
           
            }
      }
 
}

}}
![Screenshot (115)|690x387](upload://zcDz92uTyD8WXbhinN6tJfbp8F7.png)

Do not use single quotes around your "case" values; that will change the argument from an integer (which you were using in the if/else chain) to a character constant, which will have a different value...

Thank you for including your code correctly! It's indented "funny"; consider using cmd/ctrl-T for format it in a standard way.

if, else if, else statement setup which failed the same way:

#include <Arduino_LSM9DS1.h>


#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

const unsigned long StandingStillInterval = 5000;

const float MagnitudeThreshhold = 0.1;  // Determine by experiment

float x, y, z;
float oldX, oldY, oldZ;
long IsStill = false;  // board movement setting

static unsigned long timeLastMoved = 0;


float degreesX = 0;
float degreesY = 0;
float degreesZ = 0;

#define buzzerneg 12     //pin 12 buzzer negative GND 
#define buzzerpos A3     //A3 buzzer output


unsigned int LaserMeasurement = 0;

#define motorPin D6    //vibrator motor output
int SW1 = 0;



void setup()
{
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Started");

  if (!IMU.begin())
  {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println("Hz");

  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin())
  {
    Serial.println(F("Failed to boot VL53L0X"));
    while (1);
  }
  // power
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}
  
void loop()
{

  if (HasMoved())
  {
    timeLastMoved = millis();
    if (millis() - timeLastMoved < StandingStillInterval)
    {
      IsStill = false;
      Serial.print("Started moving again at ");
      Serial.println(timeLastMoved);
    }
  }

  if (millis() - timeLastMoved > StandingStillInterval)
  {
    if (!IsStill)
    {
      IsStill = true;
      Serial.print("Stopped moving at ");
      Serial.println(timeLastMoved);

      Serial.print("Has been still for 2 seconds at ");
      Serial.println(millis());

    }

  }


  // Move eyes whenever not still
  if (!IsStill)
  { Eyes();
  }
}
float square(const float f)
{
  return f * f;
}

boolean HasMoved()
{
  if (!IMU.accelerationAvailable())
    return false;  // Can't tell if it has moved because data isn't available

  IMU.readAcceleration(x, y, z);

  float magnitudeOfChange = square(x - oldX) + square(y - oldY) + square(z - oldZ);
  //{ Serial.print("magnitude of change: ");
  //Serial.println(magnitudeOfChange);
  //}
  oldX = x;
  oldY = y;
  oldZ = z;

  return magnitudeOfChange > MagnitudeThreshhold;      //measurement of board movement and comparing if greater than Magnitude Threshhold
}

/*==============================================
        Electronic eyes
===================================================*/
void Eyes()               
{
  if (HasMoved())
    ( IsStill == false);
  { Serial.println("  Started eyes scanning  ");
    Serial.println(timeLastMoved);
  }
  if (IMU.accelerationAvailable())

    IMU.readAcceleration( x, y, z );

  float fx, fy, fz;

/*==============================================
  //Angle Measurment code
===================================================*/
  
  {
    if (x > 0.1)          //activate angle measurement if within set boundaries
    {
      fx = x * 100;
      degreesX = map(fx, 0, 97, 0, 90);   
      Serial.print("  Tilting up  ");
      Serial.print(degreesX);
      Serial.println("  degrees");
      delay(500);
    }

    if (x < -0.1)
    {
      fx = x * 100;
      degreesX = map(fx, 0, -100, 0, 90);
      Serial.print("Tilting down ");
      Serial.print(degreesX);
      Serial.println("  degrees");
      delay(500);
    }

    if (y > 0.1)
    {
      fy = y * 100;
      degreesY = map(fy, 0, 97, 0, 90);
      Serial.print("Tilting left ");
      Serial.print(degreesY);
      Serial.println("  degrees");
      delay(500);
    }

    if (y < -0.1)
    {
      fy = y * 100;
      degreesY = map(fy, 0, -100, 0, 90);
      Serial.print("Tilting right ");
      Serial.print(degreesY);
      Serial.println("  degrees");
      delay(500);
    }

    if (z > 0.1)
    {
      fz =  z * 100;
      degreesZ = map(fz, 0, 97, 0, 90);
      //   Serial.print("  Tilting up  ");
      //   Serial.print(degreesZ);
      //   Serial.println("  degrees");
      //   delay(500);
    }

    if (z < -0.1)
    {
      fz =  z * 100;
      degreesZ = map(fz, 0, -100, 0, 90);
      //    Serial.print("Tilting down ");
      //   Serial.print(degreesZ);
      //   Serial.println("  degrees");
      //   delay(500);
    }
  

/*==============================================
  VL53L0X  Laser measurement
===================================================*/
  

  VL53L0X_RangingMeasurementData_t measure;

  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout


  /*==============================================
        Print Laser measurment to Serial Monitor
    ===================================================*/

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter); 
    LaserMeasurement = measure.RangeMilliMeter;
  } else {
    Serial.println(" out of range ");
  }

  //delay(100);



  /*==========================================
        Control Buzzer and Vibrator Motor
   ================ ==================================*/

//if (IsStill == false)
{
 { Serial.print("  Moving and now measuring distance  ");

  digitalWrite(12, LOW);   //Buzzer GND is always low
 
  if (measure.RangeMilliMeter > 150 && measure.RangeMilliMeter < 250)   //these statements activate when true the related switch, 
  {SW1 = 1;} 
  else if (measure.RangeMilliMeter > 260 && measure.RangeMilliMeter < 350)   
  {SW1 = 2;}
  else if (measure.RangeMilliMeter > 360 && measure.RangeMilliMeter < 450)
  {SW1 = 3;}
  else if (measure.RangeMilliMeter > 460 && measure.RangeMilliMeter < 550)
  {SW1 = 4;}
  else if (measure.RangeMilliMeter > 560 && measure.RangeMilliMeter < 650)
  {SW1 = 5;} 
  else if (measure.RangeMilliMeter > 660 && measure.RangeMilliMeter < 850)
  {SW1 = 6;}
  else if (measure.RangeMilliMeter > 860 && measure.RangeMilliMeter < 1000)
  {SW1 = 7;}
  else {SW1 = 0;}
   //}
   switch (SW1){     
  case '1':                         
                        Serial.print(" Case 1 ");
                          analogWrite(A3,255);
                          tone(A3, 40000, 100); // tone on pin 8 at 40000 Hz for 0.1 second                                              
                          analogWrite(A3,0);
                          delay(100);     //sound buzzer every second if obstacle distance is between 20-30cm.
                           digitalWrite(motorPin, HIGH);  //vibrator buzzer on
                            delay (100);
                            digitalWrite(motorPin, LOW);   //vibrator buzzer off                        
                           delay(100);
                          break;
                        

   
     
 case '2': 
                           Serial.print(" Case 2 ");
                            analogWrite(A3,255);
                            tone(A3, 35000, 100); // tone on pin 8 at 35000 Hz for 0.1 second
                            analogWrite(A3,0);
                            delay(200);   //sound buzzer every 0.5 seconds if obstacle distance is between 10-20cm.
                            digitalWrite(motorPin, HIGH);  
                            delay (100);
                            digitalWrite(motorPin, LOW);
                            delay(100);
                            break;
                          

                           

     
 case '3': 
                           Serial.print(" Case 3 ");
                            analogWrite(A3,255);
                            tone(A3, 30000, 200); // tone on pin 8 at 30000 Hz for 0.2 second
                            analogWrite(A3,0);
                            delay(300);    //sound buzzer every 0.5 seconds if obstacle distance is between 10-20cm.                                                         
                            digitalWrite(motorPin, HIGH);
                            delay (200);
                            digitalWrite(motorPin, LOW);
                             delay(100);
                             break;
                           

                           
    
    case '4': 
                           Serial.print(" Case 4 ");  
                           analogWrite(A3,255);
                           tone(A3, 25000, 300); // tone on pin 8 at 25000 Hz for 0.3 second
                           analogWrite(A3,0);
                           delay(300);     //sound buzzer every 0.1 seconds if obstacle distance is between 0-10cm.                            
                            digitalWrite(motorPin, HIGH);
                            delay (300);
                            digitalWrite(motorPin, LOW);
                            delay(200);
                            break;
                           

                          
     
   case '5':    
                          Serial.print(" Case 5 ");
                           analogWrite(A3,255);
                           tone(A3, 20000, 400); // tone on pin 8 at 20000 Hz for 0.4 second
                           analogWrite(A3,0);
                           delay(300);     //sound buzzer every 0.1 seconds if obstacle distance is between 0-10cm.                            
                            digitalWrite(motorPin, HIGH);
                            delay (400);
                            digitalWrite(motorPin, LOW);
                            delay(300);
                           break;
                           

                          
     
  case '6':   
                         Serial.print(" Case 6 ");
                            analogWrite(A3,255);
                           tone(A3, 15000, 500); // tone on pin 8 at 15000 Hz for 0.5 second
                            analogWrite(A3,0);                                                    
                           delay(400);   //sound buzzer every 0.1 seconds if obstacle distance is between 0-10cm.                                                    
                            digitalWrite(motorPin, HIGH);
                            delay (500);
                            digitalWrite(motorPin, LOW);
                            delay(400);
                           break;
                           

                          
 
     
  case '7': 
   
                           Serial.print(" Case 7 ");
                           analogWrite(A3,255);
                           tone(A3, 10000, 500); // tone on pin 8 at 10000 Hz for 0.5 second
                           analogWrite(A3,0);
                           delay(700);     //sound buzzer every 0.1 seconds if obstacle distance is between 0-10cm.                                                   
                            digitalWrite(motorPin, HIGH);
                            delay (500);
                            digitalWrite(motorPin, LOW);
                            delay(500);
                           break;
                                                     
  case '0':                 analogWrite(A3,0); //do not sound the buzzer    
                          break;
                          
  default:                               
                          analogWrite(A3,0); //do not sound the buzzer                          
                        break;
           
            }
      }
 
}

}}

Thanks for the prompt response, I have been trying for weeks to solve the issue but need help. I set it up as I found in an online example which may of been incorrect. I am still learning. I will give hat a go now. Cheers

also excuse layout, I haven't cleaned the code yet and my bad, it has got quite a messy outlay because of he constant changes trying to fix the issue :upside_down_face:

Shanon, so you mean:

case "1":

instead of

case '1':

is that correct?

This is one example I worked off: Switch case example

Definitely not; you can't use c-strings in a case in C/C++.

sorry not that familiar with c at all, please explain.

No

case 1:

Thanks, there is definitely some errors in some online examples.

Double quotes indicate a string and they can't be used in a case.

What @westfw is pointing at (I think)

        if (measure.RangeMilliMeter > 150 && measure.RangeMilliMeter < 250)   //these statements activate when true the related switch,
        {
          SW1 = 1;
        }

SW1 is a number.

and

        switch (SW1) {
          case '1':

You treat SW1 as a character

If you change all those cases to numbers as shown below for case 1, you might get it right.

        switch (SW1) {
          case 1:

@cattledog was faster :wink:

:+1: :beers: :beers: :beers: I'll try now and post result. Thanks

Out of interest, why did the if, else if, else, setup fail to work. Another part of my sketch uses this and works - angular measurement, yet in the Buzzer Motor control it fails to work?

Just to clarify, '1' will give you the ASCII value for the number 1, which is 48 49, not the numeric value of 1.

will that cause an issue in the switch case?

Yes, because
case '1' :
is the same as
case 48 49:
which will never be true in your code.

Likewise, '2' = 49 50, '3' = 50 51, etc, so none of your cases will ever be true.

so how do I rectify this, do I use a, b, c.....instead of 1,2,3...?

The solution has already been shown in previous replies, just use the number without the quotes.

case 1:

instead of

case '1':

OK, sorry I thought you meant that was an issue. Appreciate your help. After weeks of trying to solve the issue on my own has left me nearly bald :joy:

I was just trying to explain why using the quotes was causing a problem, from some of your previous replies you did not seem to understand.