problems with ultrasonic sensor controlling switch case

hello,

i have got some problems with my Robot Project

Hardware:
Arduino Nano,
US Sensor x1,
IR Sensor x1,
L298N x2 ( each per motor)
HC-05 BT Modul,

1st problem:
in serial monitor and in android bluetooth app it shows the measured distance, but to fast, i want only 1-4 times the result in a second.
2nd:
i don't get it working to conntroll my switch case with the us sensor.

if the distance is <10 i want to "stop" the motors and change direction to "left" or "right" but only a short time that the robot only makes 45-120 Degree ( depending on the speed)
and after changing direction it should drive forwards again.

here is my code

//---------------L298N Motoren----------------------------
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 5;
int in1 = 7;
int in2 = 8;
// motor two
int enB = 6;
int in3 = 9;
int in4 = 10;
int motorspeed;
int i = 56; //bei 0 -55 nur fiepsen
int HindernisIR1 = 0;
int HindernisIR2 = 0;
int HindernisIR3 = 0;
int HindernisUS = 0;
int switchvar;
//---------------L298N Motoren----------------------------

//---------------IR Sensor-----------------------------------------
int SensorAnalog = A1; // Das Sensormodul wird mit dem analogen Ausgang an Pin A1 des Arduino angeschlossen
int Wert1 = 0; // Erstellen einer Variablen mit dem Namen „Wert1“, unter dem die Messwerte gespeichert werden.
//---------------IR Sensor-----------------------------------------

//---------------US Sensor-----------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
//---------------US Sensor-----------------------------------------

//---------------BT Modul------------------------------------------
char blueToothVal; //Werte sollen per Bluetooth gesendet werden
char lastValue; //speichert den letzten Status der LED (on/off)
int inByte = 0;
//---------------BT Modul------------------------------------------

// ------------------- millis statt delay------------------------
unsigned long currentMillis;
const unsigned int wartezeit = 125;  // in Millisekunden Zeit vergleichen
unsigned long previousTimeMillis = 0x0FFFFFFF;  // damit sofort die Zeit verglichen wird
//---------------------millis statt delay------------------------

void setup() {
  Serial.begin(9600); //serieller Monitor wird gestartet, Baudrate auf 9600 festgelegt
  pinMode(13, OUTPUT); //PIN 13 wird als Ausgang festgelegt
  // Motoren
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);}
  // Motoren


