[solved] the wrong led blink!?

hello, i am new to arduino and i have a strange behaviour.
i can not explain myself.

i have a simple buildup.
a humidity sensor and 4 colored leds.
i made an alert flag and then the red led should blink.
that seems running all fine (blue was on for wet) until some minutes but then
i came back to the room i saw (only) the blue led was blinking!?

// Rot alarm
// Gelb dry
// Grün ok
// Blau wet
#include <dht_nonblocking.h>

#define DHT_SENSOR_TYPE DHT_TYPE_11
//#define DHT_SENSOR_TYPE DHT_TYPE_21
//#define DHT_SENSOR_TYPE DHT_TYPE_22

static const int DHT_SENSOR_PIN = 7;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

static const int BUTTON1_PIN = 2;

static const int LED_GREEN_PIN = 3;
static const int LED_RED_PIN = 8;
static const int LED_YELLOW_PIN = 9;
static const int LED_BLUE_PIN = 10;

const long interval = 1000;
unsigned long previousMillis = 0;
bool alarm = false;
bool onoff = false;

void setup( )
{
Serial.begin( 9600);

pinMode(BUTTON1_PIN,INPUT_PULLUP);

pinMode(LED_GREEN_PIN,OUTPUT);
pinMode(LED_RED_PIN,OUTPUT);
pinMode(LED_YELLOW_PIN,OUTPUT);
pinMode(LED_BLUE_PIN,OUTPUT);

}
void loop( )
{
float temperature;
float humidity;

if(dht_sensor.measure(&temperature, &humidity)){
//--------------------------------- Serial Monitor
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
//--------------------------------- Plotter
//Serial.print( temperature, 1 );
//Serial.print( "\t" );
//Serial.println( humidity, 1 );

}

if (humidity > 65){ digitalWrite(LED_BLUE_PIN,HIGH);} else {digitalWrite(LED_BLUE_PIN,LOW);}
if (humidity >= 35 and humidity <= 65){ digitalWrite(LED_GREEN_PIN,HIGH);} else {digitalWrite(LED_GREEN_PIN,LOW);}
if (humidity < 35){ digitalWrite(LED_YELLOW_PIN,HIGH);} else {digitalWrite(LED_YELLOW_PIN,LOW);}

alarm=false;
if (humidity > 70){
alarm=true;
}
// test alarm=true;

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

onoff = not onoff;
}

if(alarm == true and onoff==true){digitalWrite(LED_RED_PIN,HIGH);} else {digitalWrite(LED_RED_PIN,LOW);}

}

Image:

If humidity (or the sensors reading thereof) is shifting between 65 and 66, the blue LED will flash. You need to either specify a wider threshold (eg. if (humid < 65) then blueOff(); else if (humid > 66) then blueOn(); ) to prevent "flicker" or you need to take multiple readings (eg. 1 reading per second for 5 seconds) and calculate an average from that.

@Danois90 thank you for your answer, this make sense.

maybe here is my mistake and i need && here.
humidity >= 35 and humidity <= 65

MarkusRauch:
@Danois90 thank you for your answer, this make sense.

maybe here is my mistake and i need && here.
humidity >= 35 and humidity <= 65

No difference in "and" vs "&&" - same same :slight_smile: