What's new

Help Pahelp about flow chart

_Mj_

Forum Veteran
Established
make a flow chart of this program:

#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>

#define MOISTURE_PIN A0
#define SOLENOID_PIN 9

// Define the moisture threshold values
const int MOISTURE_THRESHOLD_LOW = 30; // Adjust this value as needed
const int MOISTURE_THRESHOLD_HIGH = 70; // Adjust this value as needed

// Define the LCD connections
#define I2C_ADDR 0x27 // I2C address of the LCD module
#define LCD_COLS 16 // Number of columns in the LCD
#define LCD_ROWS 2 // Number of rows in the LCD

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);

const int chipSelect = 10;
unsigned long previousMillis = 0;
const long interval = 1000; // Log interval in milliseconds

void setup() {
Serial.begin(9600);
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.backlight();

pinMode(SOLENOID_PIN, OUTPUT);
digitalWrite(SOLENOID_PIN, HIGH); // Turn off the solenoid valve initially

if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
lcd.print("SD Card Error");
while (1);
}

lcd.print("SD Card OK");
delay(2000);
lcd.clear();
}

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

int moistureValue = analogRead(MOISTURE_PIN);
float moisturePercentage = map(moistureValue, 0, 1023, 0, 100);

String logData = "Moisture: " + String(moisturePercentage, 2) + "%";
Serial.println(logData);
lcd.clear();
lcd.print(logData);

if (moisturePercentage < MOISTURE_THRESHOLD_LOW) {
digitalWrite(SOLENOID_PIN, LOW); // Open the solenoid valve
} else if (moisturePercentage > MOISTURE_THRESHOLD_HIGH) {
digitalWrite(SOLENOID_PIN, HIGH); // Close the solenoid valve
}

File dataFile = SD.open("moisture_log.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(logData);
dataFile.close();
} else {
Serial.println("Error opening data file!");
}
}
}
 
Here is a flowchart representation of the given program:

Code:
Start
|
V
Setup
|
V
Check if SD card initialization failed
|
V
Print "SD Card Error" on LCD and stop program
|
V
Print "SD Card OK" on LCD
|
V
Delay for 2 seconds
|
V
Clear LCD
|
V
Loop
|
V
Check if it is time to log data
|
V
Read moisture value from pin A0
|
V
Map moisture value to percentage
|
V
Create log data string
|
V
Print log data on Serial Monitor
|
V
Clear LCD
|
V
Print log data on LCD
|
V
Check if moisture percentage is below low threshold
|
V
Open solenoid valve
|
V
Check if moisture percentage is above high threshold
|
V
Close solenoid valve
|
V
Open moisture_log.txt file on SD card
|
V
Write log data to file
|
V
Close file
|
V
End loop

Please note that this flowchart assumes that the necessary libraries (SPI, SD, and LiquidCrystal_I2C) are properly installed and included in the program.
 

Similar threads

Back
Top