#include // for LCD #include #include #include #include // Temperature sensor LCD pins #define LCD_RS_PIN 2 #define LCD_E_PIN 3 #define LCD_D4_PIN 4 #define LCD_D5_PIN 6 #define LCD_D6_PIN 7 #define LCD_D7_PIN 8 // Wind data LCD pins #define LCD_RS_PIN_wind 13 #define LCD_E_PIN_wind 11 #define LCD_D4_PIN_wind 12 #define LCD_D5_PIN_wind 10 #define LCD_D6_PIN_wind 9 #define LCD_D7_PIN_wind A3 // Wind sensors pins #define windPin A0 #define dirPin A1 unsigned long previous_time_temp_check = 0; unsigned long previous_time_wind_check = 0; unsigned int temp_array_count = 0; char temp_array[96]; unsigned int pres_array_count = 0; int pres_array[96]; unsigned int wind_speed_array_count = 0; char wind_speed_array[300]; unsigned int wind_direction_array_count = 0; int wind_direction_array[60]; // This is the amount that we will subtract from the voltage float voltage_offset = 0.14; // Create BME280 object for I2C Adafruit_BME280 bme; // Create LCD object LiquidCrystal lcd(LCD_RS_PIN, LCD_E_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN); LiquidCrystal lcd_wind(LCD_RS_PIN_wind, LCD_E_PIN_wind, LCD_D4_PIN_wind, LCD_D5_PIN_wind, LCD_D6_PIN_wind, LCD_D7_PIN_wind); void setup() { Serial.begin(9600); lcd.begin(20, 4); lcd_wind.begin(20, 4); Serial.println(F("BME280 I2C Test")); // Filling the array with values that are less than what could be in the temp. // This is so when the computer calculates the high and low it won’t use garbage // values that are automatically put into the array. That’s why I used -100. for(int i = 0; i < 96; i++){ temp_array[i] = -50; } // same as for the temp array for(int i = 0; i < 96; i++){ pres_array[i] = 100; } for(int i = 0; i < 300; i++){ wind_speed_array[i] = 0; } // test to initialize the sensor if (!bme.begin(0x76)) { // Use 0x76 (most modules) or 0x77 if needed Serial.println(F("Could not find a valid BME280 sensor, check wiring!")); while (1); // Stop here } // initial setup lcd.setCursor(0, 0); lcd.print(F("The temperature will show in 30 seconds")); delay(30000); // to let the sensor stabilize before reading //---------------------------------------------------------------------------------------------------------------------- int temp = bme.readTemperature(); lcd.setCursor(0, 0); lcd.print(F(" ")); lcd.setCursor(0, 0); lcd.print(F("Temp = ")); lcd.print(temp); lcd.print((char)223); //-------------------------------------------------------------------------------------------------------------------------- float stationPressure = bme.readPressure() / 100.0; // BME280 gives Pa, convert to hPa int altitude = 410; // meters // I'm using int because I don't want the decimal place. unsigned int seaLevelPressure = stationPressure * pow((1 - (altitude / 44330.0)), -5.255); lcd.setCursor(0, 1); lcd.print(F(" ")); lcd.setCursor(0, 1); lcd.print(F("Pres = ")); lcd.print(seaLevelPressure); lcd.print(F(" hPa")); //------------------------------------------------------------------------------------------------------------------------- unsigned int humidity = bme.readHumidity(); lcd.setCursor(0, 2); lcd.print(F(" ")); lcd.setCursor(0, 2); lcd.print(F("Humidity = ")); lcd.print(humidity); lcd.print(F("%")); } void loop() { unsigned long time_now = millis(); // Get the current time if(time_now - previous_time_wind_check > 1000){ // ===== Wind speed ===== int analog_from_speed = analogRead(windPin); // 0–1023 float voltage_from_speed = analog_from_speed / 204.6 - voltage_offset; // 1023/5 ≈ 204.6 for each volt // voltage_offset is an amount you want to subtract from the raw volts, because the sensor is not 0 when it is // not moving and you want it to be 0. To fix that you use the offset. int wind_kmph = voltage_from_speed * 12 * 3.6; // 5V -> 60 m/s => 12 m/s per volt then * 3.6 -> km/h // Puts the wind speed value in the right array slot int array_wind_place = wind_speed_array_count % 300; wind_speed_array[array_wind_place] = wind_kmph; wind_speed_array_count++; // // this is only for testing the array place // Serial.println("array place wind"); // Serial.println(array_wind_place); // This will only be accurate 5 minutes after start int wind_average = 0; for(int i = 0; i < 300; i++){ wind_average += wind_speed_array[i]; } wind_average /= 300; // //for testing // Serial.println("wind average"); // Serial.println(wind_average); if(wind_kmph < 5){ lcd_wind.setCursor(0, 0); lcd_wind.print(F(" ")); lcd_wind.setCursor(0, 0); lcd_wind.print(F("Wind is light")); }else{ lcd_wind.setCursor(0, 0); lcd_wind.print(F(" ")); lcd_wind.setCursor(0, 0); lcd_wind.print(F("Wind Speed: ")); lcd_wind.print(wind_kmph); lcd_wind.print(F("km")); } lcd_wind.setCursor(0, 1); lcd_wind.print(F(" ")); lcd_wind.setCursor(0, 1); lcd_wind.print(F("5M wind avg: ")); lcd_wind.print(wind_average); lcd_wind.print(F("km")); lcd_wind.setCursor(0, 3); lcd_wind.print(F("V:")); lcd_wind.print(voltage_from_speed, 2); // Print results on serial for testing // Serial.print("Raw ADC: "); // Serial.print(rawSpeed); // Serial.print(" | Voltage: "); // Serial.print(voltSpeed, 2); //Serial.print(F("Wind Speed: ")); // Serial.print(windMs, 2); // Serial.print(" m/s ("); //Serial.print(wind_kmph); //Serial.print(F(" km/h)")); // ===== Wind direction ===== int analog_from_direction = analogRead(dirPin); // 0–1023 float volts_from_direction = analog_from_direction / 204.6; int wind_direction = (volts_from_direction / 5.0) * 360.0; int North = 0; int Northeast = 0; int East = 0; int Southeast = 0; int South = 0; int Southwest = 0; int West = 0; int Northwest = 0; int array_place_direction = wind_direction_array_count % 60; wind_direction_array[array_place_direction] = wind_direction; wind_direction_array_count++; for(int i = 0; i < 60; i++){ int test = wind_direction_array[i]; if(test > 341){ North++; } else if(test < 30){ Northeast++; } else if(test < 70){ East++; } else if(test < 130){ Southeast++; } else if(test < 180){ South++; } else if(test < 230){ Southwest++; } else if(test < 280){ West++; } else if(test < 340){ Northwest++; } } char direction[3] = "--"; // 2 letters max + null terminator // Checking which is the biggest. if (North > Northeast && North > East && North > Southeast && North > South && North > Southwest && North > West && North > Northwest) { strcpy(direction, "N"); } else if (Northeast > North && Northeast > East && Northeast > Southeast && Northeast > South && Northeast > Southwest && Northeast > West && Northeast > Northwest) { strcpy(direction, "NE"); } else if (East > North && East > Northeast && East > Southeast && East > South && East > Southwest && East > West && East > Northwest) { strcpy(direction, "E"); } else if (Southeast > North && Southeast > Northeast && Southeast > East && Southeast > South && Southeast > Southwest && Southeast > West && Southeast > Northwest) { strcpy(direction, "SE"); } else if (South > North && South > Northeast && South > East && South > Southeast && South > Southwest && South > West && South > Northwest) { strcpy(direction, "S"); } else if (Southwest > North && Southwest > Northeast && Southwest > East && Southwest > Southeast && Southwest > South && Southwest > West && Southwest > Northwest) { strcpy(direction, "SW"); } else if (West > North && West > Northeast && West > East && West > Southeast && West > South && West > Southwest && West > Northwest) { strcpy(direction, "W"); } else if (Northwest > North && Northwest > Northeast && Northwest > East && Northwest > Southeast && Northwest > South && Northwest > Southwest && Northwest > West) { strcpy(direction, "NW"); } lcd_wind.setCursor(0, 2); lcd_wind.print(F(" ")); lcd_wind.setCursor(0, 2); lcd_wind.print(F("wind direction: ")); lcd_wind.print(direction); // // This is just for testing // Serial.print(" || Dir Raw: "); // Serial.print(rawDir); // Serial.print(" | Dir Volt: "); // Serial.print(voltDir, 2); // Serial.print(F("Wind Direction: ")); // Serial.print(wind_direction); // Serial.println(F(" deg")); previous_time_wind_check += 1000; } if (time_now - previous_time_temp_check > 900000){ // ------------------------------------------------------------------------------------------------------------------------ int temp = bme.readTemperature(); lcd.setCursor(0, 0); lcd.print(F(" ")); lcd.setCursor(0, 0); lcd.print(F("Temp = ")); lcd.print(temp); lcd.print((char)223); // // Testing to see that the count works in serial // Serial.println(F("temp array count")); // Serial.println(temp_array_count); // places the temp value in the right array slot int array_place = temp_array_count % 96; temp_array[array_place] = temp; temp_array_count++; // // this is only for testing the array place // Serial.println(F("array place temp")); // Serial.println(array_place); // This whole chunk is for the high and low function int high_temp = temp_array[0]; int low_temp = temp_array[0]; for(int i = 0; i < 96; i++){ if(temp_array[i] < low_temp && temp_array[i] != -50){ low_temp = temp_array[i]; } else if(temp_array[i] > high_temp){ high_temp = temp_array[i]; } } lcd.setCursor(12, 0); lcd.print(F("H")); lcd.print(high_temp); lcd.print(F(" L")); lcd.print(low_temp); //-------------------------------------------------------------------------------------------------------------------------- // This gets the pressure and turns the station pressure into sea level pressure float station_pressure = bme.readPressure() / 100.0; // BME280 gives Pa, convert to hPa int altitude = 410; // meters // I'm using int because I don't want the decimal place. unsigned int sea_level_pressure = station_pressure * pow((1 - (altitude / 44330.0)), -5.255); lcd.setCursor(0, 1); lcd.print(F("Pres = ")); lcd.print(sea_level_pressure); lcd.print(F(" hPa")); // Array placement unsigned char array_place_pres = pres_array_count % 96; pres_array[array_place_pres] = sea_level_pressure; pres_array_count++; // // This is just for testing // Serial.println(F("pressure array place")); // Serial.println(pres_array_count); // Serial.println(F("array place presser")); // Serial.println(F("array place pressure")); unsigned int high_pres = pres_array[0]; unsigned int low_pres = pres_array[0]; for(int i = 0; i < 96; i++){ if(pres_array[i] < low_pres && pres_array[i] != 100){ low_pres = pres_array[i]; } else if(pres_array[i] > high_pres){ high_pres = pres_array[i]; } } lcd.setCursor(0, 2); lcd.print(F(" ")); lcd.setCursor(0, 2); lcd.print(F("HP")); lcd.print(high_pres); lcd.print(F(" LP")); lcd.print(low_pres); //------------------------------------------------------------------------------------------------------------------------ // humidity check // the int is so there should be no decimal int humidity = bme.readHumidity(); lcd.setCursor(0, 3); lcd.print(F("Humidity = ")); lcd.print(humidity); lcd.print(F("%")); // ----------------------------------------------------------------------------------------------------------------------- previous_time_temp_check += 900000; } } // lcd.setCursor(0, 3); // lcd.print("Altitude = "); // lcd.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); // lcd.print(" m");