ENIB 2024 : Smash The Button : Différence entre versions
(→étapes de fabrication) |
(→Mettre du code Arduino) |
||
Ligne 22 : | Ligne 22 : | ||
===Mettre du code Arduino=== | ===Mettre du code Arduino=== | ||
<syntaxhighlight lang="Arduino" line> | <syntaxhighlight lang="Arduino" line> | ||
− | + | ||
− | #include < | + | #include <FastLED.h> |
+ | |||
+ | #define LED_PIN 5 | ||
+ | |||
+ | #define BUZZER_PIN 7 | ||
+ | |||
+ | #define NUM_LEDS 50 | ||
+ | |||
+ | #define BUTTON_PIN1 2 | ||
+ | |||
+ | #define BUTTON_PIN2 3 | ||
+ | |||
+ | #define DELAY_MS 50 | ||
+ | |||
+ | #define RED_BUTTON_FREQ 100 // Frequency for the red button sound | ||
+ | |||
+ | #define GREEN_BUTTON_FREQ 200 // Frequency for the green button sound | ||
+ | |||
+ | CRGB leds[NUM_LEDS]; | ||
+ | |||
+ | int ledCount1 = 0; // Number of red LEDs to light up from one end | ||
+ | |||
+ | int ledCount2 = 0; // Number of green LEDs to light up from the other end | ||
+ | |||
+ | bool buttonState1 = false; | ||
+ | |||
+ | bool lastButtonState1 = false; | ||
+ | |||
+ | bool buttonState2 = false; | ||
+ | |||
+ | bool lastButtonState2 = false; | ||
+ | |||
+ | bool isRedButtonPressed = false; | ||
+ | |||
+ | bool isGreenButtonPressed = false; | ||
void setup() { | void setup() { | ||
− | + | ||
+ | FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); | ||
+ | |||
+ | pinMode(BUTTON_PIN1, INPUT_PULLUP); | ||
+ | |||
+ | pinMode(BUTTON_PIN2, INPUT_PULLUP); | ||
+ | |||
+ | pinMode(BUZZER_PIN, OUTPUT); | ||
} | } | ||
void loop() { | void loop() { | ||
− | // | + | |
+ | buttonState1 = digitalRead(BUTTON_PIN1) == LOW; | ||
+ | |||
+ | buttonState2 = digitalRead(BUTTON_PIN2) == LOW; | ||
+ | |||
+ | if (buttonState1 != lastButtonState1) { | ||
+ | |||
+ | if (buttonState1) { | ||
+ | |||
+ | // Button 1 is pressed | ||
+ | |||
+ | ledCount1++; | ||
+ | |||
+ | if (ledCount1 > NUM_LEDS) { | ||
+ | |||
+ | ledCount1 = NUM_LEDS; | ||
+ | |||
+ | } | ||
+ | |||
+ | isRedButtonPressed = true; | ||
+ | |||
+ | tone(BUZZER_PIN, RED_BUTTON_FREQ); // Play the red button sound | ||
+ | |||
+ | } | ||
+ | |||
+ | lastButtonState1 = buttonState1; | ||
+ | |||
+ | } | ||
+ | |||
+ | if (buttonState2 != lastButtonState2) { | ||
+ | |||
+ | if (buttonState2) { | ||
+ | |||
+ | // Button 2 is pressed | ||
+ | |||
+ | ledCount2++; | ||
+ | |||
+ | if (ledCount2 > NUM_LEDS) { | ||
+ | |||
+ | ledCount2 = NUM_LEDS; | ||
+ | |||
+ | } | ||
+ | |||
+ | isGreenButtonPressed = true; | ||
+ | |||
+ | tone(BUZZER_PIN, GREEN_BUTTON_FREQ); // Play the green button sound | ||
+ | |||
+ | } | ||
+ | |||
+ | lastButtonState2 = buttonState2; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | // Turn off all LEDs | ||
+ | |||
+ | FastLED.clear(); | ||
+ | |||
+ | |||
+ | |||
+ | // Light up the specified number of red LEDs from one end | ||
+ | |||
+ | for (int i = 0; i < ledCount1; i++) { | ||
+ | |||
+ | leds[i] = CRGB::Green; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | // Light up the specified number of green LEDs from the other end | ||
+ | |||
+ | for (int i = NUM_LEDS - 1; i >= NUM_LEDS - ledCount2; i--) { | ||
+ | |||
+ | leds[i] = CRGB::Red; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | if ((ledCount1 + ledCount2) >= NUM_LEDS) { | ||
+ | |||
+ | if (ledCount1 > ledCount2) { | ||
+ | |||
+ | greenChaserEffect(); | ||
+ | |||
+ | } else if (ledCount2 > ledCount1) { | ||
+ | |||
+ | redChaserEffect(); | ||
+ | |||
+ | } else { | ||
+ | |||
+ | // It's a tie | ||
+ | |||
+ | tieChaserEffect(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | // Reset the LED counts | ||
+ | |||
+ | ledCount1 = 0; | ||
+ | |||
+ | ledCount2 = 0; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | // Show the updated LED strip | ||
+ | |||
+ | FastLED.show(); | ||
+ | |||
+ | |||
+ | |||
+ | if (isRedButtonPressed || isGreenButtonPressed) { | ||
+ | |||
+ | delay(50); // Adjust this delay to control the duration of the buzzer sound | ||
+ | |||
+ | noTone(BUZZER_PIN); // Stop the buzzer sound | ||
+ | |||
+ | isRedButtonPressed = false; | ||
+ | |||
+ | isGreenButtonPressed = false; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | delay(50); // Adjust this delay to debounce the buttons and control the response speed | ||
} | } | ||
+ | |||
+ | |||
+ | |||
+ | void tieChaserEffect() { | ||
+ | |||
+ | |||
+ | |||
+ | for (int i = 0; i < NUM_LEDS / 2; i++) { | ||
+ | |||
+ | leds[i] = CRGB::Red; // Set the color of the current LED to red | ||
+ | |||
+ | FastLED.show(); // Update the LED strip | ||
+ | |||
+ | delay(DELAY_MS); // Wait for a short period | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | for (int i = NUM_LEDS; i >= NUM_LEDS / 2; i--) { | ||
+ | |||
+ | leds[i] = CRGB::Green; // Set the color of the current LED to red | ||
+ | |||
+ | FastLED.show(); // Update the LED strip | ||
+ | |||
+ | delay(DELAY_MS); // Wait for a short period | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | void greenChaserEffect() { | ||
+ | |||
+ | // Set all LEDs to green initially | ||
+ | |||
+ | for (int i = 0; i < ledCount1; i++) { | ||
+ | |||
+ | leds[i] = CRGB::Green; | ||
+ | |||
+ | } | ||
+ | |||
+ | FastLED.show(); // Update the LED strip with the initial green color | ||
+ | |||
+ | |||
+ | |||
+ | for (int i = ledCount1; i < NUM_LEDS; i++) { | ||
+ | |||
+ | leds[i] = CRGB::Green; // Set the color of the current LED to red | ||
+ | |||
+ | FastLED.show(); // Update the LED strip | ||
+ | |||
+ | delay(DELAY_MS); // Wait for a short period | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | void redChaserEffect() { | ||
+ | |||
+ | // Set all LEDs to green initially | ||
+ | |||
+ | for (int i = NUM_LEDS - 1; i >= NUM_LEDS - ledCount2; i--) { | ||
+ | |||
+ | leds[i] = CRGB::Red; | ||
+ | |||
+ | } | ||
+ | |||
+ | FastLED.show(); // Update the LED strip with the initial green color | ||
+ | |||
+ | |||
+ | |||
+ | for (int i = ledCount2; i > 0; i--) { | ||
+ | |||
+ | leds[i] = CRGB::Red; // Set the color of the current LED to red | ||
+ | |||
+ | FastLED.show(); // Update the LED strip | ||
+ | |||
+ | delay(DELAY_MS); // Wait for a short period | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
</syntaxhighlight> | </syntaxhighlight> |
Version du 30 janvier 2024 à 14:48
Titre de la fiche expérience :
Sommaire
description (résumé)
Notre équipe est composée de 4 membres : Samuel, Mathieu, Loane et Maïa.
Mettre image avec projet fini
Introduction
éventuelle vidéo
Le nom de notre jeu est Smash The Button. Il s’agit d’un jeu en un contre un. Le but est de conquérir un maximum de territoire en un minimum de temps. Le vainqueur est celui ayant le plus grand territoire.
outil et matériel
Nous avons utilisé comme matériel un WS2811 LED Strip (permettant d’obtenir un grand nombre de LED), deux boutons poussoirs l’un rouge l’autre jaune, une carte Arduino Nano, un buzzer, une batterie externe et un bouton pour lancer le jeu.
fichiers à joindre
code, ficher d'impression 3D, de découpe laser ou vinyle, ...
Mettre du code Arduino
1
2
3 #include <FastLED.h>
4
5 #define LED_PIN 5
6
7 #define BUZZER_PIN 7
8
9 #define NUM_LEDS 50
10
11 #define BUTTON_PIN1 2
12
13 #define BUTTON_PIN2 3
14
15 #define DELAY_MS 50
16
17 #define RED_BUTTON_FREQ 100 // Frequency for the red button sound
18
19 #define GREEN_BUTTON_FREQ 200 // Frequency for the green button sound
20
21 CRGB leds[NUM_LEDS];
22
23 int ledCount1 = 0; // Number of red LEDs to light up from one end
24
25 int ledCount2 = 0; // Number of green LEDs to light up from the other end
26
27 bool buttonState1 = false;
28
29 bool lastButtonState1 = false;
30
31 bool buttonState2 = false;
32
33 bool lastButtonState2 = false;
34
35 bool isRedButtonPressed = false;
36
37 bool isGreenButtonPressed = false;
38
39 void setup() {
40
41 FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
42
43 pinMode(BUTTON_PIN1, INPUT_PULLUP);
44
45 pinMode(BUTTON_PIN2, INPUT_PULLUP);
46
47 pinMode(BUZZER_PIN, OUTPUT);
48
49 }
50
51 void loop() {
52
53 buttonState1 = digitalRead(BUTTON_PIN1) == LOW;
54
55 buttonState2 = digitalRead(BUTTON_PIN2) == LOW;
56
57 if (buttonState1 != lastButtonState1) {
58
59 if (buttonState1) {
60
61 // Button 1 is pressed
62
63 ledCount1++;
64
65 if (ledCount1 > NUM_LEDS) {
66
67 ledCount1 = NUM_LEDS;
68
69 }
70
71 isRedButtonPressed = true;
72
73 tone(BUZZER_PIN, RED_BUTTON_FREQ); // Play the red button sound
74
75 }
76
77 lastButtonState1 = buttonState1;
78
79 }
80
81 if (buttonState2 != lastButtonState2) {
82
83 if (buttonState2) {
84
85 // Button 2 is pressed
86
87 ledCount2++;
88
89 if (ledCount2 > NUM_LEDS) {
90
91 ledCount2 = NUM_LEDS;
92
93 }
94
95 isGreenButtonPressed = true;
96
97 tone(BUZZER_PIN, GREEN_BUTTON_FREQ); // Play the green button sound
98
99 }
100
101 lastButtonState2 = buttonState2;
102
103 }
104
105
106
107 // Turn off all LEDs
108
109 FastLED.clear();
110
111
112
113 // Light up the specified number of red LEDs from one end
114
115 for (int i = 0; i < ledCount1; i++) {
116
117 leds[i] = CRGB::Green;
118
119 }
120
121
122
123 // Light up the specified number of green LEDs from the other end
124
125 for (int i = NUM_LEDS - 1; i >= NUM_LEDS - ledCount2; i--) {
126
127 leds[i] = CRGB::Red;
128
129 }
130
131
132
133 if ((ledCount1 + ledCount2) >= NUM_LEDS) {
134
135 if (ledCount1 > ledCount2) {
136
137 greenChaserEffect();
138
139 } else if (ledCount2 > ledCount1) {
140
141 redChaserEffect();
142
143 } else {
144
145 // It's a tie
146
147 tieChaserEffect();
148
149 }
150
151
152
153 // Reset the LED counts
154
155 ledCount1 = 0;
156
157 ledCount2 = 0;
158
159 }
160
161
162
163 // Show the updated LED strip
164
165 FastLED.show();
166
167
168
169 if (isRedButtonPressed || isGreenButtonPressed) {
170
171 delay(50); // Adjust this delay to control the duration of the buzzer sound
172
173 noTone(BUZZER_PIN); // Stop the buzzer sound
174
175 isRedButtonPressed = false;
176
177 isGreenButtonPressed = false;
178
179 }
180
181
182
183 delay(50); // Adjust this delay to debounce the buttons and control the response speed
184
185 }
186
187
188
189 void tieChaserEffect() {
190
191
192
193 for (int i = 0; i < NUM_LEDS / 2; i++) {
194
195 leds[i] = CRGB::Red; // Set the color of the current LED to red
196
197 FastLED.show(); // Update the LED strip
198
199 delay(DELAY_MS); // Wait for a short period
200
201 }
202
203
204
205 for (int i = NUM_LEDS; i >= NUM_LEDS / 2; i--) {
206
207 leds[i] = CRGB::Green; // Set the color of the current LED to red
208
209 FastLED.show(); // Update the LED strip
210
211 delay(DELAY_MS); // Wait for a short period
212
213 }
214
215 }
216
217
218
219 void greenChaserEffect() {
220
221 // Set all LEDs to green initially
222
223 for (int i = 0; i < ledCount1; i++) {
224
225 leds[i] = CRGB::Green;
226
227 }
228
229 FastLED.show(); // Update the LED strip with the initial green color
230
231
232
233 for (int i = ledCount1; i < NUM_LEDS; i++) {
234
235 leds[i] = CRGB::Green; // Set the color of the current LED to red
236
237 FastLED.show(); // Update the LED strip
238
239 delay(DELAY_MS); // Wait for a short period
240
241 }
242
243 }
244
245
246
247 void redChaserEffect() {
248
249 // Set all LEDs to green initially
250
251 for (int i = NUM_LEDS - 1; i >= NUM_LEDS - ledCount2; i--) {
252
253 leds[i] = CRGB::Red;
254
255 }
256
257 FastLED.show(); // Update the LED strip with the initial green color
258
259
260
261 for (int i = ledCount2; i > 0; i--) {
262
263 leds[i] = CRGB::Red; // Set the color of the current LED to red
264
265 FastLED.show(); // Update the LED strip
266
267 delay(DELAY_MS); // Wait for a short period
268
269 }
270
271 }
Étapes de fabrication
indiquer autant d'étape que nécessaire, chacune illustrée par des images (phot, dessins, ...)
étape 1
Inventaire de ce qui manque
étape 2
Mise en place de carte (communication)
étape 3
cablage
étape 4
soudure
étape 5
Choix matériau dessin du patron
étape 6
découpage
étape 7
assemblage + renforcement
étape 8
assemble final du prototype
troubleshouting
probleme mathieu