Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
4.9 kB
15
Indexable
Never
#include <DHT_U.h>
#include <DHT.h>

float TempHumdTable[15][11] =
	{{331.4313, 334.4107, 337.2659, 340.0071, 342.6441, 345.1864, 347.6434, 350.0246, 352.3395, 354.5979, 356.8101},
	 {331.447, 334.4334, 337.2982, 340.0525, 342.707, 345.2724, 347.7595, 350.1796, 352.5442, 354.8655, 357.1564},
	 {331.4627, 334.4561, 337.3306, 340.0979, 342.7698, 345.3583, 347.8756, 350.3346, 352.7488, 355.1329, 357.5024},
	 {331.4783, 334.4787, 337.3629, 340.1433, 342.8327, 345.4442, 347.9916, 350.4895, 352.9533, 355.4, 357.848},
	 {331.494, 334.5014, 337.3952, 340.1887, 342.8955, 345.5302, 348.1076, 350.6443, 353.1577, 355.667, 358.1933},
	 {331.5097, 334.5241, 337.4275, 340.234, 342.9584, 345.616, 348.2236, 350.799, 353.3619, 355.9338, 358.5383},
	 {331.5253, 334.5468, 337.4598, 340.2794, 343.0212, 345.7019, 348.3395, 350.9537, 353.566, 356.2003, 358.8829},
	 {331.541, 334.5694, 337.4921, 340.3248, 343.084, 345.7878, 348.4554, 351.1083, 353.7701, 356.4667, 359.2272},
	 {331.5567, 334.5921, 337.5244, 340.3701, 343.1468, 345.8736, 348.5713, 351.2629, 353.9739, 356.7329, 359.5712},
	 {331.5723, 334.6148, 337.5567, 340.4155, 343.2096, 345.9594, 348.6871, 351.4174, 354.1777, 356.9988, 359.9148},
	 {331.588, 334.6375, 337.589, 340.4608, 343.2724, 346.0452, 348.8028, 351.5718, 354.3814, 357.2646, 360.2581},
	 {331.6036, 334.6601, 337.6213, 340.5062, 343.3352, 346.1309, 348.9186, 351.7261, 354.5849, 357.5302, 360.6011},
	 {331.6193, 334.6828, 337.6536, 340.5515, 343.3979, 346.2167, 349.0343, 351.8804, 354.7884, 357.7955, 360.9437},
	 {331.6349, 334.7054, 337.6859, 340.5968, 343.4607, 346.3024, 349.1499, 352.0346, 354.9917, 358.0607, 361.2861},
	 {331.6506, 334.7281, 337.7182, 340.6421, 343.5234, 346.3881, 349.2655, 352.1887, 355.1949, 358.3257, 361.6281}};

const int trig = 13;
const int echo = 12;
const int led = 11;

const int sega = 2;
const int segb = 3;
const int segc = 4;
const int segd = 5;
const int sege = 6;
const int segf = 7;
const int segg = 8;
const int segdp = 9;

int seg_table[10][8] =
	{{1, 1, 1, 1, 1, 1, 0, 0},
	 {0, 1, 1, 0, 0, 0, 0, 0},
	 {1, 1, 0, 1, 1, 0, 1, 0},
	 {1, 1, 1, 1, 0, 0, 1, 0},
	 {0, 1, 1, 0, 0, 1, 1, 0},
	 {1, 0, 1, 1, 0, 1, 1, 0},
	 {1, 0, 1, 1, 1, 1, 1, 0},
	 {1, 1, 1, 0, 0, 0, 0, 0},
	 {1, 1, 1, 1, 1, 1, 1, 0},
	 {1, 1, 1, 1, 0, 1, 1, 0}};

const int DHTPIN = 10;
const int DHTTYPE = DHT11;
DHT dht(DHTPIN, DHTTYPE);

float distance_cm = 0;

void setup()
{
	Serial.begin(9600);
	pinMode(trig, OUTPUT);
	pinMode(echo, INPUT);
	pinMode(led, OUTPUT);

	dht.begin();
}

void loop()
{
	digitalWrite(trig, LOW);
	delay(10);
	digitalWrite(trig, HIGH);
	delay(10);
	digitalWrite(trig, LOW);

	// distance
	distance_cm = pulseIn(echo, HIGH) / 58.8;
	distance_cm = (int)(distance_cm * 100) / 100.0;
	Serial.print("Distance: ");
	Serial.print(distance_cm);
	Serial.println(" cm");

	// LED
	if (distance_cm < 10)
	{
		digitalWrite(led, HIGH);
	}
	else
	{
		digitalWrite(led, LOW);
	}

	// print temperature and humidity from sensor
	float humidity = dht.readHumidity();
	float temperature = dht.readTemperature();
	Serial.print("Humidity: ");
	Serial.print(humidity);
	Serial.print(" %\t");
	Serial.print("Temperature: ");
	Serial.print(temperature);
	Serial.println(" *C");

	// fix distance by temperature and humidity
	int temp = (int)temperature / 10;
	int humd = (int)humidity / 5 - 2;
	float fix_speed_of_sound = TempHumdTable[temp][humd];
	Serial.print("Fix speed of sound: ");
	Serial.print(fix_speed_of_sound);
	Serial.println(" m/s");
	float fix_distance = distance_cm * 340 / fix_speed_of_sound;
	fix_distance = (int)(fix_distance * 100) / 100.0;
	Serial.print("Fix distance: ");
	Serial.print(fix_distance);
	Serial.println(" cm");
	Serial.println();

	// print distance on 7-segment
	int digit[4];
	digit[0] = (int)(fix_distance / 10);
	digit[1] = (int)fix_distance % 10;
	digit[2] = (int)(fix_distance * 10) % 10;
	digit[3] = (int)(fix_distance * 100) % 10;

	digitalWrite(sega, seg_table[0][0]);
	digitalWrite(segb, seg_table[0][1]);
	digitalWrite(segc, seg_table[0][2]);
	digitalWrite(segd, seg_table[0][3]);
	digitalWrite(sege, seg_table[0][4]);
	digitalWrite(segf, seg_table[0][5]);
	digitalWrite(segg, seg_table[0][6]);
	digitalWrite(segdp, seg_table[0][7]);
	delay(1000);
	for (int i = 0; i < 4; i++)
	{
		digitalWrite(sega, seg_table[digit[i]][0]);
		digitalWrite(segb, seg_table[digit[i]][1]);
		digitalWrite(segc, seg_table[digit[i]][2]);
		digitalWrite(segd, seg_table[digit[i]][3]);
		digitalWrite(sege, seg_table[digit[i]][4]);
		digitalWrite(segf, seg_table[digit[i]][5]);
		digitalWrite(segg, seg_table[digit[i]][6]);
		digitalWrite(segdp, seg_table[digit[i]][7]);
		if (i == 1)
		{
			digitalWrite(segdp, 1);
		}
		delay(1000);
	}

	delay(1000);
}