Guest User

Untitled

a guest
Feb 27th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.58 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <U8x8lib.h>
  3. #include "LIS3DHTR.h"
  4. LIS3DHTR<TwoWire> LIS; //IIC
  5.  
  6.  
  7. //Rotary potentiometer variables
  8. #define WIRE Wire
  9. #define ROTARY_ANGLE_SENSOR A0
  10. #define LED 3  
  11. #define ADC_REF 5
  12. #define GROVE_VCC 5
  13. #define FULL_ANGLE 300
  14.  
  15. //Musical notes for buzzer song
  16. #define NOTE_B0  31
  17. #define NOTE_C1  33
  18. #define NOTE_CS1 35
  19. #define NOTE_D1  37
  20. #define NOTE_DS1 39
  21. #define NOTE_E1  41
  22. #define NOTE_F1  44
  23. #define NOTE_FS1 46
  24. #define NOTE_G1  49
  25. #define NOTE_GS1 52
  26. #define NOTE_A1  55
  27. #define NOTE_AS1 58
  28. #define NOTE_B1  62
  29. #define NOTE_C2  65
  30. #define NOTE_CS2 69
  31. #define NOTE_D2  73
  32. #define NOTE_DS2 78
  33. #define NOTE_E2  82
  34. #define NOTE_F2  87
  35. #define NOTE_FS2 93
  36. #define NOTE_G2  98
  37. #define NOTE_GS2 104
  38. #define NOTE_A2  110
  39. #define NOTE_AS2 117
  40. #define NOTE_B2  123
  41. #define NOTE_C3  131
  42. #define NOTE_CS3 139
  43. #define NOTE_D3  147
  44. #define NOTE_DS3 156
  45. #define NOTE_E3  165
  46. #define NOTE_F3  175
  47. #define NOTE_FS3 185
  48. #define NOTE_G3  196
  49. #define NOTE_GS3 208
  50. #define NOTE_A3  220
  51. #define NOTE_AS3 233
  52. #define NOTE_B3  247
  53. #define NOTE_C4  262
  54. #define NOTE_CS4 277
  55. #define NOTE_D4  294
  56. #define NOTE_DS4 311
  57. #define NOTE_E4  330
  58. #define NOTE_F4  349
  59. #define NOTE_FS4 370
  60. #define NOTE_G4  392
  61. #define NOTE_GS4 415
  62. #define NOTE_A4  440
  63. #define NOTE_AS4 466
  64. #define NOTE_B4  494
  65. #define NOTE_C5  523
  66. #define NOTE_CS5 554
  67. #define NOTE_D5  587
  68. #define NOTE_DS5 622
  69. #define NOTE_E5  659
  70. #define NOTE_F5  698
  71. #define NOTE_FS5 740
  72. #define NOTE_G5  784
  73. #define NOTE_GS5 831
  74. #define NOTE_A5  880
  75. #define NOTE_AS5 932
  76. #define NOTE_B5  988
  77. #define NOTE_C6  1047
  78. #define NOTE_CS6 1109
  79. #define NOTE_D6  1175
  80. #define NOTE_DS6 1245
  81. #define NOTE_E6  1319
  82. #define NOTE_F6  1397
  83. #define NOTE_FS6 1480
  84. #define NOTE_G6  1568
  85. #define NOTE_GS6 1661
  86. #define NOTE_A6  1760
  87. #define NOTE_AS6 1865
  88. #define NOTE_B6  1976
  89. #define NOTE_C7  2093
  90. #define NOTE_CS7 2217
  91. #define NOTE_D7  2349
  92. #define NOTE_DS7 2489
  93. #define NOTE_E7  2637
  94. #define NOTE_F7  2794
  95. #define NOTE_FS7 2960
  96. #define NOTE_G7  3136
  97. #define NOTE_GS7 3322
  98. #define NOTE_A7  3520
  99. #define NOTE_AS7 3729
  100. #define NOTE_B7  3951
  101. #define NOTE_C8  4186
  102. #define NOTE_CS8 4435
  103. #define NOTE_D8  4699
  104. #define NOTE_DS8 4978
  105. #define REST      0
  106.  
  107. U8X8_SSD1306_128X64_ALT0_HW_I2C u8x8(U8X8_PIN_NONE);
  108. int sensorValue = 0; // value read from the port
  109. int outputValue = 0; // value output to the PWM (analog out)
  110. int ledPin = 4;
  111. int buzzerPin = 5;
  112. int potentiometer = A0;
  113. int soundPin = A2;
  114. int sensorPin = A6;
  115. int rotaryPin = A0;
  116. int rotaryValue = 0;
  117. const int buttonPin = 6;
  118. int buttonState = 0;
  119. unsigned long startTime;
  120. unsigned long endTime;
  121. unsigned long duration;
  122. int checkState;
  123. int i;
  124. int score = 0;
  125.  
  126. void setup() {
  127.  Serial.begin(9600);
  128.  
  129.  pinMode(ledPin, OUTPUT);
  130.  pinMode(buzzerPin, OUTPUT);
  131.  pinMode(buttonPin, INPUT);
  132.  pinMode(ROTARY_ANGLE_SENSOR, INPUT);
  133.  pinMode(LED,OUTPUT);  
  134.  
  135.  u8x8.begin();
  136.  u8x8.setFlipMode(1);
  137.  
  138.  LIS.begin(WIRE, LIS3DHTR_ADDRESS_UPDATED); //IIC init
  139.  delay(100);
  140.  LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
  141.  
  142.  //Get random analog noise to generate different seed numbers
  143.  randomSeed(analogRead(0));
  144. }
  145.  
  146. void loop() {
  147.   firstLevel();
  148.   secondLevel();
  149.   thirdLevel();
  150.   fourthLevel();
  151.   fifthLevel();
  152.  
  153.   //Print total nunmber of points at the end of the game
  154.   u8x8.clear();
  155.   u8x8.drawString(2,0,"points");
  156.   u8x8.print(score);
  157.   delay(3000);
  158.   u8x8.clear();
  159.   u8x8.drawString(0,0,"Thank you ");
  160.   u8x8.drawString(1,0,"for playing!");
  161.   delay(1000);
  162.  
  163.   playSong();
  164. }
  165.  
  166. //Flash the lights to signal the start of the level
  167. void startLevel(){
  168.   digitalWrite(ledPin, HIGH);
  169.   delay(1000);
  170.   digitalWrite(ledPin, LOW);
  171.   delay(1000);
  172. }
  173.  
  174. //Show text and flash the LED when someone wins a level
  175. void endLevel(){
  176.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  177.   u8x8.clear();
  178.   u8x8.print("Level cleared!");
  179.  
  180.   digitalWrite(ledPin, HIGH);
  181.   delay(500);
  182.   digitalWrite(ledPin, LOW);
  183.   delay(500);
  184.   digitalWrite(ledPin, HIGH);
  185.   delay(500);
  186.   digitalWrite(ledPin, LOW);
  187.   delay(500);
  188. }
  189.  
  190. //Show text, flash the LED and play the buzzer when someone loses
  191. void gameOver(){
  192.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  193.   u8x8.clear();
  194.   u8x8.print("Game over.");
  195.   delay(3000);
  196.   digitalWrite(ledPin, HIGH);
  197.  
  198.   analogWrite(buzzerPin, 200);
  199.   delay(500);
  200.   analogWrite(buzzerPin, 0);
  201.   delay(2000);
  202. }
  203.  
  204. //Level one, cover the light sensor to score the point
  205. void firstLevel(){
  206.   startLevel();
  207.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  208.   u8x8.clear();
  209.   u8x8.print("Level 1: Light");
  210.   delay(3000);
  211.  
  212.   u8x8.clear();
  213.   u8x8.drawString(0,0,"Block the light");
  214.   u8x8.drawString(0,1,"to pass the ");
  215.   u8x8.drawString(0,2,"level.");
  216.   delay(5000);
  217.  
  218.   sensorValue = analogRead(sensorPin);
  219.   outputValue = map(sensorValue, 0, 1023, 0, 255);
  220.   Serial.println(sensorValue);
  221.   if (sensorValue < 100){
  222.    score = score + 1;
  223.    endLevel();
  224.    delay(3000);
  225.   }
  226.   else{
  227.    gameOver();
  228.   }
  229. }
  230.  
  231. //Level two, speak into the microphone above a certain dB to score the point
  232. void secondLevel(){
  233.   startLevel();
  234.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  235.   u8x8.clear();
  236.   u8x8.print("Level 2: Sound");
  237.   delay(3000);
  238.   u8x8.clear();
  239.   u8x8.drawString(0,0,"Make noise");
  240.   u8x8.drawString(0,1,"to pass the ");
  241.   u8x8.drawString(0,2,"level.");
  242.   delay(5000);
  243.  
  244.   int soundState = analogRead(soundPin);
  245.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  246.   u8x8.clear();
  247.   u8x8.print(soundState);
  248.   delay(3000);
  249.  
  250.   if(soundState > 50){
  251.    score = score + 1;
  252.    endLevel();
  253.    delay(3000);
  254.   }
  255.   else{
  256.    gameOver();
  257.   }
  258. }
  259.  
  260. //Level three, hold the button when the LED switches on.
  261. void thirdLevel(){
  262.   startLevel();
  263.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  264.   u8x8.clear();
  265.   u8x8.print("Level 3: Timing");
  266.   delay(3000);
  267.   u8x8.clear();
  268.   u8x8.drawString(0,0,"Hold the button");
  269.   u8x8.drawString(0,1,"when the LED");
  270.   u8x8.drawString(0,2,"turns on.");
  271.   delay(3000);
  272.   u8x8.clear();
  273.   delay(3000);
  274.   checkState = 0;
  275.  
  276.   startTime = millis();
  277.   digitalWrite(ledPin, HIGH);
  278.   //Generate random number for how long the light stays on.
  279.   int randomLight = random (800, 2000);
  280.   delay(randomLight);
  281.   digitalWrite(ledPin, LOW);
  282.   delay(1000);
  283.   buttonState = digitalRead(buttonPin);
  284.   if (buttonState == HIGH){
  285.     checkState = 1;
  286.   }
  287.   endTime = millis();
  288.   duration = endTime - startTime;
  289.   if (checkState == 1 && duration < 3000){
  290.     score = score + 1;
  291.     endLevel();
  292.   }
  293.   else{
  294.     gameOver();
  295.   }
  296. }
  297.  
  298.  
  299. //Level four, angle the rotary knob within a certain range to score the point.
  300. void fourthLevel(){
  301.   startLevel();
  302.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  303.   u8x8.clear();
  304.   u8x8.print("Level 4: Twist");
  305.   u8x8.clear();
  306.   u8x8.drawString(0,0,"Turn the ");
  307.   u8x8.drawString(0,1,"rotary knob to");
  308.   u8x8.drawString(0,2,"the middle.");
  309.   delay(5000);
  310.  
  311.   float voltage;
  312.   int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
  313.   voltage = (float)sensor_value*ADC_REF/1023;
  314.   float degrees = (voltage*FULL_ANGLE)/GROVE_VCC;
  315.   //print readings on OLED display
  316.   for(i=0; i <= 10; i++){
  317.     u8x8.clear();
  318.     u8x8.print(degrees);  
  319.     delay(1000);
  320.   }
  321.   delay(3000);
  322.   if(degrees > 130 && degrees < 190){
  323.     score = score + 1;
  324.     endLevel();
  325.   }
  326.   else{
  327.     gameOver();
  328.   }
  329. }
  330.  
  331. //Level five, skew the board within a positive X, Y and Z angle to score the point.
  332. //Note: our display of the coordinates is bugged and all appears on one line
  333. //however, this still provides some valuable information for the player even if it doesn't display "correctly".
  334. void fifthLevel(){
  335.   startLevel();
  336.   u8x8.setFont(u8x8_font_chroma48medium8_r);
  337.   u8x8.clear();
  338.   u8x8.print("Level 5: Move");
  339.   delay(3000);
  340.   u8x8.clear();
  341.   u8x8.drawString(0,0,"Balance the");
  342.   u8x8.drawString(0,1,"board to pass");
  343.   u8x8.drawString(0,2,"the level.");
  344.   delay(3000);
  345.   u8x8.clear();
  346.   u8x8.drawString(0,0,"Hint: ");
  347.   u8x8.drawString(0,1,"X, Y, Z > 0");
  348.   delay(3000);
  349.  
  350.   if (!LIS)
  351.    {
  352.     Serial.println("LIS3DHTR didn't connect.");
  353.     while (1)
  354.       ;
  355.     return;
  356.    }
  357.   //Print readings on OLED screen
  358.   for(i = 0; i <=10; i++){
  359.     u8x8.setFont(u8x8_font_chroma48medium8_r);
  360.     u8x8.clear();
  361.     u8x8.drawString(0,0,"x:"); u8x8.print(LIS.getAccelerationX());
  362.     u8x8.drawString(0,1,"y:"); u8x8.print(LIS.getAccelerationY());
  363.     u8x8.drawString(0,2,"z:"); u8x8.print(LIS.getAccelerationZ());
  364.     delay(1000);
  365.   }
  366.   if(LIS.getAccelerationX() > 0 && LIS.getAccelerationY() > 0 && LIS.getAccelerationZ() > 0){
  367.     score = score + 1;
  368.     endLevel();
  369.   }
  370.   else{
  371.     gameOver();
  372.   }
  373. }
  374.  
  375. //The rest of the code below deals with playing the melody for our winning song.
  376.  
  377. int tempo = 114;
  378.  
  379. int melody[] = {
  380.   NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //1
  381.   NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8,
  382.   NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2,
  383.   NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16,
  384.   NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //repeat from 1
  385.   NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8,
  386.   NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2,
  387.   NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16,
  388.   REST,4, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_D5,8, NOTE_E5,8, NOTE_CS5,-8,
  389.   NOTE_B4,16, NOTE_A4,2, REST,4,
  390.  
  391.   REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,4, NOTE_A4,8, //7
  392.   NOTE_A5,8, REST,8, NOTE_A5,8, NOTE_E5,-4, REST,4,
  393.   NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_D5,8, NOTE_E5,8, REST,8,
  394.   REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4,
  395.   REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4,
  396.   NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, REST,4,
  397.    
  398.   NOTE_D5,2, NOTE_E5,8, NOTE_FS5,8, NOTE_D5,8, //13
  399.   NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, NOTE_A4,4,
  400.   REST,2, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8,
  401.   REST,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
  402.   NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
  403. };
  404.  
  405. int notes = sizeof(melody) / sizeof(melody[0]) / 2;
  406. int wholenote = (60000 * 4) / tempo;
  407. int divider = 0, noteDuration = 0;
  408. int length;
  409.  
  410. void playSong(){
  411.     for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
  412.     divider = melody[thisNote + 1];
  413.     if (divider > 0) {
  414.       noteDuration = (wholenote) / divider;
  415.     } else if (divider < 0) {
  416.       noteDuration = (wholenote) / abs(divider);
  417.       noteDuration *= 1.5; //
  418.     }
  419.     tone(buzzerPin, melody[thisNote], noteDuration * 0.9);
  420.     delay(noteDuration);
  421.     noTone(buzzerPin);
  422.   }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment