forked from DFRobot/DFRobot_utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keycheck.cpp
53 lines (44 loc) · 1.14 KB
/
keycheck.cpp
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
/*
* created: 2013-09-24
* by: lisper ([email protected])
* description: key check
* function: ispressed (uint16_t key, uint16_t state);
* -> for digital pin
* issticked (uint16_t key, uint16_t value);
* -> for analog pin
* issticked2 (uint16_t key, uint16_t left, uint16_t right);
* -> for analog pin
*
*/
#include "Arduino.h"
extern uint16_t key_analog_range;
extern uint16_t key_delay_time;
//digital pin key 0 or 1
boolean ispressed (uint16_t key, uint16_t state) {
if (digitalRead (key) == state) {
delay (key_delay_time);
if (digitalRead (key) == state)
return true;
}
return false;
}
//analog pin key
boolean issticked (uint16_t key, uint16_t range) {
if (abs (analogRead (key) - range) < key_analog_range) {
delay (key_delay_time);
if (abs (analogRead (key) - range) < key_analog_range)
return true;
}
return false;
}
//analog pin key
boolean issticked2 (uint16_t key, uint16_t left, uint16_t right) {
uint16_t value = analogRead (key);
if (value >= left && value < right) {
delay (key_delay_time);
value = analogRead (key);
if (value >= left && value < right)
return true;
}
return false;
}