Sensor acting weird - 100% code issue

Hello.

While using this code below i am getting strange results.

I blocked calculations of HCSNR sensor after it has passed out of range (0-200 cm).
When i cover up one "hole" e.q. reciever the sensor blocks himself and asks for restart.
But when i just click 2 or 3 for measuring temperature or humidity it does the thing and i am able to press 4 to continue measuring distance.
BUT i really need it to do the thing while i press 1 for menu.
What is so different when i press 2 or 3 and the sensor is avaliable again, but when i press 1 it is not???

"this code" ?

AWOL:
"this code" ?

No, "this one" :smiley:

Well i thought u guys will be able to take a hint :stuck_out_tongue:
I guess not :slight_smile:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <dht.h>
#define dht_apin A0 //výstup z DHT
#define trigPin 2 //výstup zo SNR
#define echoPin 3 //vstup do SNR
//#define BACKLIGHT_PIN 13
dht DHT;
float duration, distance;

//Nastavenie displeja
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const uint8_t charBitmap[][8] =
{
  { 0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0 },
  { 0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0 },
  { 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0, 0x0 },
  { 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0, 0x0 },
  { 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0x0 },
  { 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0x0 },
  { 0x0, 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0x0 },
  { 0x0, 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0x0 }
};

const byte ROWS = 4; //4 rady
const byte COLS = 4; //4 riadky
byte rowPins[ROWS] = {39, 41, 43, 45}; //zapojenie radov klávesnice do Arduina 
byte colPins[COLS] = {31, 33, 35, 37}; //zapojenie riadkov klávesnice do Arduina 
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'}, //definovanie znakov klávesnice
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {

  Serial.begin (9600);
  lcd.begin(16, 4);
  int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));
  for ( int i = 0; i < charBitmapSize; i++ )
  {
    lcd.createChar ( i, (uint8_t *)charBitmap[i] );
  }

  lcd.home ();
  lcd.print("Diplomova praca");
  lcd.setCursor (0, 1);
  lcd.print(" Radovan MATEJ");
  lcd.setCursor (0, 2);
  lcd.print("System OK");
  lcd.setCursor (0, 3);
  lcd.print("Stlac tlacitko");
  delay(500); // System reboot
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(dht_apin, INPUT);
  pinMode(A1, OUTPUT); // LED pin
  pinMode(A2, OUTPUT); // LED pin
  pinMode(8, OUTPUT); //pin pre napájanie SNR
  digitalWrite(8, HIGH);
  digitalWrite(A1, LOW);
  digitalWrite(A2, LOW);
  pinMode(A7, OUTPUT);
  digitalWrite(A7, HIGH);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

//LED SIGNAL
// digitalWrite(A1, HIGH); digitalWrite(A2, HIGH); delay(1000); digitalWrite(A1, LOW); digitalWrite(A2, LOW); delay(1000);
// digitalWrite(A1, HIGH); digitalWrite(A2, HIGH); delay(1000); digitalWrite(A1, LOW); digitalWrite(A2, LOW); delay(1000);
}

void loop()
{
  char key = keypad.getKey();
  if (key)
  {
//    Serial.println(key);
    switch (key)
    {
      case '1': // MENU
        lcd.clear();
        lcd.home();
        lcd.print("Menu        - 1");
        lcd.setCursor(0, 1);
        lcd.print("Teplota     - 2"); //TEMPERATURE
        lcd.setCursor(0, 2);
        lcd.print("Vlhkost     - 3"); //HUMIDITY
        lcd.setCursor(0, 3);
        lcd.print("Vzdialenost - 4"); //DISTANCE
        break;

      case '2': //Teplota
        DHT.read11(dht_apin);
        lcd.clear();
        lcd.home();
        lcd.print ("Teplota"); //TEMPERATURE
        lcd.setCursor(0, 1);
        lcd.print(DHT.temperature);
        lcd.setCursor(6, 1);
        lcd.print("C");
        lcd.setCursor(0, 3);
        lcd.print("Stlac 1 pre menu"); //PRESS 1 FOR MENU
        break;

      case '3': //HUMIDITY
        DHT.read11(dht_apin);
        lcd.clear();
        lcd.home();
        lcd.print("Vlhkost"); //HUMIDITY
        lcd.setCursor (0, 1);
        lcd.print(DHT.humidity+20);
        lcd.setCursor(6, 1);
        lcd.print("%");
        lcd.setCursor(0, 3);
        lcd.print("Stlac 1 pre menu");//PRESS 1 FOR MENU
        break;

      case '4': //RANGE
        lcd.clear();
        lcd.home();
        lcd.print("Vzdielenost"); //DISTANCE
        digitalWrite (echoPin, LOW);
        digitalWrite(trigPin, LOW);
        duration = 0; 
        distance = 0;
        delayMicroseconds(2);
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);
        duration = pulseIn(echoPin, HIGH);
        distance = (duration / 2) / 29.1;
        Serial.println(distance);
        Serial.println(duration);

        if (distance >= 200 || distance <= 0)
        {
          lcd.clear();
          lcd.home();
          lcd.print("Vzdialenost"); //DISTANCE
          lcd.setCursor(0, 1);
          lcd.print("Mimo rozsah"); //OUT OF RANGE
          lcd.setCursor(0, 2);
          lcd.print("Restartujte ..."); //RESTART
          lcd.setCursor(0, 3);
          lcd.print("Stlacte  '*' "); //PRESS *
        }
        else
        {
          lcd.setCursor(0, 1);
          lcd.print(distance, 2);
          lcd.setCursor(6, 1);
          lcd.print("cm");
          lcd.setCursor(0, 3);
          lcd.print("Stlac 1 pre menu"); //PRESS 1 FOR MENU
          delay(1);
        }
        break;

      case '*':
        lcd.home();
        lcd.clear();
        lcd.print ("Restartujem ...."); //RESTARTING
        lcd.setCursor(0, 1);
        lcd.print("Prosim cakajte ..."); // PLEASE WAIT
        digitalWrite(A7, LOW);
        delay (1000);
        digitalWrite(A7, HIGH);
        lcd.clear();
        lcd.home();
        lcd.print("Menu        - 1");
        lcd.setCursor(0, 1);
        lcd.print("Teplota     - 2"); // TEMPERATURE
        lcd.setCursor(0, 2);
        lcd.print("Vlhkost     - 3"); //HUMIDITY
        lcd.setCursor(0, 3);
        lcd.print("Vzdialenost - 4"); //DISTANCE
        break;
        
      default:
        break;
    }
  }
}

I translated what i think is needed in comments.

No ideas ? :o

I have an idea.

You describe what you expect to happen, what is actually happening, and how your expectations differ from your observations.

What is so different when i press 2 or 3 and the sensor is avaliable again, but when i press 1 it is not???

The most obvious difference is that case '1' does not read the sensor whereas cases '2' and '3' do