#include <LiquidCrystal.h>
#include "led_control.h"
#include "joystick_control.h"
#include "sensors_handle.h"
// led
Led led(5, LOW);
// lcd
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
const int LCD_ROWS = 2;
const int LCD_COLS = 16;
byte full_char[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
};
// joystick
Joystick joy(A1, DEFAULT, A2, DEFAULT, 2, HIGH);
const int THRESHOLD = 100;
const int CENTER_POS = 512;
// lcd joystick
int time_set = 5;
int prev_time_set = 0;
int time_set_limit[] = {2, 10};
// sensor
SensorPirMotion sensor(3);
size_t timer;
bool is_on = false;
void setup() {
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
lcd.print("Time set [2, 10]:");
lcd.createChar(0, full_char);
Serial.begin(9600); // дублирование выхода на COM порт
}
void UpdateScreen() {
if (time_set != prev_time_set) {
lcd.setCursor(0,1);
lcd.print(" ");
for (int i = 0; i < time_set; ++i) {
lcd.setCursor(i, 1);
lcd.write(byte(0));
}
lcd.setCursor(11, 1);
lcd.print(time_set);
lcd.print(" s");
prev_time_set = time_set;
}
}
void CheckMotion() {
if (sensor.read() == HIGH && !is_on) {// есть сигнал от датчика
led.switch_on();
timer = millis();
is_on = true;
Serial.println("Motion detected!");
}
if (is_on && (timer + time_set * 1000 < millis())) {
led.switch_off();
is_on = false;
Serial.println("Motion ended!");
}
}
void HandleButton() {
if (!joy.button_is_changed()) {
return;
}
if (time_set + 1 > time_set_limit[1]) {
time_set = time_set_limit[0];
} else {
++time_set;
}
}
void HandleStickHorz() {
int pos_x = joy.read_horz() - CENTER_POS;
if (abs(pos_x) < THRESHOLD) {
return;
}
if (pos_x > 0 && time_set != time_set_limit[0]) {
--time_set;
} else if (pos_x < 0 && time_set != time_set_limit[1]){
++time_set;
}
}
void HandleStickVert() {
int pos_y = joy.read_vert() - CENTER_POS;
if (abs(pos_y) < THRESHOLD) {
return;
}
if (pos_y > 0 && time_set != time_set_limit[1]) {
++time_set;
} else if (pos_y < 0 && time_set != time_set_limit[0]){
--time_set;
}
}
void HandleJoystick() {
HandleButton();
HandleStickHorz();
HandleStickVert();
}
void MotionControl() {
CheckMotion();
HandleJoystick();
UpdateScreen();
}
void loop() {
MotionControl();
delay(100);
}