-
Notifications
You must be signed in to change notification settings - Fork 14
/
pc_momentary_common_cathode.ino
180 lines (158 loc) · 5.69 KB
/
pc_momentary_common_cathode.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
String inByte; // reading serial for powershell commands
const int button1Pin = 2; // pushbutton 1 pin
const int unmewtledPin = 7;
const int mewtledPin = 4;
const int RED_PIN = 4; // LED for visualizer
const int GREEN_PIN = 9; // LED for visualizer
const int BLUE_PIN = 7; // LED for visualizer
int toggleState = 1;
unsigned long lastLedDisplayUpdate;
float lastVolume;
int ledDisplay;
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT_PULLUP);
// Set up the LEDs pin to be an outputs:
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Set up the serial bus
Serial.begin(9600);
Serial.setTimeout(50); //decreases parseInt timeout
}
void loop()
{
int button1State, button2State; // variables to hold the pushbutton states
button1State = digitalRead(button1Pin);
if (Serial.available() > 0) {
inByte = Serial.readString();
float inBytefloat = inByte.toFloat();
ledDisplay = String(inByte).toFloat();
if (ledDisplay == 0) {
digitalWrite(unmewtledPin, LOW);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, HIGH);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
else if (ledDisplay == 1) {
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}
else if (ledDisplay == 2) {
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
else if (ledDisplay == 101) {
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(mewtledPin, HIGH);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(300);
digitalWrite(unmewtledPin, LOW);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(200);
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(mewtledPin, HIGH);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(300);
digitalWrite(unmewtledPin, LOW);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(200);
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(mewtledPin, HIGH);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(300);
digitalWrite(unmewtledPin, LOW);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
else if ((ledDisplay % 3) == 0) {
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}
else if (ledDisplay % 3 == 1) {
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
else if (ledDisplay % 3 == 2) {
digitalWrite(unmewtledPin, HIGH);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
}
//timestamps last MEWT display update
lastLedDisplayUpdate = millis();
}
//if it's been over 1 second since last MEWT display update, turn off LEDs to avoid confusion
if(millis() - lastLedDisplayUpdate > 1000) {
digitalWrite(unmewtledPin, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
if (button1State == LOW) // if we're pushing button 1
{
// turn on LED to indicate button pressed, turn off all other LEDs
digitalWrite(unmewtledPin, LOW);
digitalWrite(mewtledPin, LOW);
digitalWrite(RED_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
// wait for bounce
delay(200);
//read button state again to see if button has been released or if it's still depressed
button1State = digitalRead(button1Pin);
//if button has been released, then check for togglestate change
if (button1State == HIGH) {
if (toggleState == 0) {
toggleState = 1;
}
else {
toggleState = 0;
}
Serial.println(toggleState);
}
}
}