Skip to content

Commit 32a2b6e

Browse files
committed
reaction
1 parent e91e282 commit 32a2b6e

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

2025/links.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ <h2>🖼️ Projects</h2>
120120
<div class="link-item highlight">
121121
<a href="rgb.ino" target="_blank">RGB</a>
122122
</div>
123+
<div class="link-item highlight">
124+
<a href="reaction.ino" target="_blank">Reaction</a>
125+
</div>
123126
<div class="link-item">
124127
<a href="breakout.sb3" target="_blank">Scratch Breakout</a>
125128
<div class="description">The breakout game made during camp</div>

2025/reaction.ino

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <LiquidCrystal_I2C.h>
2+
#include <Wire.h>
3+
4+
#define LED_R 25
5+
#define LED_G 26
6+
#define LED_B 27
7+
#define BUTTON 4
8+
9+
#define SDA 13
10+
#define SCL 14
11+
12+
LiquidCrystal_I2C lcd(0x27, 16, 2);
13+
14+
void setup() {
15+
ledcAttachChannel(LED_R, 1000, 8, 0);
16+
ledcAttachChannel(LED_G, 1000, 8, 1);
17+
ledcAttachChannel(LED_B, 1000, 8, 2);
18+
19+
pinMode(BUTTON, INPUT);
20+
21+
Wire.begin(SDA, SCL);
22+
lcd.init();
23+
lcd.backlight();
24+
lcd.setCursor(0,0);
25+
lcd.print("Lets play a game");
26+
delay(1000);
27+
}
28+
29+
void setColor(byte r, byte g, byte b) {
30+
ledcWrite(LED_R, 255 - r);
31+
ledcWrite(LED_G, 255 - g);
32+
ledcWrite(LED_B, 255 - b);
33+
}
34+
35+
void loop() {
36+
lcd.setCursor(0,0);
37+
lcd.print("Press button ");
38+
lcd.setCursor(0,1);
39+
lcd.print("when green");
40+
int wait = random(3,8);
41+
for (int i = 0; i < wait; i++) {
42+
if (i % 2 == 0) {
43+
setColor(255,0,0);
44+
} else {
45+
setColor(0,0,255);
46+
}
47+
delay(400);
48+
}
49+
setColor(0,255,0);
50+
unsigned long start = millis();
51+
while (digitalRead(BUTTON) == HIGH) {
52+
delay(10);
53+
}
54+
unsigned long end = millis();
55+
setColor(0,0,0);
56+
unsigned long duration = end - start;
57+
lcd.setCursor(0,0);
58+
lcd.print("Your score: ");
59+
lcd.setCursor(0,1);
60+
lcd.print(duration);
61+
lcd.print(" ");
62+
delay(2000);
63+
}

0 commit comments

Comments
 (0)