#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int ledPin = 3;
const int inputPin = 4;
int pirState = LOW;
int val = 0;
int last_val;
const int pinX = A0;
const int pinY = A1;
const int joy_comparator = 10;
const int X_dir = 0;
const int Y_dir = 1;
int x, y;
int cur_dir_up, cur_dir_down, cur_dir_left, cur_dir_right;
int last_dir_up, last_dir_down, last_dir_left, last_dir_right;
const int max_delay = 10;
const int min_delay = 2;
int off_delay = min_delay;
int last_off_delay = min_delay;
int glitch_delay = 0;
byte pBar[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
int measure_timer;
int signal_timer;
int enable_led;
int joy_arrow (int direction)
{
if (direction == X_dir)
{
if (x > (127 + joy_comparator) && last_dir_right == LOW) {
if (off_delay > min_delay)
{
--off_delay;
}
last_dir_right = HIGH;
}
else if (x < (127 - joy_comparator))
{
if (off_delay < max_delay)
{
++off_delay;
}
last_dir_left = HIGH;
}
else
{
last_dir_right = LOW;
last_dir_left = LOW;
}
}
else
{
NULL;
}
}
void setup()
{
lcd.begin(16, 2);
lcd.createChar(0, pBar);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delay: ");
lcd.setCursor(7, 0);
lcd.print(off_delay);
measure_timer = millis();
digitalWrite(ledPin, LOW);
enable_led = 0;
}
void loop()
{
if ((measure_timer + 75) < millis())
{
x = analogRead(pinX);
y = analogRead(pinY);
x = map(x, 0, 1023, 0, 255);
y = map(y, 0, 1023, 0, 255);
joy_arrow(X_dir);
measure_timer = millis();
}
else {
}
if (last_off_delay != off_delay)
{
last_off_delay = off_delay;
lcd.clear();
for (int i = 0; i < (off_delay * 2 - 4); i++)
{
lcd.setCursor(i, 1);
lcd.write(byte(0));
}
lcd.setCursor(0, 0);
lcd.print("Delay: ");
lcd.setCursor(7, 0);
lcd.print(off_delay);
}
val = digitalRead(inputPin);
if (val == HIGH)
{
if (pirState == LOW)
{
signal_timer = millis();
enable_led = 1;
glitch_delay = off_delay;
digitalWrite(ledPin, HIGH);
pirState = HIGH;
}
}
else
{
if (pirState == HIGH)
{
pirState = LOW;
}
}
if ((signal_timer + (glitch_delay * 1000)) < millis() && enable_led == 1) {
digitalWrite(ledPin, LOW);
enable_led = 0;
}
}