Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pitches.h"
  2.  
  3. #define pinSwitch 6
  4. #define pinBuzzer 7
  5. #define pinKey1 2
  6. #define pinKey2 3
  7. #define pinKey3 4
  8. #define pinKey4 5
  9.  
  10. int zm1, zm2, zm3, zm4;
  11. int wej1;
  12.  
  13. int dzwieki[] = {NOTE_C2};
  14. int losowa;
  15. int i = 0;
  16. unsigned long czas;
  17. unsigned long czasStart;
  18. int wynik =0 ;
  19. int kod[20];
  20. int wcisniety = 0;
  21. int ton =0;
  22. int koniec = 0;
  23.  
  24. void setup() {
  25.   pinMode(pinSwitch, INPUT_PULLUP);
  26.   pinMode(pinBuzzer, OUTPUT);
  27.   pinMode(pinKey1, INPUT);
  28.   pinMode(pinKey2, INPUT);
  29.   pinMode(pinKey3, INPUT);
  30.   pinMode(pinKey4, INPUT);
  31.   Serial.begin(9600);
  32.   generuj();
  33. }
  34.  
  35. void loop() {
  36.   wcisniety = 0;
  37.   czasStart = millis();
  38.  
  39.   do {
  40.  
  41.   zm1 = digitalRead(pinKey1);
  42.   if ( zm1 == 1) {
  43.     ton = NOTE_C2;
  44.     wcisniety = 1;
  45.   }
  46.  
  47.   } while (wcisniety == 0);
  48.  
  49.   tone(pinBuzzer, ton);
  50.   delay(300);
  51.   noTone(pinBuzzer);
  52.   delay(50);
  53.  
  54.   if (ton == kod[i]) {
  55.       Serial.print(ton);
  56.       Serial.println(" OK");
  57.       i = i + 1;
  58.       delay(1000);
  59.     }
  60.     else {
  61.       Serial.print(ton);
  62.       Serial.println(" NIE");
  63.       koniecgry();
  64.     }
  65.  
  66.    if (kod[i] == 0) {
  67.       generuj();
  68.       wynik += 1;
  69.       i = 0;
  70.    }
  71. }
  72. void  tmobile() {
  73.    tone(pinBuzzer, NOTE_C4);
  74.     delay(120);
  75.     noTone(pinBuzzer);
  76.     delay(20);
  77.     tone(pinBuzzer, NOTE_C4);
  78.     delay(120);
  79.      noTone(pinBuzzer);
  80.     delay(20);
  81.     tone(pinBuzzer, NOTE_C4);
  82.     delay(120);
  83.      noTone(pinBuzzer);
  84.     delay(20);
  85.     tone(pinBuzzer, NOTE_E4);
  86.     delay(120);
  87.     noTone(pinBuzzer);
  88.     delay(20);
  89.     tone(pinBuzzer, NOTE_C4);
  90.     delay(120);
  91.      noTone(pinBuzzer);
  92.     delay(50);
  93. }
  94.  
  95. void generuj()
  96. {
  97.   losowa = random(4);
  98.   kod[i] = dzwieki[losowa];
  99.   wyswietl_kod();
  100.   odtworz();
  101. }
  102.  
  103. void odtworz()
  104. {
  105.   int dc = sizeof(kod)/2;
  106.   for (int j=0; j<dc; j++){
  107.     if (kod[j] != 0 ) {
  108.       tone(pinBuzzer, kod[j]);
  109.       delay(500);
  110.       noTone(pinBuzzer);
  111.       delay(500);
  112.     }
  113.   }
  114. }  
  115.  
  116. void wyswietl_kod(){
  117.    int dc = sizeof(kod)/2;
  118.  
  119.   for (int j=0; j<dc; j++){
  120.     Serial.print(kod[j]);
  121.     Serial.print(" ");
  122.   }
  123.   Serial.println("");
  124. }
  125.  
  126. void koniecgry() {
  127.     tmobile();
  128.     Serial.print("Twój wynik: ");
  129.     Serial.println(wynik);
  130.     Serial.println("Chcesz zagrać jeszcze raz?");
  131.     wej1 = 1;
  132.     while ( wej1 == 1 ) {
  133.       wej1 =  digitalRead(pinSwitch);
  134.     }
  135.     tmobile();
  136.     delay(1000);
  137.     memset(kod, 0, 20 * sizeof(int));
  138.     i = 0;
  139.     wynik = 0;
  140.     generuj();
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement