Components and supplies
1
LED, RGB
1
Alphanumeric LCD, 20 x 4
1
Male/Female Jumper Wires
1
Arduino Nano R3
1
DHT11 Temperature & Humidity Sensor (4 pins)
1
TinyShield Real-Time Clock
1
Gravity BMP388 Barometric Pressure Sensors
Tools and machines
1
Hot glue gun (generic)
1
Solder Wire, Lead Free
1
Soldering iron (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Untitled file
c_cpp
1#include <stdio.h> 2#include <DS1302.h> 3#include "DHT.h" 4#include <Wire.h> 5#include <Adafruit_BMP085.h> 6#include <LiquidCrystal_I2C.h> 7 8#define DHTPIN 2 // what pin we're connected to 9// DHT sensor type 10#define DHTTYPE DHT11 // DHT 11 11//#define DHTTYPE DHT22 // DHT 22 (AM2302) 12DHT dht(DHTPIN, DHTTYPE); 13LiquidCrystal_I2C lcd(0x27, 20, 4); 14Adafruit_BMP085 bmp; 15 16 17const int kCePin = 5; // Chip Enable 18const int kIoPin = 6; // Input/Output 19const int kSclkPin = 7; // Serial Clock 20 21int redPin = 11; 22int greenPin = 13; 23int bluePin = 12; 24 25// Create a DS1302 object. 26DS1302 rtc(kCePin, kIoPin, kSclkPin); 27 28String dayAsString(const Time::Day day) { 29 switch (day) { 30 case Time::kSunday: return "Sunday"; 31 case Time::kMonday: return "Monday"; 32 case Time::kTuesday: return "Tuesday"; 33 case Time::kWednesday: return "Wednesday"; 34 case Time::kThursday: return "Thursday"; 35 case Time::kFriday: return "Friday"; 36 case Time::kSaturday: return "Saturday"; 37 } 38 return "(unknown day)"; 39} 40 41void printTime() { 42 // Get the current time and date from the chip. 43 Time t = rtc.time(); 44 45 // Name the day of the week. 46 const String day = dayAsString(t.day); 47 48 // Format the time and date and insert into the temporary buffer. 49 char buf[50]; 50 snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", 51 day.c_str(), 52 t.yr, t.mon, t.date, 53 t.hr, t.min, t.sec); 54 55 56 57 58 59 // Print the formatted string to serial so we can see the time. 60 Serial.println(buf); 61 float h = dht.readHumidity(); 62 63 lcd.setCursor(0, 0); 64 lcd.print(h); 65 lcd.setCursor(0, 1); 66 lcd.print(t.hr); 67} 68 69 70 71void setup() { 72 Serial.begin(9600); 73 if (!bmp.begin()) { 74 Serial.println("Could not find a valid BMP085 sensor, check wiring!"); 75 while (1) {} 76 } 77 78 dht.begin(); 79 lcd.begin(); 80 lcd.clear(); 81 show_logo(); 82 delay(5000); 83 84 pinMode(9,OUTPUT); 85 pinMode(redPin, OUTPUT); 86 pinMode(greenPin, OUTPUT); 87 pinMode(bluePin, OUTPUT); 88 89 // Initialize a new chip by turning off write protection and clearing the 90 // clock halt flag. These methods needn't always be called. See the DS1302 91 // datasheet for details. 92 rtc.writeProtect(false); 93 rtc.halt(false); 94 95 // Make a new time object to set the date and time. 96 // Sunday, March 29, 2021 at 10:30:50. 97 //Time t(2021, 03, 29, 10, 30, 50, Time::kSunday); 98 99 // Set the time and date on the chip. 100 //rtc.time(t); 101} 102 103// Loop and print the time every second. 104void loop() { 105 106 // Get the current time and date from the chip. 107 Time t = rtc.time(); 108 109 // Name the day of the week. 110 const String day = dayAsString(t.day); 111 112 // Format the time and date and insert into the temporary buffer. 113 char buf[50]; 114 snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", 115 day.c_str(), 116 t.yr, t.mon, t.date, 117 t.hr, t.min, t.sec); 118 119 int h = dht.readHumidity(); 120 int temp = dht.readTemperature(); 121 122 if((t.hr >= 06) and (t.hr < 23)){ //if the time is greater than or equal to 6 a.m. and less than 23 p.m., the screen will work 123 124 lcd.display(); //turn on 125 lcd.backlight(); //turn on backlight 126 127 lcd.setCursor(0, 0); 128 lcd.print("Humidity: "); 129 lcd.print(h); 130 lcd.print("%"); 131 132 lcd.setCursor(0, 1); 133 lcd.print("Temperature: "); 134 lcd.print(temp); 135 lcd.print("c"); 136 137 lcd.setCursor(0, 3); 138 lcd.print("Time: "); 139 if(t.hr < 10) lcd.print("0"); 140 lcd.print(t.hr); 141 lcd.print(":"); 142 if(t.min < 10) lcd.print("0"); 143 lcd.print(t.min); 144 lcd.print(":"); 145 if(t.sec < 10) lcd.print("0"); 146 lcd.print(t.sec); 147 148 float p = bmp.readPressure()/133.3; 149 lcd.setCursor(0, 2); //Place the cursor on the 0th character of the 2nd line 150 lcd.print("Pressure: "); //Displaying the text 151 lcd.print(p,0); //Displaying the pressure value 152 lcd.print("mm"); 153 154 155} 156 157 else{ 158 lcd.clear(); 159 lcd.noDisplay(); 160 lcd.noBacklight(); 161 } 162 163 164int led_r=0, led_g=0, led_b=0; 165 166//change the color of the LED from the temperature 167 168if ((temp >= 0) and (temp < 5) and (t.hr >= 06) and (t.hr < 22)){ 169 led_r = 1; 170 led_g = 1; 171 led_b = 255; 172 } 173 174if ((temp >= 5) and (temp < 10) and (t.hr >= 06) and (t.hr < 22)){ 175 led_r = 1; 176 led_g = 50; 177 led_b = 255; 178 } 179 180if ((temp >= 10) and (temp < 15) and (t.hr >= 06) and (t.hr < 22)){ 181 led_r = 1; 182 led_g = 200; 183 led_b = 200; 184 } 185 186if ((temp >= 15) and (temp < 20) and (t.hr >= 06) and (t.hr < 22)){ 187 led_r = 1; 188 led_g = 250; 189 led_b = 200; 190 } 191 192if ((temp >= 20) and (temp < 25) and (t.hr >= 06) and (t.hr < 22)){ 193 led_r = 10; 194 led_g = 200; 195 led_b = 1; 196 } 197 198if ((temp >= 25) and (temp < 30) and (t.hr >= 06) and (t.hr < 22)){ 199 led_r = 150; 200 led_g = 150; 201 led_b = 1; 202 } 203 204 205if ((temp >= 30) and (temp < 35) and (t.hr >= 06) and (t.hr < 22)){ 206 led_r = 255; 207 led_g = 255; 208 led_b = 1; 209 } 210 211if ((temp >= 35) and (temp < 40) and (t.hr >= 06) and (t.hr < 22)){ 212 led_r = 255; 213 led_g = 1; 214 led_b = 1; 215 } 216 217 analogWrite(redPin, led_r); 218 analogWrite(greenPin, led_g); 219 analogWrite(bluePin, led_b); 220 delay(1000); 221} 222 223void show_logo() { 224 lcd.setCursor(7, 1); 225 lcd.print("Meteo"); 226 lcd.setCursor(6, 2); 227 lcd.print("Station"); 228 229 230 231}
Comments
Only logged in users can leave comments