#include <Servo.h>
#include <LiquidCrystal.h>
#define IR_PIN 0
#define SERVO_PIN 9
Servo myServo;
LiquidCrystal lcd(12, 11, 5, 4, 3, 6);
int inputAngle = 0;
unsigned long lastCode = 0;
bool lcdReady = false;
unsigned long readNEC() {
unsigned long timeout = 100000;
// Агрессивное обнаружение сигнала
if (pulseIn(IR_PIN, LOW, timeout) < 8000) return 0;
if (pulseIn(IR_PIN, HIGH, timeout) < 4000) return 0;
unsigned long data = 0;
for (int i = 0; i < 32; i++) {
pulseIn(IR_PIN, LOW, timeout);
unsigned long mark = pulseIn(IR_PIN, HIGH, timeout);
if (mark > 1000) data |= (1UL << i);
}
return data;
}
void updateDisplay() {
lcd.setCursor(0, 1);
lcd.print(" "); // 16 пробелов
lcd.setCursor(0, 1);
if (inputAngle == 0) {
lcd.print("0 ");
} else {
lcd.print(inputAngle);
}
}
void addDigit(int d) {
inputAngle = inputAngle * 10 + d;
if (inputAngle > 180) inputAngle = 0; // Сброс при переполнении
updateDisplay();
}
void setup() {
Serial.begin(9600);
Serial.println("=== СТАРТ ===");
pinMode(IR_PIN, INPUT);
// Инициализация LCD
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Angle: ");
lcd.setCursor(0, 1);
lcd.print("0 ");
lcdReady = true;
Serial.println("LCD OK");
// Инициализация серво
myServo.attach(SERVO_PIN);
myServo.write(90);
delay(500);
Serial.println("Servo OK на 90");
}
void loop() {
unsigned long code = readNEC();
if (code != 0 && code != lastCode) {
lastCode = code;
Serial.print("КОД: 0x");
Serial.println(code, HEX);
if (code == 0xFF6897) addDigit(0);
else if (code == 0xFF30CF) addDigit(1);
else if (code == 0xFF18E7) addDigit(2);
else if (code == 0xFF7A85) addDigit(3);
else if (code == 0xFF10EF) addDigit(4);
else if (code == 0xFF38C7) addDigit(5);
else if (code == 0xFF5AA5) addDigit(6);
else if (code == 0xFF42BD) addDigit(7);
else if (code == 0xFF4AB5) addDigit(8);
else if (code == 0xFF52AD) addDigit(9);
else if (code == 0xFFB04F) {
inputAngle = 0;
updateDisplay();
Serial.println("ОЧИСТКА");
}
else if (code == 0xFFA857) {
myServo.write(inputAngle);
Serial.print("СЕРВО -> ");
Serial.println(inputAngle);
lcd.setCursor(12, 0);
lcd.print("OK ");
delay(1000);
lcd.setCursor(12, 0);
lcd.print(" ");
}
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6