PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Examples
Devices
Interface
Keypad 4x4 Countdown Timer.c
1
/*
2
Keypad 4x4 Countdown Timer.c
3
Builds a number with multiple key presses, then counts down to zero from that
4
number.
5
*/
6
7
#include "
simpletools.h
"
// Libraries simpletools & keypad
8
#include "
keypad.h
"
9
10
int
countdown(
int
t);
// Function prototypes
11
int
getNumber(
void
);
12
13
int
rows[4] = {7, 6, 5, 4};
// Row I/O pins (top to bottom)
14
int
cols[4] = {3, 2, 1, 0};
// Column I/O pins (left to right)
15
16
int
values[16] = { 1, 2, 3,
'A'
,
// Values for each key in the 4x4
17
4, 5, 6,
'B'
,
18
7, 8, 9,
'C'
,
19
'*'
, 0,
'#'
,
'D'
};
20
21
int
main
()
// Main function
22
{
23
keypad_setup
(4, 4, rows, cols, values);
// Setup dimensions, keypad arrays
24
25
while
(1)
// Main loop
26
{
27
print
(
"Type number of seconds, "
);
// User prompt
28
print
(
"then press #.\r"
);
29
30
int
number =
keypad_getNumber
();
// Get number from keypad
31
32
int
result = countdown(number);
// Start the countdown
33
34
if
(result == 0)
// If counted down to 0
35
{
36
print
(
"Countdown complete!\r"
);
// ...display success
37
}
38
else
// If not
39
{
40
print
(
"Countdown terminated"
);
// ...display terminated
41
print
(
" at %d seconds.\r"
, result);
42
while
(
keypad_read
() != -1);
// Wait for key release
43
}
44
}
45
}
46
47
int
countdown(
int
t)
// t = countdown time in s
48
{
49
int
key;
// keypad reading variable
50
51
do
52
{
// before starting countdown
53
key =
keypad_read
();
54
}
55
while
(key != -1);
// Wait for key to be released
56
57
while
(1)
// Keep counting down until 0
58
{
59
print
(
"t = %d\r"
, t);
// Display seconds
60
if
(
keypad_read
() != -1)
break
;
61
if
(t == 0)
break
;
// t == 0 exits loop
62
pause
(1000);
// Delay 1 s
63
t--;
// Subtract 1 from seconds
64
}
65
66
return
t;
// After loop, return the result
67
}
68
keypad_setup
void keypad_setup(int rowCount, int columnCount, int *rowPinCons, int *columnPinCons, int *buttonValues)
Set up the row and column dimensions, pins, and key values.
Definition:
keypad.c:24
pause
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
Definition:
libws2812.c:125
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
keypad_getNumber
int keypad_getNumber(void)
Get a number from the keypad. The number will be retured as soon as a non-numeric key is pressed and ...
Definition:
getNumber.c:18
keypad_read
int keypad_read(void)
Returns the first key pressed, or -1 if no key is pressed.
Definition:
keypad.c:42
print
int print(const char *format,...) __attribute__((format(printf
Print format "..." args to the default simple terminal device. The output is limited to 256 bytes.
keypad.h
Scans matrix keypad and returns a unique value for each key that is pressed. For an example circuit a...
main
int main(void)
Definition:
GraphicsTest.c:20
Generated on Fri May 20 2022 16:48:04 for PropWare by
1.8.17