Serial input and output simultaneously - error

I'm working on a greenhouse automation project.
I have two sensors reading values from the Arduino pins and sending serial values over to the rpi for processing. Then I have values coming from the RPI for manual overrides such as manual turn off of lamp, pump, etc.

I've gotten it working but:
when I open the serial monitor it prints normal values.
Then, when I send a value from the monitor or raspberry pi to it, it produces an error when reading the sensor and doesn't work until reboot(keeps printing errors.)
What could be the issue?

Arduino Uno, DHT22 sensor.

#include <idDHTLib.h>

idDHTLib DHTLib(2, idDHTLib::DHT22);
unsigned long previousMillis = 0;
const long interval = 10000; 
void setup() {

  pinMode(6, INPUT_PULLUP);
  Serial.begin(115200);
  pinMode(6,OUTPUT);
}

void loop() {

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval && Serial.available() == 0) {
  previousMillis = currentMillis;
  int result = DHTLib.acquireAndWait();
  int soilmoisture = analogRead(0);
  if (result == IDDHTLIB_OK) {
    Serial.print(DHTLib.getCelsius());
    Serial.print("\t");
    Serial.print(DHTLib.getHumidity());
    Serial.print("\t");
    Serial.print(soilmoisture);
    Serial.println("");
    Serial.flush();
  } else {
    Serial.println(result); 
    Serial.flush();
  }
  
}
if (Serial.available() > 0 ) {
    char kar = Serial.read();
    switch(kar){
      case 'H':
        digitalWrite(6,HIGH);
        break;
      case 'L':
        digitalWrite(6,LOW);
        break;
    }

}



}

So you have the Rpi and serial monitor, both, connected to Serial? Connecting more than one device to a serial port can cause problems. What Arduino board are you using?

Arduino uno. For debugging, I'm connecting only the arduino, so it's a 1 - 1 connection.

What is the error?

What is connected to pin 6?

The method DHTLib.acquireAndWait() produces an error, presumably it has something to do with the sensor.

On pin 6 there's a relay.

I've investigated a bit more, seems like the only time the issue happens is when send something to the Arduino with the pi.

Please make a simple pencil drawing showing how you have everything connected and post a photo of your drawing. See this Simple Image Posting Guide

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.

Relay <---- (pin D6) Arduino (USB) <----->(USB) RPI
|
|
temp/moisture sensor(D2)
water sensor(A0)

The connections in Reply #6 are difficult to reconcile with this from the Original Post

when I open the serial monitor it prints normal values.
Then, when I send a value from the monitor or raspberry pi to it,

Are you running the Serial Monitor on the RPi?

You need to provide a drawing of all the connections to the relay, as well as a link to the relay datasheet.

My wild guess is that the relay, or the load it is controlling, is pulling down the voltage for the Arduino.

...R