Вы находитесь на странице: 1из 6

#include <Wire.

h>
#include <SHT1x.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 20,4);

// sensor anemometer
const int sensorPin = A0; //Defines the pin that the anemometer output is connected to
int sensorValue = 0; //Variable stores the value direct from the analog pin
float sensorVoltage = 0; //Variable that stores the voltage (in Volts) from the anemometer
being sent to the analog pin
float windSpeed = 0; // Wind speed in meters per second (m/s)
float Speed;
float voltageConversionConstant = 0.004887586; //This constant maps the value provided
from the analog read function,
//which ranges from 0 to 1023, to actual voltage, which ranges from 0V to 5V
//Anemometer Technical Variables
//The following variables correspond to the anemometer sold by Adafruit, but could be
modified to fit other anemometers.
float voltageMin = 0.15; // Mininum output voltage from anemometer in mV.
float windSpeedMin = 0; // Wind speed in meters/sec corresponding to minimum voltage
float voltageMax = 5.0; // Maximum output voltage from anemometer in mV.
float windSpeedMax = 30; // Wind speed in meters/sec corresponding to maximum voltage

// sensor wind direction


int VaneValue;// raw analog value from wind vane
int Direction;// translated 0 - 360 direction
int CalDirection;// converted value with offset applied
int LastValue;
#define Offset 0;
int degree = 360;

// SHT15
#define dataPin 2
#define clockPin 3
SHT1x sht1x(dataPin,clockPin);
float temp_c;
float humidity;

void setup()
{
LastValue = 1;
lcd.begin();
}

void loop()
{

//program anemometer
sensorValue = analogRead(A0); //Get a value between 0 and 1023 from the analog pin
connected to the anemometer
sensorVoltage = sensorValue * voltageConversionConstant; //Convert sensor value to actual
voltage
//Convert voltage value to wind speed using range of max and min voltages and wind speed
for the anemometer
if (sensorVoltage <= voltageMin)
{
windSpeed = 0; //Check if voltage is below minimum value. If so, set wind speed to zero.
}
else
{
windSpeed = (sensorVoltage - voltageMin)*windSpeedMax/(voltageMax - voltageMin);
//For voltages above minimum value, use the linear relationship to calculate wind speed.
}
if ((windSpeed >= 0) and (windSpeed <= 0.56))
{
Speed = (windSpeed*3);
}
if ((windSpeed > 0.56) and (windSpeed <= 2.71))
{
Speed = windSpeed*1.376;
}
if ((windSpeed >2.71) and (windSpeed <= 5.63))
{
Speed = windSpeed*1.2078;
}
if ((windSpeed > 5.63) and (windSpeed <= 6.78))
{
Speed = windSpeed*1.1579;
}
if ((windSpeed > 6.78) and (windSpeed <= 8.14))
{
Speed = windSpeed*1.1057;
}
if ((windSpeed > 8.14) and (windSpeed <= 8.55))
{
Speed = windSpeed*1.1579;
}
if ((windSpeed > 8.55) and (windSpeed <= 8.91))
{
Speed = windSpeed*1.1448;
}

//program sht15
temp_c=sht1x.readTemperatureC();
humidity= sht1x.readHumidity();
//program wind direction
VaneValue = analogRead(A1);
Direction = map(VaneValue, 0, 1023, 0, 360);
CalDirection = Direction + Offset;
if(CalDirection > 360)
{
CalDirection = CalDirection - 360;
}

if(CalDirection < 0)
{
CalDirection = CalDirection + 360;
}
// Only update the display if change greater than 2 degrees.
if(abs(CalDirection - LastValue) > 5)
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Wind D:");
lcd.setCursor(8,1);
lcd.print(CalDirection);
lcd.setCursor (13,1);
getHeading(CalDirection);
LastValue = CalDirection;
delayMicroseconds(10);
}

//program lcd
lcd.setCursor(0,0);
lcd.print("Wind S:");
lcd.setCursor(8,0);
lcd.print(Speed);
lcd.setCursor(13,0);
lcd.print("m/s");
lcd.setCursor (0,2);
lcd.print("S:");
lcd.setCursor(3,2);
lcd.print(temp_c);
lcd.setCursor(8,2);
lcd.print("C");
lcd.setCursor(10,2);
lcd.print("H:");
lcd.setCursor(13,2);
lcd.print(humidity);
lcd.setCursor(18,2);
lcd.print("%");

delay(1000);
}
// Converts compass direction to heading
void getHeading(int direction)
{
if(direction < 22)
lcd.print("N");
else if (direction < 67)
lcd.print("NE");
else if (direction < 112)
lcd.print("E");
else if (direction < 157)
lcd.print("SE");
else if (direction < 212)
lcd.print("S");
else if (direction < 247)
lcd.print("SW");
else if (direction < 292)
lcd.print("W");
else if (direction < 337)
lcd.print("NW");
else
lcd.print("N");
}