-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalcode.ino
More file actions
174 lines (153 loc) · 5.26 KB
/
Copy pathfinalcode.ino
File metadata and controls
174 lines (153 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Pin assignments
#define DHTPIN 4 // DHT sensor pin
#define DHTTYPE DHT22 // DHT22 sensor type
#define SSD1306_I2C_ADDRESS 0x3C // OLED I2C address
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin (not used)
#define SENSOR1_PIN 9 // IR Sensor 1 pin
#define SENSOR2_PIN 13 // IR Sensor 2 pin
#define LED_PIN1 3 // LED 1 (PWM)
#define LED_PIN2 5 // LED 2 (PWM)
#define LED_PIN3 6 // LED 3 (PWM)
#define IR_SENSOR1 8 // IR sensor 1
#define IR_SENSOR2 9 // IR sensor 2
#define IR_SENSOR3 10 // IR sensor 3
#define sensorPin 12 // LDR sensor pin
// Constants
const float distance = 0.5; // Distance between IR sensors in meters
const int dimBrightness = 1; // Low brightness (turned off for light detection)
const int maxBrightness = 255; // Max brightness (fully on for IR sensor detection)
// Objects and variables
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float hum = 0.0, temp = 0.0, velocity = 0.0;
unsigned long timeSensor1 = 0, timeSensor2 = 0;
int currentBrightness1 = dimBrightness;
int currentBrightness2 = dimBrightness;
int currentBrightness3 = dimBrightness;
void setup() {
// Serial Monitor initialization
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Initialize OLED display
if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("System Initializing..."));
display.display();
delay(2000);
// Set sensor pins as input
pinMode(SENSOR1_PIN, INPUT);
pinMode(SENSOR2_PIN, INPUT);
pinMode(IR_SENSOR1, INPUT);
pinMode(IR_SENSOR2, INPUT);
pinMode(IR_SENSOR3, INPUT);
pinMode(sensorPin, INPUT); // LDR sensor pin
// Set LED pins as output
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(LED_PIN3, OUTPUT);
}
void loop() {
// Check LDR sensor value to decide LED behavior
int ldrValue = digitalRead(sensorPin); // Read the LDR value
if (ldrValue == LOW) {
// If light is detected (LDR is in light), turn off LEDs
analogWrite(LED_PIN1, 0);
analogWrite(LED_PIN2, 0);
analogWrite(LED_PIN3, 0);
} else {
// If no light is detected (LDR is in darkness), control LEDs based on IR sensors
controlLEDs();
}
// Update DHT sensor data
hum = dht.readHumidity();
temp = dht.readTemperature();
// Display temperature and humidity
if (!isnan(hum) && !isnan(temp)) {
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
} else {
Serial.println(F("Failed to read from DHT sensor!"));
}
// Check IR sensors for velocity calculation
if (digitalRead(SENSOR1_PIN) == LOW) {
timeSensor1 = micros();
delay(5); // Debounce
}
if (digitalRead(SENSOR2_PIN) == LOW) {
timeSensor2 = micros();
float timeTaken = (timeSensor2 - timeSensor1) / 1e6;
if (timeTaken > 0) {
velocity = distance / timeTaken;
Serial.print("Time Taken: ");
Serial.print(timeTaken, 6);
Serial.println(" seconds");
Serial.print("Velocity: ");
Serial.print(velocity, 2);
Serial.println(" m/s");
} else {
velocity = 0.0;
Serial.println("Invalid time taken");
}
timeSensor1 = 0;
timeSensor2 = 0;
}
// Update OLED display
displayData();
delay(10); // Short delay for stability
}
void controlLEDs() {
// Check IR sensors for object detection
int irValue1 = digitalRead(IR_SENSOR1);
int irValue2 = digitalRead(IR_SENSOR2);
int irValue3 = digitalRead(IR_SENSOR3);
int targetBrightness1 = (irValue1 == LOW) ? maxBrightness : dimBrightness;
int targetBrightness2 = (irValue2 == LOW) ? maxBrightness : dimBrightness;
int targetBrightness3 = (irValue3 == LOW) ? maxBrightness : dimBrightness;
adjustBrightness(LED_PIN1, currentBrightness1, targetBrightness1);
adjustBrightness(LED_PIN2, currentBrightness2, targetBrightness2);
adjustBrightness(LED_PIN3, currentBrightness3, targetBrightness3);
}
void adjustBrightness(int ledPin, int ¤tBrightness, int targetBrightness) {
if (currentBrightness < targetBrightness) {
currentBrightness++;
} else if (currentBrightness > targetBrightness) {
currentBrightness--;
}
analogWrite(ledPin, currentBrightness);
}
void displayData() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println(F("Temp & Humidity"));
display.setCursor(0, 10);
display.print("Temp: ");
display.print(temp);
display.println(" C");
display.setCursor(0, 20);
display.print("Hum: ");
display.print(hum);
display.println(" %");
display.setCursor(0, 35);
display.println(F("Velocity"));
display.setCursor(0, 45);
display.print("Speed: ");
display.print(velocity, 2);
display.println(" m/s");
display.display();
}