// #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// #define SCREEN_WIDTH 128
// #define SCREEN_HEIGHT 64
// #define OLED_RESET -1
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// // Define pins for A4988 stepper motor driver
// #define DIR_PIN 25
// #define STEP_PIN 26
// // Define LED pin
// #define LED_PIN 27
// // Define temperature sensor pin
// #define TEMP_SENSOR_PIN 34
// #define BETA 3950 // Beta value of the NTC thermistor
// float temperatureC;
// void setup() {
// // Initialize serial communication
// Serial.begin(115200);
// // Set up pin modes
// pinMode(DIR_PIN, OUTPUT);
// pinMode(STEP_PIN, OUTPUT);
// pinMode(LED_PIN, OUTPUT);
// // Initialize the LED and motor as off
// digitalWrite(LED_PIN, LOW);
// digitalWrite(DIR_PIN, LOW);
// digitalWrite(STEP_PIN, LOW);
// // Initialize the OLED display
// if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println(F("SSD1306 allocation failed"));
// for (;;);
// }
// display.display();
// delay(2000); // Pause for 2 seconds
// display.clearDisplay();
// }
// void loop() {
// // Read the NTC thermistor value and calculate temperature
// int analogValue = analogRead(TEMP_SENSOR_PIN);
// float resistance = 10000.0 * (4095.0 / analogValue - 1.0);
// temperatureC = 1.0 / (log(resistance / 10000.0) / BETA + 1.0 / 298.15) - 273.15;
// Serial.print("Temperature: ");
// Serial.print(temperatureC);
// Serial.println(" °C");
// display.clearDisplay();
// display.setTextSize(1);
// display.setTextColor(SSD1306_WHITE);
// display.setCursor(0, 0);
// if (temperatureC > 30) {
// // Temperature threshold exceeded, trigger motor and LED
// Serial.println("Rain Detected! wiper starts working ");
// display.println("Rain Detected!");
// digitalWrite(LED_PIN, HIGH); // Turn on LED
// // Step the motor (open umbrella)
// digitalWrite(DIR_PIN, HIGH); // Set direction
// for (int i = 0; i < 200; i++) { // Step for 200 steps
// digitalWrite(STEP_PIN, HIGH);
// delayMicroseconds(800);
// digitalWrite(STEP_PIN, LOW);
// delayMicroseconds(800);
// }
// } else {
// // No rain detected, stop motor and LED
// Serial.println("No Rain.wiper does not work ");
// display.println("No Rain.");
// digitalWrite(LED_PIN, LOW); // Turn off LED
// // Step the motor in the reverse direction (close umbrella)
// digitalWrite(DIR_PIN, LOW); // Set direction
// for (int i = 0; i < 200; i++) { // Step for 200 steps
// digitalWrite(STEP_PIN, HIGH);
// delayMicroseconds(800);
// digitalWrite(STEP_PIN, LOW);
// delayMicroseconds(800);
// }
// }
// display.display(); // Show the updated content on the OLED
// delay(2000); // Wait 2 seconds before the next reading
// }
#include <Wire.h>
// Define pins for A4988 stepper motor driver
#define DIR_PIN 25
#define STEP_PIN 26
// Define LED pin
#define LED_PIN 27
// Define temperature sensor pin
#define TEMP_SENSOR_PIN 34
#define BETA 3950 // Beta value of the NTC thermistor
float temperatureC;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Set up pin modes
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Initialize the LED and motor as off
digitalWrite(LED_PIN, LOW);
digitalWrite(DIR_PIN, LOW);
digitalWrite(STEP_PIN, LOW);
}
void loop() {
// Read the NTC thermistor value and calculate temperature
int analogValue = analogRead(TEMP_SENSOR_PIN);
float resistance = 10000.0 * (4095.0 / analogValue - 1.0);
temperatureC = 1.0 / (log(resistance / 10000.0) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
if (temperatureC > 30) {
// Temperature threshold exceeded, trigger motor and LED
Serial.println("Rain Detected! Opening Umbrella...");
digitalWrite(LED_PIN, HIGH); // Turn on LED
// Step the motor (open umbrella)
digitalWrite(DIR_PIN, HIGH); // Set direction
for (int i = 0; i < 200; i++) { // Step for 200 steps
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(800);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(800);
}
} else {
// No rain detected, stop motor and LED
Serial.println("No Rain. Closing Umbrella...");
digitalWrite(LED_PIN, LOW); // Turn off LED
// Step the motor in the reverse direction (close umbrella)
digitalWrite(DIR_PIN, LOW); // Set direction
for (int i = 0; i < 200; i++) { // Step for 200 steps
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(800);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(800);
}
}
delay(2000); // Wait 2 seconds before the next reading
}