-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-serial.ino
138 lines (119 loc) · 2.52 KB
/
arduino-serial.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <LiquidCrystal.h>
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_BL = 10;
LiquidCrystal lcd(pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
int displayTime = 5000;
void setup()
{
Serial.begin(9600);
Serial.setTimeout(5000);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB
}
// set backlight pin
pinMode(pin_BL, OUTPUT);
// turn backlight off
digitalWrite(pin_BL, LOW);
// init lcd
lcd.begin(16, 2);
// clear lcd
lcd.setCursor(0, 0);
lcd.clear();
}
void checkButton()
{
// adjust the keyrate to match
// your preferred pressing button speed
int keyRate = 1000;
int x = analogRead(0);
if (x < 1000)
{
Serial.println("ECHO: x = " + String(x));
delay(keyRate);
}
// this is analogue reading
// the threshold may differs every hardware
// feel free to tweak the value
if (x <= 50)
{
Serial.println("BTN_RIGHT_PRESSED");
}
else if (x <= 100)
{
Serial.println("BTN_UP_PRESSED");
}
else if (x <= 300)
{
Serial.println("BTN_DOWN_PRESSED");
}
else if (x <= 500)
{
Serial.println("BTN_LEFT_PRESSED");
}
else if (x <= 700)
{
Serial.println("BTN_SELECT_PRESSED");
}
}
void loop()
{
while (!Serial.available())
{
checkButton();
}
while (Serial.available() > 0)
{
String str = Serial.readStringUntil('\n');
Serial.println("ECHO: " + str);
if (str == "LCD_BL_ON")
{
digitalWrite(pin_BL, HIGH);
Serial.println("RESULT: LCD BL = ON");
}
else if (str == "LCD_BL_OFF")
{
digitalWrite(pin_BL, LOW);
Serial.println("RESULT: LCD BL = OFF");
}
else if (str.startsWith("DISP_TIME_"))
{
String timeStr = str.substring(10);
int displayTime = timeStr.toInt();
Serial.println("RESULT: DISPLAY TIME = " + String(displayTime));
}
else
{
if (str.length() > 32)
{
str = str.substring(0, 32);
}
if (str.length() > 16)
{
String str1 = str.substring(0, 16);
String str2 = str.substring(16, 32);
lcd.print(str1);
lcd.setCursor(0, 1);
lcd.print(str2);
// display time
delay(displayTime);
Serial.println("RESULT: OK");
}
else
{
lcd.print(str);
// display time
delay(displayTime);
Serial.println("RESULT: OK");
}
// clear lcd
lcd.setCursor(0, 0);
lcd.clear();
}
}
}