-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
89 lines (72 loc) · 1.99 KB
/
Copy pathcontroller.cpp
File metadata and controls
89 lines (72 loc) · 1.99 KB
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
//////////////////////////////////////////////////////////
// <put header here>
//////////////////////////////////////////////////////////
#include <Arduino.h>
#define SHORT_DELAY 50 // 50 ms delay for button debounce
#define DEBUG false // set false before submitting!
#define BAUD_RATE 9600 // 9600
// pin numbers
const int pinButtonRight = 6;
const int pinButtonDown = 7;
const int pinButtonUp = 8;
const int pinButtonLeft = 9;
const int buttonPins[4] = { pinButtonRight, pinButtonDown, pinButtonUp, pinButtonLeft };
const int numButtonPins = sizeof(buttonPins) / sizeof(buttonPins[0]);
// track button states in an array
int pinStates[4] = { HIGH, HIGH, HIGH, HIGH };
const int numPinStates = sizeof(pinStates) / sizeof(pinStates[0]);
void setup()
{
init();
Serial.begin(BAUD_RATE); // serial comms is done over default Serial port
Serial.setTimeout(1); // TEST
#if DEBUG
Serial.println("[DEBUG] Serial setup complete.");
#endif
// set control pins and their pull-up resistors
for (int i = 0; i < numButtonPins; i++)
{
pinMode(buttonPins[i], INPUT);
digitalWrite(buttonPins[i], HIGH);
}
}
void getPinStates()
{
// update state of each pin
for (int i = 0; i < numPinStates; i++)
{
pinStates[i] = digitalRead(buttonPins[i]);
delay(SHORT_DELAY); // delay for button debounce
}
}
// comms format:
// <pin 1> , <0 for button down, 1 for button up> , <pin 2>,...,<newline>
void transmit()
{
const int numChars = 15; // 4 pins + 4 states + 7 commas = 15 characters
for (int i = 0; i < numChars; i++)
{
// i in (0, 4, 8, 12): add pin ID (0, 1, 2, 3)
if (i % 4 == 0)
Serial.print(buttonPins[i/numButtonPins]);
// i in (2, 6, 10, 14): add pin state (0, 1, 2, 3)
else if ((i+2) % 2 == 0)
Serial.print(pinStates[(i-2)/numPinStates]);
// i is odd: add comma
else if (i % 2 != 0)
Serial.print(',');
}
Serial.println();
}
int main()
{
setup();
while(true)
{
// check and update pin states
getPinStates();
// write pin states to serial
transmit();
}
return 0;
}