Work 4+5 (Temperatuuritundlik servolülitus + Interaktiivne infoekraan)

Описание

Данная программа, в зависимости от температуры перемещает серводвигатель, а так же отображает информацию на LCD экране.
30 градусов и выше, переводят сервопривод в положение 180 градусов, и отображается грустный смайлик.
Всё что ниже 30 градусов переместит сервопривод на 0 градусов (в изначальное положение) и отобразит радостный смайлик.
Движения сервопривода максимально плавные!!!

Использованые компоненты

1x 1602 LCD экран
1x потенциометр
22x стандартные одиночные провода «папа-папа»
1x 330 Ω резистор
1x термодатчик
1x Servo мотор

Код программы

#include <Servo.h>
#include <LiquidCrystal.h>

#define DEBUG

const int TEMP_PIN = 0;
Servo servo_;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

byte gradChar[8] = { 0b00110, 0b01001, 0b01001, 0b00110, 0b00000, 0b00000, 0b00000, 0b00000 };

byte sm_eye_1[8] = { 0b00000, 0b00000, 0b00000, 0b00011, 0b00011, 0b00000, 0b00000, 0b00000 };
byte sm_eye_2[8] = { 0b00000, 0b00000, 0b00000, 0b11000, 0b11000, 0b00000, 0b00000, 0b00000 };

byte sm_positive_1[8] = { 0b01111, 0b00011, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 };
byte sm_positive_2[8] = { 0b00000, 0b00000, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 };
byte sm_positive_3[8] = { 0b11110, 0b11000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 };

byte sm_negative_1[8] = { 0b00000, 0b00011, 0b01111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 };
byte sm_negative_2[8] = { 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 };
byte sm_negative_3[8] = { 0b00000, 0b11000, 0b11110, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 };

void print_degrees_lcd(float temp)
{
	//char* str_f;
	//snprintf(str_f, sizeof(str_f), "Temp: %i%c%c", temp, (char*)gradChar, 'C');
	//lcd.print(str_f);
	//lcd.printf ??
	//lcd.clear();

	lcd.setCursor(3, 0);
	lcd.print("T: ");
	lcd.print(temp);
	lcd.write(byte(0));
	lcd.print("C  ");

	if (temp < 24) {
		lcd.createChar(3, sm_positive_1);
		lcd.createChar(4, sm_positive_2);
		lcd.createChar(5, sm_positive_3);
	} else if (temp >= 25) {
		lcd.createChar(3, sm_negative_1);
		lcd.createChar(4, sm_negative_2);
		lcd.createChar(5, sm_negative_3);
	}

	lcd.setCursor(0, 0);
	lcd.write(byte(1));
	lcd.setCursor(2, 0);
	lcd.write(byte(2));
	lcd.setCursor(0, 1);
	lcd.write(byte(3));
	lcd.write(byte(4));
	lcd.write(byte(5));
}

void move_servo(float temp)
{
	int cur_pos = servo_.read();

	// 24 kraadi viib servo asendisse 0
	// 25 kraadi viib servo asendisse 180

	if (temp < 24) {
		for (int pos = cur_pos; pos > 0; pos -= 2)
			servo_.write(pos < 0 ? 0 : pos);
	} else if (temp >= 25) {
		for (int pos = cur_pos; pos < 180; pos += 2)
			servo_.write(pos > 180 ? 180 : pos);
	}
delay(1000); } float getVoltage(int pin) { return (analogRead(pin) * 0.004882814f); } void setup() { servo_.attach(11); lcd.begin(16, 2); lcd.createChar(0, gradChar); lcd.createChar(1, sm_eye_1); lcd.createChar(2, sm_eye_2); #ifdef DEBUG Serial.begin(9600); #endif delay(100); servo_.write(90); delay(1000); servo_.write(0);
delay(1000); } void loop() { float voltage = getVoltage(TEMP_PIN); float temp = (voltage - 0.5f) * 100.0f; #ifdef DEBUG float temp_F = temp * (9.0f / 5.0f) + 32.0f; Serial.print("voltage: "); Serial.print(voltage); Serial.print(" deg C: "); Serial.print(temp); Serial.print(" deg F: "); Serial.println(temp_F); #endif print_degrees_lcd(temp); move_servo(temp); }

Видеоотчет