Advertisement
Szerelo

Wire Dexterity game

Jan 5th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.82 KB | None | 0 0
  1. // Wire dexterity game with 2 difficult level
  2. // There are 2 wire loops, one on the left and one on the right.
  3. // The wire loop must be moved from side to side and back so that it does not touch the wire running in the center.
  4. // If you touch it, a counter will indicate hit.
  5. // That when we get back to the starting point, we will indicate this by the wire reels on the opposite side.
  6. // We get the final score and after a while the game starts again.
  7. // Have fun!
  8.  
  9. #include <Arduino.h>
  10. #include <TM1637Display.h>
  11.  
  12. #define CLK 8
  13. #define DIO 9
  14. #define easyPin 7
  15. #define hardPin 6
  16. #define tonePin 5
  17.  
  18. bool easy=false, play=false;
  19. uint8_t counter;
  20.  
  21. const uint8_t SEG_DONE[] = {
  22.     SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  23.     SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  24.     SEG_C | SEG_E | SEG_G,                           // n
  25.     SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
  26.     };
  27.  
  28. const uint8_t SEG_EASY[] = {
  29.   SEG_A | SEG_F | SEG_G | SEG_E | SEG_D,           // E
  30.   SEG_A | SEG_B | SEG_C | SEG_G | SEG_E | SEG_F,   // A
  31.   SEG_A | SEG_F | SEG_G | SEG_C | SEG_D,           // S
  32.   SEG_F | SEG_G | SEG_B | SEG_C | SEG_D            // Y
  33.   };
  34.  
  35. const uint8_t SEG_HARD[] = {
  36.   SEG_B | SEG_F | SEG_G | SEG_E | SEG_C,           // H
  37.   SEG_A | SEG_B | SEG_C | SEG_G | SEG_E | SEG_F,   // A
  38.   SEG_E | SEG_G,                                   // r
  39.   SEG_B | SEG_C | SEG_D | SEG_E | SEG_G            // d
  40.   };
  41.  
  42. const uint8_t SEG_HI[] = {
  43.   0,                                               //
  44.   SEG_B | SEG_F | SEG_G | SEG_E | SEG_C,           // H
  45.   SEG_B | SEG_C,                                   // I
  46.   0                                                //
  47.   };
  48.  
  49. const uint8_t SEG_SEG[] = { SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F};
  50. uint8_t SEG_WE[] = { SEG_A, SEG_A, SEG_A, SEG_A };
  51.  
  52. TM1637Display display(CLK, DIO);
  53.  
  54. void setup() {
  55.   Serial.begin(115200);
  56.   Serial.println("Start ...");
  57.   pinMode(easyPin,INPUT_PULLUP);
  58.   pinMode(hardPin,INPUT_PULLUP);
  59.   pinMode(tonePin,OUTPUT);
  60.   pinMode(4,OUTPUT);        // speaker GND
  61.   display.setBrightness(0x0f);
  62.   display.setSegments(SEG_HI);
  63.   delay(1000);
  64.   wfs();
  65. }
  66.  
  67. void wfs() { //waiting for start
  68.   display.clear();
  69.   counter=0;
  70.   easy=false;
  71.   while (!play) {
  72.     if (digitalRead(easyPin)==0 or digitalRead(hardPin)==0) {
  73.       tone(tonePin,440,200);
  74.       if (digitalRead(easyPin)==0) easy=true;
  75.       play=true;
  76.     }
  77.     SEG_WE[0]=SEG_SEG[counter];
  78.     SEG_WE[1]=SEG_SEG[counter];
  79.     SEG_WE[2]=SEG_SEG[counter];
  80.     SEG_WE[3]=SEG_SEG[counter];
  81.     display.setSegments(SEG_WE);
  82.     delay(200);
  83.     counter++;
  84.     if (counter==6) counter=0;
  85.   }
  86.   if (easy) {
  87.     display.setSegments(SEG_EASY);
  88.     delay(1000);
  89.   } else {
  90.     display.setSegments(SEG_HARD);
  91.     delay(1000);
  92.   }
  93.   delay(1000);
  94.   display.showNumberDec(3333, true);
  95.   tone(tonePin,440,200);
  96.   delay(500);
  97.   display.showNumberDec(2222, true);
  98.   tone(tonePin,500,200);
  99.   delay(500);
  100.   display.showNumberDec(1111, true);
  101.   tone(tonePin,600,200);
  102.   delay(500);
  103.   display.clear();
  104.   counter=0;
  105. }
  106.  
  107. void loop() {
  108.   display.showNumberDec(counter, false);
  109.   if (play==false) {
  110.     wfs();
  111.   }
  112.   if (play==true and easy==true and digitalRead(easyPin)==LOW) { // Easy mode, and detect easy pin contact, --> miss
  113.     counter++;
  114.     tone(tonePin,800,200);
  115.     delay(500);
  116.   }
  117.   if (play==true and easy==true and digitalRead(hardPin)==LOW) { // Easy mode, and detect hard pin contact, --> end game
  118.     tone(tonePin,440,200);
  119.     display.clear();
  120.     display.setSegments(SEG_DONE);
  121.     delay(1000);
  122.     display.setSegments(SEG_EASY);
  123.     delay(500);
  124.     display.showNumberDec(counter, false);
  125.     delay(500);
  126.     display.clear();
  127.     display.setSegments(SEG_EASY);
  128.     delay(500);
  129.     display.showNumberDec(counter, false);
  130.     delay(500);
  131.     display.clear();
  132.     display.setSegments(SEG_EASY);
  133.     delay(500);
  134.     display.showNumberDec(counter, false);
  135.     delay(500);
  136.     display.clear();
  137.     delay(500);
  138.     play=false;
  139.   }
  140.   if (play==true and easy==false and digitalRead(hardPin)==LOW) { // Hard mode, and detect hard pin contact, --> miss
  141.     counter++;
  142.     tone(tonePin,800,200);
  143.     delay(500);
  144.   }
  145.   if (play==true and easy==false and digitalRead(easyPin)==LOW) { // Hard mode, and detect easy pin contact, --> end game
  146.     tone(tonePin,440,200);
  147.     display.clear();
  148.     display.setSegments(SEG_DONE);
  149.     delay(1000);
  150.     display.setSegments(SEG_HARD);
  151.     delay(500);
  152.     display.showNumberDec(counter, false);
  153.     delay(500);
  154.     display.clear();
  155.     display.setSegments(SEG_HARD);
  156.     delay(500);
  157.     display.showNumberDec(counter, false);
  158.     delay(500);
  159.     display.clear();
  160.     display.setSegments(SEG_HARD);
  161.     delay(500);
  162.     display.showNumberDec(counter, false);
  163.     delay(500);
  164.     display.clear();
  165.     delay(500);
  166.     play=false;
  167.   }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement