#include #include #define VRX_PIN A0 // Arduino pin connected to VRX pin #define VRY_PIN A1 // Arduino pin connected to VRY pin #define SW_PIN 1 // Arduino pin connected to SW pin ezButton button(SW_PIN); const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); int xValue = 0; // To store value of the X axis int yValue = 0; // To store value of the Y axis int bValue = 0; // To store value of the button int buzzer = 8; int LED_G = 7; int LED_R = 6; int state = 0; int question_id = 1; int num = 0; int k = 0; int lives = 2; int score = 0; void setup() { Serial.begin(9600) ; pinMode(buzzer,OUTPUT); pinMode(LED_G, OUTPUT); pinMode(LED_R, OUTPUT); button.setDebounceTime(20); lcd.begin(16, 2); lcd.setCursor(0,0); lcd.print("Press Button to"); lcd.setCursor(0,1); lcd.print("start the game!"); } void clear_lcd(){ lcd.setCursor(0,0); lcd.print(" "); lcd.setCursor(0,1); lcd.print(" "); lcd.setCursor(0,0); } void question_picker () { switch(question_id){ case 1: q_1(); break; case 2: q_2(); break; case 3: q_3(); break; case 4: q_4(); break; case 5: q_5(); break; case 6: q_6(); break; } } void answer_select(int q){ switch(q){ case 1: a_1(num); break; case 2: a_2(num); break; case 3: a_3(num); break; case 4: a_4(num); break; case 5: a_5(num); break; case 6: a_6(num); break; } } void next() { if(num < 4){ num = num + 1; answer_select(question_id); } } void prev() { if(num > 0){ num = num - 1; answer_select(question_id); } if(num == 0){ answer_select(question_id); } } void good_ending() { if (score == 6) { clear_lcd(); lcd.print("You Won!"); lcd.setCursor(0,1); lcd.print("Perfect score!"); state = 99; }else { clear_lcd(); lcd.print("You Won!"); lcd.setCursor(0,1); lcd.print("Congrats!"); } winner(); } void check() { if (question_id == 1 && num == 3) { clear_lcd(); lcd.print("Correct!"); correct_song(); score = score + 1; }else if (question_id == 2 && num == 1) { clear_lcd(); lcd.print("Correct!"); correct_song(); score = score + 1; }else if (question_id == 3 && num == 1) { clear_lcd(); lcd.print("Correct!"); correct_song(); score = score + 1; }else if (question_id == 4 && num == 2) { clear_lcd(); lcd.print("Correct!"); correct_song(); score = score + 1; }else if (question_id == 5 && num == 4) { clear_lcd(); lcd.print("Correct!"); correct_song(); score = score + 1; }else if (question_id == 6 && num == 3) { clear_lcd(); score = score + 1; good_ending(); }else { clear_lcd(); lcd.print("Incorrect!"); lives = lives - 1; if(lives == 1){ incorrect_song(); if(question_id == 6) { good_ending(); }else{ lcd.setCursor(0,1); lcd.print("1 Life Remaining"); } }else { clear_lcd(); lcd.setCursor(0,0); lcd.print("Game Over"); lcd.setCursor(0,1); lcd.print("Final Score:"); lcd.print(score); loser(); state = 99; k=99; } } question_id = question_id + 1; } void loop() { button.loop(); delay(30); // read analog X and Y analog values xValue = analogRead(VRX_PIN); if(k == 0){ if (button.isPressed()) { if(state == 0) { question_picker(); }else if(state == 1 && num !=0) { state = 0; check(); } k = 1; } } if (button.isReleased()) k = 0; if (state == 1){ if(xValue > 700) { next(); } if(xValue < 300) { prev(); } delay(200); } } //1st---------------------------- void q_1 () { clear_lcd(); lcd.print("Cati biti are un"); lcd.setCursor(0,1); lcd.print("byte?"); question_id = 1; state = 1; num = 0; } void a_1 (int quest) { clear_lcd(); if(quest == 0) q_1(); if(quest == 1) lcd.print("A : 5"); if(quest == 2) lcd.print("B : 4"); if(quest == 3) lcd.print("C : 8"); if(quest == 4) lcd.print("D : 10"); } //2nd---------------------------- void q_2 () { clear_lcd(); lcd.print("Cati cai detin"); lcd.setCursor(0,1); lcd.print("eu?"); question_id = 2; state = 1; num = 0; } void a_2 (int quest) { clear_lcd(); if(quest == 0) q_2(); if(quest == 1) lcd.print("A : 0"); if(quest == 2) lcd.print("B : 42"); if(quest == 3) lcd.print("C : 53"); if(quest == 4) lcd.print("D : 69"); } //3rd---------------------------- void q_3 () { clear_lcd(); lcd.print("Care e cel mai"); lcd.setCursor(0,1); lcd.print("bun suc?"); question_id = 3; state = 1; num = 0;; } void a_3 (int quest) { clear_lcd(); if(quest == 0) q_3(); if(quest == 1) lcd.print("A : Pepsi Max"); if(quest == 2) lcd.print("B : Cola"); if(quest == 3) lcd.print("C : Mtn Dew"); if(quest == 4) lcd.print("D : Mirinda"); } //4th---------------------------- void q_4 () { clear_lcd(); lcd.print("Ce motor rupe"); lcd.setCursor(0,1); lcd.print("asfaltul?"); question_id = 4; state = 1; num = 0; } void a_4 (int quest) { clear_lcd(); if(quest == 0) q_4(); if(quest == 1) lcd.print("A : 1.1"); if(quest == 2) lcd.print("B : 1.4"); if(quest == 3) lcd.print("C : 2.0"); if(quest == 4) lcd.print("D : 0.9"); } //5th---------------------------- void q_5 () { clear_lcd(); lcd.print("Ghiceste"); lcd.setCursor(0,1); lcd.print("Raspunsul!"); question_id = 5; state = 1; num = 0; } void a_5 (int quest) { clear_lcd(); if(quest == 0) q_5(); if(quest == 1) lcd.print("A : NU"); if(quest == 2) lcd.print("B : Poate"); if(quest == 3) lcd.print("C : Nici asta"); if(quest == 4) lcd.print("D : Asa da"); } //6th---------------------------- void q_6 () { clear_lcd(); lcd.print("Care e simbolul"); lcd.setCursor(0,1); lcd.print("cuprului?"); question_id = 6; state = 1; num = 0; } void a_6 (int quest) { clear_lcd(); if(quest == 0) q_6(); if(quest == 1) lcd.print("A : Ku"); if(quest == 2) lcd.print("B : Ca"); if(quest == 3) lcd.print("C : Cu"); if(quest == 4) lcd.print("D : Co"); } //// SONG #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978 #define REST 0 // change this to make the song slower or faster int tempo = 160; // change this to whichever pin you want to use // notes of the moledy followed by the duration. // a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on // !!negative numbers are used to represent dotted notes, // so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!! int melody[] = { // Keyboard cat // Score available at https://musescore.com/user/142788/scores/147371 REST,1, REST,1, NOTE_C4,4, NOTE_E4,4, NOTE_G4,4, NOTE_E4,4, NOTE_C4,4, NOTE_E4,8, NOTE_G4,-4, NOTE_E4,4, NOTE_A3,4, NOTE_C4,4, NOTE_E4,4, NOTE_C4,4, NOTE_A3,4, NOTE_C4,8, NOTE_E4,-4, NOTE_C4,4, NOTE_G3,4, NOTE_B3,4, NOTE_D4,4, NOTE_B3,4, NOTE_G3,4, NOTE_B3,8, NOTE_D4,-4, NOTE_B3,4, NOTE_G3,4, NOTE_G3,8, NOTE_G3,-4, NOTE_G3,8, NOTE_G3,4, NOTE_G3,4, NOTE_G3,4, NOTE_G3,8, NOTE_G3,4, NOTE_C4,4, NOTE_E4,4, NOTE_G4,4, NOTE_E4,4, NOTE_C4,4, NOTE_E4,8, NOTE_G4,-4, NOTE_E4,4, NOTE_A3,4, NOTE_C4,4, NOTE_E4,4, NOTE_C4,4, NOTE_A3,4, NOTE_C4,8, NOTE_E4,-4, NOTE_C4,4, NOTE_G3,4, NOTE_B3,4, NOTE_D4,4, NOTE_B3,4, NOTE_G3,4, NOTE_B3,8, NOTE_D4,-4, NOTE_B3,4, NOTE_G3,-1, }; // sizeof gives the number of bytes, each int value is composed of two bytes (16 bits) // there are two values per note (pitch and duration), so for each note there are four bytes int notes = sizeof(melody) / sizeof(melody[0]) / 2; // this calculates the duration of a whole note in ms int wholenote = (60000 * 4) / tempo; int divider = 0, noteDuration = 0; void winner() { for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) { // calculates the duration of each note divider = melody[thisNote + 1]; if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } // we only play the note for 90% of the duration, leaving 10% as a pause tone(buzzer, melody[thisNote], noteDuration * 0.9); digitalWrite(LED_G, HIGH); // Wait for the specief duration before playing the next note. delay(noteDuration/2); digitalWrite(LED_G, LOW); delay(noteDuration/2); // stop the waveform generation before the next note. noTone(buzzer); } } void loser() { tone(buzzer,300 , 1000); digitalWrite(LED_R, HIGH); delay(150); digitalWrite(LED_R, LOW); delay(150); tone(buzzer,222 , 1000); digitalWrite(LED_R, HIGH); delay(150); digitalWrite(LED_R, LOW); delay(150); tone(buzzer,100 , 2000); digitalWrite(LED_R, HIGH); delay(2000); digitalWrite(LED_R, LOW); } void correct_song(){ tone(buzzer,500 , 1000); digitalWrite(LED_G, HIGH); delay(150); digitalWrite(LED_G, LOW); delay(150); tone(buzzer,600 , 1000); digitalWrite(LED_G, HIGH); delay(150); digitalWrite(LED_G, LOW); delay(150); tone(buzzer,700 , 1500); digitalWrite(LED_G, HIGH); delay(1500); digitalWrite(LED_G, LOW); } void incorrect_song(){ tone(buzzer,600 , 1000); digitalWrite(LED_R, HIGH); delay(150); digitalWrite(LED_R, LOW); delay(150); tone(buzzer,500 , 1000); digitalWrite(LED_R, HIGH); delay(150); digitalWrite(LED_R, LOW); delay(150); tone(buzzer,400 , 1500); digitalWrite(LED_R, HIGH); delay(1500); digitalWrite(LED_R, LOW); }