Components and supplies
Arduino Oplà IoT Kit
Tools and machines
Screwdrivers
Apps and platforms
Arduino IDE
Project description
Code
code blink
cpp
.
1/* 2 Blink 3 4 Turns an LED on for one second, then off for one second, repeatedly. 5 6 Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO 7 it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to 8 the correct LED pin independent of which board is used. 9 If you want to know what pin the on-board LED is connected to on your Arduino 10 model, check the Technical Specs of your board at: 11 https://www.arduino.cc/en/Main/Products 12 13 modified 8 May 2014 14 by Scott Fitzgerald 15 modified 2 Sep 2016 16 by Arturo Guadalupi 17 modified 8 Sep 2016 18 by Colby Newman 19 20 This example code is in the public domain. 21 22 https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink 23*/ 24 25// the setup function runs once when you press reset or power the board 26void setup() { 27 // initialize digital pin LED_BUILTIN as an output. 28 pinMode(13, OUTPUT); 29} 30 31// the loop function runs over and over again forever 32void loop() { 33 digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) 34 Serial.println("Led On"); 35 delay(1000); // wait for a second 36 digitalWrite(13, LOW); // turn the LED off by making the voltage LOW 37 Serial.println("Led Off"); 38 delay(1000); // wait for a second 39}
code Symon Says
cpp
.
1/* 2 Opla Simon Says 3 4 Created by Julián Caro Linares 5 6 CC-BY-SA 7*/ 8 9// Libraries 10#include <Arduino_MKRIoTCarrier.h> 11 12 13// Definitions 14 15#define DEBUG true// Uncomment to see the Serial port debug messages 16 17#define SIMON_BLUE 0 18#define SIMON_RED 1 19#define SIMON_GREEN 3 20#define SIMON_YELLOW 4 21 22#define TOUCH_BLUE TOUCH0 23#define TOUCH_RED TOUCH1 24#define TOUCH_GREEN TOUCH3 25#define TOUCH_YELLOW TOUCH4 26 27#define BLUE_TONE 330 // MI 28#define RED_TONE 262 // DO 29#define GREEN_TONE 440 // LA 30#define YELLOW_TONE 294 // SI 31#define GAMEOVER_TONE 100 32 33// Global variables 34int difficulty = 1;// Between 1-3 where 1 is the easiest and 3 the hardest 35int sequence[30]; // 0-BLUE,1-RED,2-GREEN,3-YELLOW 36//int sequence[10] = {0,1,2,3,0,1,2,3,0,1}; // 0-BLUE,1-RED,2-GREEN,3-YELLOW --TEST SEQUENCE 37int turn = 0; 38int delay_time = 500; // Milliseconds 39bool wait_press = false; 40bool wrong_button = false; 41bool difficulty_press =false; 42 43 44 45// Objects initialization 46MKRIoTCarrier carrier; 47uint32_t gameover_color = carrier.leds.Color(0, 255, 0); 48 49 50// Functions declaration 51void color_animation(char color); // Where b-blue, r-red, g-green, y-yellow 52void gameover(); 53void difficulty_selector(); 54void new_sequence(); 55 56 57void setup() { 58 59 // Opla setup 60 CARRIER_CASE = true; 61 carrier.begin(); 62 63#ifdef DEBUG 64 // Initialize serial and wait for port to open: 65 Serial.begin(9600); 66 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found 67 delay(1500); 68#endif 69 70 // Random seed 71 while (!carrier.Light.colorAvailable()) { 72 delay(5); 73 } 74 int none; 75 int light; 76 carrier.Light.readColor(none, none, none, light); 77 randomSeed(light); // TODO create a good seed 78 79 80 // Difficulty selector 81 difficulty_selector(); 82 83 // Sequence generator 84 new_sequence(); 85 86 // LEDS Initialization 87 carrier.leds.setBrightness(10); 88 carrier.leds.setPixelColor(SIMON_BLUE, 0, 0, 255); // BLUE button GRB notation 89 carrier.leds.setPixelColor(SIMON_RED, 0, 255, 0); // RED button GRB notation 90 carrier.leds.setPixelColor(SIMON_GREEN, 255, 0, 0); // GREEN button GRB notation 91 carrier.leds.setPixelColor(SIMON_YELLOW, 255, 255, 0); // YELLOW button GRB notation 92 carrier.leds.show(); 93 94 95 // Delay before starting the game 96 delay(3000); 97} 98 99void loop() { 100 101 // Begin of the game 102 carrier.leds.clear(); 103 carrier.leds.show(); 104 carrier.display.fillScreen(ST77XX_BLACK); 105 carrier.display.setCursor(35, 100); 106 carrier.display.print("BEGIN"); 107 wrong_button = false; 108 delay(5000); 109 110 // Turn loop 111 for (int turn = 1; turn <= difficulty * 10; turn ++) { 112 113 if (wrong_button == true){ 114 #ifdef DEBUG 115 Serial.println("EXIT TURN LOOP"); 116 #endif 117 break; 118 } 119 120 #ifdef DEBUG 121 Serial.print("\nTurn: "); 122 Serial.println(turn); 123 #endif 124 125 // Prepare turn 126 if (turn < 10) { 127 carrier.display.setCursor(95, 85); 128 } 129 else { 130 carrier.display.setCursor(65, 85); 131 } 132 carrier.display.fillScreen(ST77XX_BLACK); 133 carrier.display.setTextSize(10); 134 carrier.display.print(turn); 135 136 // Machine turn 137 for (int j = 0; j < turn; j++) { 138 139 #ifdef DEBUG 140 Serial.print("Mach index: "); 141 Serial.println(j); 142 143 Serial.print("Value: "); 144 Serial.println(sequence[j]); 145 #endif 146 147 carrier.leds.clear(); 148 carrier.leds.show(); 149 150 switch (sequence[j]) { 151 case 0: // BLUE 152 color_animation('b'); 153 break; 154 case 1: // RED 155 color_animation('r'); 156 break; 157 case 2: // GREEN 158 color_animation('g'); 159 break; 160 case 3: // YELLOW 161 color_animation('y'); 162 break; 163 default: 164 #ifdef DEBUG 165 Serial.println("ERROR: Wrong sequence value"); 166 Serial.println(sequence[j]); 167 #endif 168 break; 169 } 170 171 172 } 173 174 // Player turn 175 for (int k = 0; k < turn; k++) { 176 177 if (wrong_button == true){ 178 #ifdef DEBUG 179 Serial.println("EXIT PLAYER LOOP"); 180 #endif 181 break; 182 } 183 184 #ifdef DEBUG 185 Serial.println("PLAYER TURN INDEX"); 186 Serial.println(k); 187 Serial.println("SEQUENCE VALUE: "); 188 Serial.println(sequence[k]); 189 #endif 190 191 wait_press = false; 192 193 switch (sequence[k]) { 194 195 case 0: // BLUE 196 197 #ifdef DEBUG 198 Serial.println("BLUE CASE"); 199 Serial.print("PLAYER: "); 200 Serial.println(sequence[k]); 201 #endif 202 203 while (wait_press == false) { 204 carrier.Buttons.update(); 205 206 if (carrier.Buttons.onTouchDown(TOUCH_BLUE)){ 207 #ifdef DEBUG 208 Serial.println("SUCCESS BLUE"); 209 #endif 210 delay(200); 211 color_animation('b'); 212 213 wait_press = true; 214 continue; 215 } 216 else if (carrier.Buttons.onTouchDown(TOUCH_RED) || carrier.Buttons.onTouchDown(TOUCH_GREEN) || carrier.Buttons.onTouchDown(TOUCH_YELLOW)){ 217 #ifdef DEBUG 218 Serial.println("BLUE-WRONG BUTTON"); 219 #endif 220 delay(200); 221 wait_press = true; 222 wrong_button = true; 223 gameover(); 224 break; 225 } 226 } 227 228 break; 229 case 1: // RED 230 231 #ifdef DEBUG 232 Serial.println("RED CASE"); 233 #endif 234 235 while (wait_press == false) { 236 carrier.Buttons.update(); 237 238 if (carrier.Buttons.onTouchDown(TOUCH_RED)){ 239 #ifdef DEBUG 240 Serial.println("SUCCESS RED"); 241 #endif 242 delay(200); 243 color_animation('r'); 244 245 wait_press = true; 246 continue; 247 } 248 else if (carrier.Buttons.onTouchDown(TOUCH_BLUE) || carrier.Buttons.onTouchDown(TOUCH_GREEN) || carrier.Buttons.onTouchDown(TOUCH_YELLOW)){ 249 #ifdef DEBUG 250 Serial.println("RED-WRONG BUTTON"); 251 #endif 252 delay(200); 253 wait_press = true; 254 wrong_button = true; 255 gameover(); 256 break; 257 } 258 } 259 260 break; 261 case 2: // GREEN 262 263 #ifdef DEBUG 264 Serial.println("GREEN CASE"); 265 #endif 266 267 while (wait_press == false) { 268 carrier.Buttons.update(); 269 270 if (carrier.Buttons.onTouchDown(TOUCH_GREEN)){ 271 #ifdef DEBUG 272 Serial.println("SUCCESS GREEN"); 273 #endif 274 delay(200); 275 color_animation('g'); 276 277 wait_press = true; 278 continue; 279 } 280 else if (carrier.Buttons.onTouchDown(TOUCH_RED) || carrier.Buttons.onTouchDown(TOUCH_BLUE) || carrier.Buttons.onTouchDown(TOUCH_YELLOW)){ 281 #ifdef DEBUG 282 Serial.println("GREEN-WRONG BUTTON"); 283 #endif 284 delay(200); 285 wait_press = true; 286 wrong_button = true; 287 gameover(); 288 break; 289 } 290 } 291 292 break; 293 294 case 3: // YELLOW 295 296 #ifdef DEBUG 297 Serial.println("YELLOW CASE"); 298 #endif 299 300 while (wait_press == false) { 301 carrier.Buttons.update(); 302 303 if (carrier.Buttons.onTouchDown(TOUCH_YELLOW)){ 304 #ifdef DEBUG 305 Serial.println("SUCCESS YELLOW"); 306 #endif 307 delay(200); 308 color_animation('y'); 309 310 wait_press = true; 311 continue; 312 } 313 else if (carrier.Buttons.onTouchDown(TOUCH_RED) || carrier.Buttons.onTouchDown(TOUCH_GREEN) || carrier.Buttons.onTouchDown(TOUCH_BLUE)){ 314 #ifdef DEBUG 315 Serial.println("YELLOW-WRONG BUTTON"); 316 #endif 317 delay(200); 318 wait_press = true; 319 wrong_button = true; 320 gameover(); 321 break; 322 } 323 } 324 325 break; 326 } 327 328 } 329 } 330 331 // End catch 332 carrier.leds.clear(); 333 carrier.leds.setPixelColor(2, 255, 0, 0); 334 carrier.leds.show(); 335 carrier.display.fillScreen(ST77XX_BLACK); 336 carrier.display.setTextSize(6); 337 carrier.display.setCursor(70, 100); 338 carrier.display.print("END"); 339 340 while (true) { 341 carrier.Buttons.update(); 342 343 if (carrier.Buttons.onTouchUp(TOUCH2)) { 344 carrier.Buzzer.sound(750); 345 delay(250); 346 carrier.Buzzer.noSound(); 347 carrier.display.fillScreen(ST77XX_BLACK); 348 carrier.display.setTextSize(6); 349 carrier.display.setCursor(70, 100); 350 carrier.display.print("NEW"); 351 delay(3000); 352 break; 353 } 354 delay(500); 355 } 356 357 // New game sequence 358 difficulty_selector(); 359 new_sequence(); 360 delay(2000); // Time to see the difficulty selected 361 362 363 364} 365 366 367void difficulty_selector(){ 368 369 carrier.leds.setBrightness(10); 370 carrier.leds.setPixelColor(1, 255, 0, 0); // EASY 371 carrier.leds.setPixelColor(2, 0, 0, 255); // NORMAL 372 carrier.leds.setPixelColor(3, 0, 255, 0); // HARD 373 carrier.leds.show(); 374 carrier.display.setRotation(0); 375 carrier.display.setTextSize(3); 376 carrier.display.fillScreen(ST77XX_BLACK); 377 carrier.display.setCursor(35, 110); 378 carrier.display.print("DIFFICULTY"); 379 380 difficulty_press = false; 381 while (difficulty_press == false) { 382 carrier.Buttons.update(); 383 if (carrier.Buttons.onTouchDown(TOUCH1)){ 384 difficulty = 1; 385 difficulty_press = true; 386 } 387 else if (carrier.Buttons.onTouchDown(TOUCH2)){ 388 difficulty = 2; 389 difficulty_press = true; 390 } 391 else if (carrier.Buttons.onTouchDown(TOUCH3)){ 392 difficulty = 3; 393 difficulty_press = true; 394 } 395 delay(100); 396 } 397} 398 399void new_sequence(){ 400 401 carrier.display.fillScreen(ST77XX_BLACK); 402 carrier.display.setRotation(0); 403 carrier.display.setTextSize(6); 404 405 switch (difficulty) { 406 default: 407 //Like case 1 408 case 1: 409 carrier.display.setCursor(55, 100); 410 carrier.display.print("EASY"); 411 412 for (int i = 0; i < 10; i++) { 413 sequence[i] = random(0, 4); 414 #ifdef DEBUG 415 Serial.print("Sequence:"); 416 Serial.println(i); 417 Serial.println(sequence[i]); 418 #endif 419 } 420 break; 421 case 2: 422 carrier.display.setCursor(17, 100); 423 carrier.display.print("NORMAL"); 424 425 for (int i = 0; i < 20; i++) { 426 sequence[i] = random(0, 4); 427 #ifdef DEBUG 428 Serial.print("Sequence:"); 429 Serial.println(i); 430 Serial.println(sequence[i]); 431 #endif 432 } 433 break; 434 case 3: 435 carrier.display.setCursor(50, 100); 436 carrier.display.print("HARD"); 437 438 for (int i = 0; i < 30; i++) { 439 sequence[i] = random(0, 4); 440 #ifdef DEBUG 441 Serial.print("Sequence:"); 442 Serial.println(i); 443 Serial.println(sequence[i]); 444 #endif 445 } 446 break; 447 } 448} 449 450void color_animation(char color){ 451 452 switch (color){ 453 454 case 'b': 455 // ON 456 carrier.leds.setPixelColor(SIMON_BLUE, 0, 0, 255); 457 carrier.leds.show(); 458 //carrier.display.fillScreen(ST77XX_BLUE); 459 carrier.Buzzer.sound(BLUE_TONE); 460 delay(delay_time); 461 462 // OFF 463 carrier.Buzzer.noSound(); 464 carrier.leds.clear(); 465 carrier.leds.show(); 466 //carrier.display.fillScreen(ST77XX_BLACK); 467 delay(delay_time); 468 469 break; 470 471 case 'r': 472 // ON 473 carrier.leds.setPixelColor(SIMON_RED, 0, 255, 0); 474 carrier.leds.show(); 475 //carrier.display.fillScreen(ST77XX_RED); 476 carrier.Buzzer.sound(RED_TONE); 477 delay(delay_time); 478 479 // OFF 480 carrier.Buzzer.noSound(); 481 carrier.leds.clear(); 482 carrier.leds.show(); 483 //carrier.display.fillScreen(ST77XX_BLACK); 484 delay(delay_time); 485 486 break; 487 488 case 'g': 489 // ON 490 carrier.leds.setPixelColor(SIMON_GREEN, 255, 0, 0); 491 carrier.leds.show(); 492 //carrier.display.fillScreen(ST77XX_GREEN); 493 carrier.Buzzer.sound(GREEN_TONE); 494 delay(delay_time); 495 496 // OFF 497 carrier.Buzzer.noSound(); 498 carrier.leds.clear(); 499 carrier.leds.show(); 500 //carrier.display.fillScreen(ST77XX_BLACK); 501 delay(delay_time); 502 503 break; 504 505 case 'y': 506 // ON 507 carrier.leds.setPixelColor(SIMON_YELLOW, 255, 255, 0); 508 carrier.leds.show(); 509 //carrier.display.fillScreen(ST77XX_YELLOW); 510 carrier.Buzzer.sound(YELLOW_TONE); 511 delay(delay_time); 512 513 // OFF 514 carrier.Buzzer.noSound(); 515 carrier.leds.clear(); 516 carrier.leds.show(); 517 //carrier.display.fillScreen(ST77XX_BLACK); 518 delay(delay_time); 519 520 break; 521 522 } 523} 524 525void gameover(){ 526 527 #ifdef DEBUG 528 Serial.println("GAMEOVER"); 529 #endif 530 // ON 531 carrier.leds.fill(gameover_color, 0, 5); 532 carrier.leds.show(); 533 carrier.Buzzer.sound(GAMEOVER_TONE); 534 carrier.display.fillScreen(ST77XX_RED); 535 carrier.display.setTextSize(4); 536 carrier.display.setCursor(25, 105); 537 carrier.display.print("GAMEOVER"); 538 539 delay(2000); 540 541 // OFF 542 carrier.Buzzer.noSound(); 543 //carrier.display.fillScreen(ST77XX_BLACK); 544 delay(5000); 545}
Comments
Only logged in users can leave comments
mircemk
0 Followers
•0 Projects
Table of contents
Intro
0
0