forked from ducklin5/MusicBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialWritter.cpp
103 lines (84 loc) · 2.35 KB
/
serialWritter.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
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
// ---------------------------------------------------
// Name: Azeez Abass
// ID: 1542780
// Name: Matthew Braun
// ID: 1497171
// CMPUT 274 EA1, Fall 2018
// Project: ZMat 2000 (SerialWriter)
// ---------------------------------------------------
#include <Arduino.h>
// State digital and analog pins to be used
// Initialize all variable
int dPins[] = {2,3,4,5,6,7,8,9,10,11,12,13,22,23,25};
const int dPinCount = sizeof(dPins)/sizeof(dPins[0]);
int dpinStates[dPinCount];
int aPins[] = {0,14,15};
const int aPinCount = sizeof(aPins)/sizeof(aPins[0]);
int apinStates[aPinCount];
int intRound(int num, int factor){
/**
* Quick round function for analog smoothing
* Inputs:
* num (int): number to round
* factor (int): factor to round by
**/
int result = num + factor/2;
result -= result % factor;
return result;
}
void init_aPins(){
// Initializes Stated Analog pins
for (int i = 0; i < aPinCount; i++) {
int pin = aPins[i];
pinMode(pin, INPUT);
}
}
void init_dPins(){
// Initializes Stated Digital pins
for (int i = 0; i < dPinCount; i++) {
int pin = dPins[i];
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
}
void setup() {
// Initialize the arduino, analog pins and digital pins
init();
Serial.begin(19200);
init_aPins(); init_dPins();
}
void update_aPins(){
// print the analog pin and value to serial if its has changed
for (int i = 0; i < aPinCount; i++) {
int pin = aPins[i];
int newVal = intRound(analogRead(pin), 500);
if (newVal != apinStates[i]) {
Serial.print("A");
Serial.print(pin);
Serial.print(":");
Serial.println(newVal);
apinStates[i] = newVal;
}
}
}
void update_dPins(){
// print the digital pin and value to serial if its has changed
for (int i = 0; i < dPinCount; i++) {
int pin = dPins[i];
int newVal = not digitalRead(pin); // invert this fo push buttons
if (newVal != dpinStates[i]) {
Serial.print(pin);
Serial.print(":");
Serial.println(newVal);
dpinStates[i] = newVal;
}
}
}
int main() {
setup();
while (true) {
// update all digital pins
update_aPins(); update_dPins();
}
return 0;
}