#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
const int potPin = A0;
const int buzzerPin = 8;
const int chipSelect = 10;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
// SD card initialization
if (!SD.begin(chipSelect)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SD init failed!");
Serial.println("SD card not found.");
return; // Return was used instead of while(1) to avoid crashing in the simulation.
}
lcd.setCursor(0, 0);
lcd.print("SD Ready");
Serial.println("SD card is ready.");
delay(1000);
lcd.clear();
// Write CSV header (if file is newly created)
if (!SD.exists("wind_data.csv")) {
File dataFile = SD.open("wind_data.csv", FILE_WRITE);
if (dataFile) {
dataFile.println("Time(ms),WindSpeed(km/h)");
dataFile.close();
}
}
}
void loop() {
int potValue = analogRead(potPin);
int windSpeed = map(potValue, 0, 1023, 0, 100);
// LCD Update
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wind Speed:");
lcd.setCursor(0, 1);
lcd.print(windSpeed);
lcd.print(" km/h");
// Buzzer
if (windSpeed >= 70) {
tone(buzzerPin, 1000); // Alarm sound
} else {
noTone(buzzerPin);
}
// Write CSV formatted output to serial monitor
Serial.print(millis());
Serial.print(",");
Serial.println(windSpeed);
// Writing data to SD card
File dataFile = SD.open("wind_data.csv", FILE_WRITE);
if (dataFile) {
dataFile.print(millis());
dataFile.print(",");
dataFile.println(windSpeed);
dataFile.close();
Serial.println("Data has been written.");
} else {
Serial.println("Data could not be written. (Expected behavior in simulation environment)");
}
delay(1000);
}