0% found this document useful (0 votes)
5 views3 pages

Arduino Test final

Uploaded by

yejixx999
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
5 views3 pages

Arduino Test final

Uploaded by

yejixx999
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 3

makle thi 2 codes on 1 sketch

const int sensor1Pin = A0; // Analog pin for moisture sensor


const int outputPin1 = 3; // Digital pin for controlling an output (e.g., an
LED)
int moistureValue;

// Sensor 2 Setup
const int sensor2Pin = A1; // Analog pin for moisture sensor
const int outputPin2 = 4; // Digital pin for controlling an output (e.g., an
LED)
int moistureValue2;

// Sensor 3 Setup
const int sensor3Pin = A2; // Analog pin for the water level sensor
const int buzzerPin = 8; // Digital pin for the buzzer
const int threshold = 500; // Threshold for the water level sensor

void setup() {
pinMode(outputPin1, OUTPUT);
pinMode(outputPin2, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
Serial.println("Reading from sensors...");
}

void loop() {
// Reading from Sensor 1
moistureValue = analogRead(sensor1Pin);
moistureValue = map(moistureValue, 550, 10, 0, 100);
Serial.print("Moisture: ");
Serial.print(moistureValue);
Serial.println("%");

if (moistureValue > 0) {
digitalWrite(outputPin1, HIGH); // Turn on the output if moisture value is
above 0
} else {
digitalWrite(outputPin1, LOW); // Turn off the output if moisture value is 0
or below
}

// Reading from Sensor 2


moistureValue2 = analogRead(sensor2Pin);
moistureValue2 = map(moistureValue2, 550, 10, 0, 100);
Serial.print("Moisture: ");
Serial.print(moistureValue2);
Serial.println("%");

if (moistureValue2 > 0) {
digitalWrite(outputPin2, HIGH); // Turn on the output if moisture value is
above 0
} else {
digitalWrite(outputPin2, LOW); // Turn off the output if moisture value is 0
or below
}

// Reading from Sensor 3


int sensor3Value = analogRead(sensor3Pin);
Serial.print("Sensor 3 Value: ");
Serial.println(sensor3Value);

if (sensor3Value < threshold) {


digitalWrite(buzzerPin, HIGH); // Turn buzzer on
} else {
digitalWrite(buzzerPin, HIGH); // Turn buzzer on for 200ms
delay(200);
digitalWrite(buzzerPin, LOW); // Turn buzzer off
delay(200);
}

delay(1000); // Delay for stability


}

and this

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Create an LCD object with I2C address 0x27 (common for many I2C LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define the analog pins for the sensors


const int sensor1Pin = A0;
const int sensor2Pin = A1;
const int sensor3Pin = A2;

// Define threshold for the sensor3 to determine water presence


const int waterThreshold = 500; // Adjust based on sensor calibration

// Define maximum sensor values observed


const int maxSensorValue = 1023; // Assuming 10-bit ADC resolution

void setup() {
// Initialize the LCD
lcd.begin();
lcd.backlight(); // Turn on the backlight (if applicable)

// Print initial messages to the LCD


lcd.setCursor(0, 0);
lcd.print("S1: % S2: %");
}

void loop() {
// Read the moisture levels from the sensors
int sensor1Value = analogRead(sensor1Pin);
int sensor2Value = analogRead(sensor2Pin);
int sensor3Value = analogRead(sensor3Pin);

// Check if the sensor value is within a reasonable range (e.g., 0 to 1000)


int maxSensorReading = 1000; // Adjust this based on your sensor's expected range
if (sensor1Value > maxSensorReading) sensor1Value = maxSensorReading;
if (sensor2Value > maxSensorReading) sensor2Value = maxSensorReading;

// Convert the sensor readings to percentages


int sensor1Percent = map(sensor1Value, 0, maxSensorReading, 0, 100);
int sensor2Percent = map(sensor2Value, 0, maxSensorReading, 0, 100);
// Clear the first line and update it with the new sensor values
lcd.setCursor(0, 0);
lcd.print("S1:");
lcd.print(sensor1Percent);
lcd.print("% S2:");
lcd.print(sensor2Percent);
lcd.print("%");

// Clear the second line


lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous message

// Check the sensor3 for water presence


lcd.setCursor(0, 1);
if (sensor3Value < waterThreshold) {
lcd.print("Tank: Water OK");
} else {
lcd.print("Tank: No Water");
}

// Wait for a second before updating


delay(1000);
}

You might also like