analogRead value to multiple digitalWrite's

Does anyone know how I can make a single analogRead from a sensor to do the following at the same time:

  1. Send a message to an I2C LCD screen
  2. Make a buzzer go of when it meets a certain condition
  3. Make one of two LED's go HIGH when it meet that some condition

Each of these action separatly is not the problem, but how to let them work at the same time.

what does "at the same time" mean? you only have one processor, so it deals with most of high level things sequentially....

post the code of what you tried (using code tags)

Make functions of every separate code and put them in an IF

if (analogRead(A0) > 500)
{
SendMessageLCD();
Buzzer();
SetLeds();
}

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>          //https://github.com/marcoschwartz/LiquidCrystal_I2C

#define SoilMoistureADMax 625           //maximal analog value
#define SoilMoistureADMin 0             //minimal analog value

byte MaxThreshold = 60;                 //maximal threshold percentage
byte MinThreshold = 40;                 //minimal threshold percentage

LiquidCrystal_I2C lcd(0x27,20,4);       //sets the LCD address to 0x27 for a 20 chars and 4 line display

const int LED_red = 8;                        //assigns pin 8 to red LED
const int LED_green = 9;                      //assigns pin 9 to green LED

const int buzzer = 12;                  //assigns pin 12 to buzzer

void setup()
{
  lcd.init();                           //initializes the LCD
  
pinMode(LED_red, OUTPUT);
pinMode(LED_green, OUTPUT); 
 
}


void loop() {
  int moist = analogRead(0);

  moist = map(moist, SoilMoistureADMin, SoilMoistureADMax, 0, 100);

  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Moisture ");
  lcd.setCursor(3,1);
  lcd.print("percentage:");
  lcd.setCursor(16,1);
  lcd.print(moist);
  lcd.setCursor(18,1);
  lcd.print("%");

delay(1500);
 
 
  if (moist >= MaxThreshold) {
    digitalWrite(LED_red, HIGH);
    lcd.setCursor(0,3);
    lcd.print("Soil is to wet     ");
    
     }

  else if (moist <= MinThreshold) { 
    digitalWrite(LED_red, HIGH);
    lcd.setCursor(0,3);
    lcd.print("");
    lcd.print("Soil is to dry     ");
   
     }
    
   else {
   digitalWrite(LED_red, LOW);
   digitalWrite(LED_green, HIGH);
   lcd.setCursor(0,3);
   lcd.print("");
   lcd.print("Good soil condition");
    
     }

   
}

so what’s the issue?

LCD Screen works, but red LED keeps blinking and green LED doesn't do anything, despite changing analogRead values

Do you have wiring problems? If the LCD reflects correct readings (dry/wet/ok) but green led does not work with e.g. a mapped value of 50, you might very well have a wiring error. Red led blinks, so it alternates between dry/wet/ok.

Use Serial.print statements of the analog reading (not the mapped one) as well as of the mapped reading. Do they reflect what you expect?

Don't forget to add a Serial.begin() in setup :wink:

Problem solved (faulty breadboard), working fine now. Thx