Skip to content

Commit 0424d6b

Browse files
committed
Add example code for lab 2
1 parent 2d1335a commit 0424d6b

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const unsigned char BUTTON_0_PIN = 49;
2+
const unsigned char BUTTON_1_PIN = 47;
3+
4+
const unsigned char LED_PINS[] = { 62, 63, 64, 65, 66, 67, 68, 69 };
5+
const unsigned int DELAY = 800;
6+
7+
void setup() {
8+
for (int i = 0; i < 8; i++) {
9+
pinMode(LED_PINS[i], OUTPUT);
10+
digitalWrite(LED_PINS[i], LOW);
11+
}
12+
13+
pinMode(BUTTON_0_PIN, INPUT);
14+
digitalWrite(BUTTON_0_PIN, HIGH);
15+
16+
pinMode(BUTTON_1_PIN, INPUT);
17+
digitalWrite(BUTTON_1_PIN, HIGH);
18+
19+
Serial.begin(9600);
20+
}
21+
22+
23+
unsigned char currentIteration = 0;
24+
unsigned char currentMode = 0;
25+
void bleep_sequential() {
26+
digitalWrite(LED_PINS[currentIteration], HIGH);
27+
delay(DELAY);
28+
digitalWrite(LED_PINS[currentIteration], LOW);
29+
currentIteration = (currentIteration + 1) % 8;
30+
}
31+
32+
void bleep_two_converge() {
33+
digitalWrite(LED_PINS[currentIteration], HIGH);
34+
digitalWrite(LED_PINS[7 - currentIteration], HIGH);
35+
delay(DELAY);
36+
digitalWrite(LED_PINS[currentIteration], LOW);
37+
digitalWrite(LED_PINS[7 - currentIteration], LOW);
38+
currentIteration = (currentIteration + 1) % 4;
39+
}
40+
41+
42+
43+
void loop() {
44+
int inByte = -1;
45+
if (Serial.available())
46+
inByte = Serial.read();
47+
48+
if (currentMode == 0) {
49+
if (inByte == 0xA1)
50+
currentMode = 1;
51+
else if (inByte == 0xB1)
52+
currentMode = 2;
53+
}
54+
55+
if (currentMode == 1)
56+
bleep_sequential();
57+
else if (currentMode == 2)
58+
bleep_two_converge();
59+
60+
if (currentIteration == 0)
61+
currentMode = 0;
62+
63+
64+
if (digitalRead(BUTTON_0_PIN) == LOW)
65+
{
66+
Serial.write(0xA1);
67+
delay(1000);
68+
}
69+
if (digitalRead(BUTTON_1_PIN) == LOW)
70+
{
71+
Serial.write(0xB1);
72+
delay(1000);
73+
}
74+
}
21.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)