void loop() {
  
//---------------US Sensor-----------------------------------------
unsigned int uS = sonar.ping();
  float distance_cm = uS/57.0;    // The constant "US_ROUNDTRIP_CM" = 57
  currentMillis = millis();
  if (currentMillis - previousTimeMillis >= 250 )//&& Wert1 > 30&& distance >=10)
  {Serial.print("Ping: ");
    Serial.print(distance_cm);
    Serial.println("cm");}
  
    
  //---------------US Sensor-----------------------------------------

  //---------------IR Sensor-----------------------------------------
  Wert1 = analogRead(SensorAnalog); // Der analoge Wert an Pin A1 wird ausgelesen
    if (Wert1<=100)  // …wenn der Wert „Wert1“ kleiner oder gleich 50 ist…
//{digitalWrite(13,HIGH); // …geht die LED an…}
  //---------------IR Sensor-----------------------------------------

  //---------------Motor1-----------------------------------------
  // int i=56// bei 0 -55 nur fiepsen
  motorspeed = i;
  
  if (Serial.available() > 0) {
    inByte = Serial.read();
    switch (inByte) {

      case '0':
        digitalWrite(13, LOW); //...soll die LED nicht leuchten
        break;

      case '1':
        digitalWrite(13, HIGH); //...soll die LED leuchten
        break;

      case '2'://  Richtung Vorwärts, beide Motoren
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        //analogWrite(enA, motorspeed);
        //analogWrite(enB, motorspeed);
        break;

      case '3':// Richtung Rückwärts, beide Motoren
        digitalWrite(in2, LOW);
        digitalWrite(in1, HIGH);
        digitalWrite(in4, LOW);
        digitalWrite(in3, HIGH);
        //analogWrite(enA, motorspeed);
        //analogWrite(enB, motorspeed);
        break;

      //analogWrite(enB, i);
      //Serial.println(motorspeed);
      //delay(100);

      case '4':// Richtung Links
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in4, LOW);
        digitalWrite(in3, HIGH);
        //analogWrite(enA, motorspeed);
        //analogWrite(enB, motorspeed);
        break;

      case '5':// Richtung Rechts
        digitalWrite(in2, LOW);
        digitalWrite(in1, HIGH);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        //analogWrite(enA, motorspeed);
        //analogWrite(enB, motorspeed);
        break;

      case '6':// STOPP
        digitalWrite(in2, LOW);
        digitalWrite(in1, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
        break;

      case '7':// Geschwindigkeit LOW
        analogWrite(enA, 64);
        analogWrite(enB, 64);
        break;

      case '8':// Geschwindigkeit MEDIUM
        analogWrite(enA, 128);
        analogWrite(enB, 128);;
        break;

      case '9':// Geschwindigkeit FAST
        analogWrite(enA, 255);
        analogWrite(enB, 255);
        break;

    }
  }
  //---------------Motor1-----------------------------------------

  //---------------------millis statt delay------------------------
  //currentMillis = millis();
  //if (currentMillis - previousTimeMillis >= 1000 )//&& Wert1 > 30&& distance >=10)
  //{
   // delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    //Serial.println("IR:   ");
    //Serial.println(Wert1); // IR Sensor Wert
    //Serial.print("/");
    //Serial.print(distance); // Send ping, get distance in cm and print result (0 = outside set distance range)
    // Serial.print("cm");
    //Serial.print (motorspeed);
    //Serial.println ("/256");
   // previousTimeMillis = currentMillis;
    //if (i < 246) { // ab 256 Motor Stillstand da PWM zu hoch
    //  i = i + 10;
    //  // i++;
    // }

  //}
  //---------------------millis statt delay------------------------
  //if (Serial.available()) //wenn Daten empfangen werden...
  //{
  //  blueToothVal = Serial.read(); //..sollen diese ausgelesen werden
  //}
  //if (blueToothVal == '1') //wenn das Bluetooth Modul eine „1“ empfängt..
  //{
  // digitalWrite(13, HIGH); //...soll die LED leuchten

  //}
  //else if (blueToothVal == '0') //wenn das Bluetooth Modul „0“ empfängt...
  //{
  //  digitalWrite(13, LOW); //..soll die LED nicht leuchten
  //  if (lastValue != '0') //wenn der letzte empfangene Wert keine „0“ war...
  //    Serial.println(F("LED is off")); //..soll auf dem seriellen Monitor „LED is off“ angezeigt werden
  //  lastValue = blueToothVal;
  //}
  }

thank you for your time

You should write a few functions that you would call whenever you need them : forward(), left(), right(), stop(), highSpeed(), medSpeed(), etc. For example:

void forward() {
       digitalWrite(in1, LOW);
       digitalWrite(in2, HIGH);
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
}

This would help you writing your code in the way you want it.

if the distance is <10 i want to "stop" the motors and change direction to "left" or "right" but only a short time that the robot only makes 45-120 Degree ( depending on the speed)
and after changing direction it should drive forwards again.

This would translate to something like:

if (distance_cm < 10) {
    stop();
    left();
    delay(100); // to be defined more precisely
    forward();
}

Nowhere in your code to you assign to the value previousTimeMillis so this code

  if (currentMillis - previousTimeMillis >= 250 )//&& Wert1 > 30&& distance >=10)
  {Serial.print("Ping: ");
    Serial.print(distance_cm);
    Serial.println("cm");}

will execute every time through loop() once currentMillis is greater than 250

You do have the assignment further down your code by Motor1, but it is commented out.