Skip to content

Commit 4f622b6

Browse files
committed
Add source switcher Arduino code
1 parent cf9eddd commit 4f622b6

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const int iButtonPin1 = 6;
2+
const int iButtonPin2 = 7;
3+
const int iButtonPin3 = 8;
4+
const int iRelayPin1 = 9;
5+
const int iRelayPin2 = 10;
6+
const int iIndicator1 = 2;
7+
const int iIndicator2 = 3;
8+
const int iIndicator3 = 4;
9+
10+
int iPreviousButton = -1;
11+
int iLatestButton = 1;
12+
int iButtonState1 = 1;
13+
int iButtonState2 = 1;
14+
int iButtonState3 = 1;
15+
int iState = -1;
16+
17+
int iDebounceDelay = 100;
18+
19+
void setup() {
20+
// initialize the button pins as inputs
21+
pinMode(iButtonPin1, INPUT_PULLUP);
22+
pinMode(iButtonPin2, INPUT_PULLUP);
23+
pinMode(iButtonPin3, INPUT_PULLUP);
24+
// initialize the relays as outputs
25+
pinMode(iRelayPin1, OUTPUT);
26+
pinMode(iRelayPin2, OUTPUT);
27+
// initialise the indicator pins as outputs (to the Pi)
28+
pinMode(iIndicator1, OUTPUT);
29+
pinMode(iIndicator2, OUTPUT);
30+
pinMode(iIndicator3, OUTPUT);
31+
}
32+
33+
34+
void loop() {
35+
iState = digitalRead(iButtonPin1);
36+
if (iState != iButtonState1) {
37+
delay(iDebounceDelay);
38+
iState = digitalRead(iButtonPin1);
39+
if (iState != iButtonState1) {
40+
iLatestButton = 1;
41+
iButtonState1 = iState;
42+
}
43+
}
44+
iState = digitalRead(iButtonPin2);
45+
if (iState != iButtonState2) {
46+
delay(iDebounceDelay);
47+
iState = digitalRead(iButtonPin2);
48+
if (iState != iButtonState2) {
49+
iLatestButton = 2;
50+
iButtonState2 = iState;
51+
}
52+
}
53+
iState = digitalRead(iButtonPin3);
54+
if (iState != iButtonState3) {
55+
delay(iDebounceDelay);
56+
iState = digitalRead(iButtonPin3);
57+
if (iState != iButtonState3) {
58+
iLatestButton = 3;
59+
iButtonState3 = iState;
60+
}
61+
}
62+
if (iLatestButton != iPreviousButton) {
63+
iPreviousButton = iLatestButton;
64+
switch(iLatestButton) {
65+
case 1:
66+
digitalWrite(iRelayPin1, LOW);
67+
digitalWrite(iRelayPin2, LOW);
68+
digitalWrite(iIndicator1, HIGH);
69+
digitalWrite(iIndicator2, LOW);
70+
digitalWrite(iIndicator3, LOW);
71+
break;
72+
case 2:
73+
digitalWrite(iRelayPin1, HIGH);
74+
digitalWrite(iRelayPin2, LOW);
75+
digitalWrite(iIndicator1, LOW);
76+
digitalWrite(iIndicator2, HIGH);
77+
digitalWrite(iIndicator3, LOW);
78+
break;
79+
case 3:
80+
digitalWrite(iRelayPin1, LOW);
81+
digitalWrite(iRelayPin2, HIGH);
82+
digitalWrite(iIndicator1, LOW);
83+
digitalWrite(iIndicator2, LOW);
84+
digitalWrite(iIndicator3, HIGH);
85+
break;
86+
default:
87+
break;
88+
}
89+
}
90+
}
91+
92+
93+
94+
95+
96+
97+
98+
99+

0 commit comments

Comments
 (0)