Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <U8x8lib.h>
- #include "LIS3DHTR.h"
- LIS3DHTR<TwoWire> LIS; //IIC
- //Rotary potentiometer variables
- #define WIRE Wire
- #define ROTARY_ANGLE_SENSOR A0
- #define LED 3
- #define ADC_REF 5
- #define GROVE_VCC 5
- #define FULL_ANGLE 300
- //Musical notes for buzzer 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
- U8X8_SSD1306_128X64_ALT0_HW_I2C u8x8(U8X8_PIN_NONE);
- int sensorValue = 0; // value read from the port
- int outputValue = 0; // value output to the PWM (analog out)
- int ledPin = 4;
- int buzzerPin = 5;
- int potentiometer = A0;
- int soundPin = A2;
- int sensorPin = A6;
- int rotaryPin = A0;
- int rotaryValue = 0;
- const int buttonPin = 6;
- int buttonState = 0;
- unsigned long startTime;
- unsigned long endTime;
- unsigned long duration;
- int checkState;
- int i;
- int score = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(ledPin, OUTPUT);
- pinMode(buzzerPin, OUTPUT);
- pinMode(buttonPin, INPUT);
- pinMode(ROTARY_ANGLE_SENSOR, INPUT);
- pinMode(LED,OUTPUT);
- u8x8.begin();
- u8x8.setFlipMode(1);
- LIS.begin(WIRE, LIS3DHTR_ADDRESS_UPDATED); //IIC init
- delay(100);
- LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
- //Get random analog noise to generate different seed numbers
- randomSeed(analogRead(0));
- }
- void loop() {
- firstLevel();
- secondLevel();
- thirdLevel();
- fourthLevel();
- fifthLevel();
- //Print total nunmber of points at the end of the game
- u8x8.clear();
- u8x8.drawString(2,0,"points");
- u8x8.print(score);
- delay(3000);
- u8x8.clear();
- u8x8.drawString(0,0,"Thank you ");
- u8x8.drawString(1,0,"for playing!");
- delay(1000);
- playSong();
- }
- //Flash the lights to signal the start of the level
- void startLevel(){
- digitalWrite(ledPin, HIGH);
- delay(1000);
- digitalWrite(ledPin, LOW);
- delay(1000);
- }
- //Show text and flash the LED when someone wins a level
- void endLevel(){
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print("Level cleared!");
- digitalWrite(ledPin, HIGH);
- delay(500);
- digitalWrite(ledPin, LOW);
- delay(500);
- digitalWrite(ledPin, HIGH);
- delay(500);
- digitalWrite(ledPin, LOW);
- delay(500);
- }
- //Show text, flash the LED and play the buzzer when someone loses
- void gameOver(){
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print("Game over.");
- delay(3000);
- digitalWrite(ledPin, HIGH);
- analogWrite(buzzerPin, 200);
- delay(500);
- analogWrite(buzzerPin, 0);
- delay(2000);
- }
- //Level one, cover the light sensor to score the point
- void firstLevel(){
- startLevel();
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print("Level 1: Light");
- delay(3000);
- u8x8.clear();
- u8x8.drawString(0,0,"Block the light");
- u8x8.drawString(0,1,"to pass the ");
- u8x8.drawString(0,2,"level.");
- delay(5000);
- sensorValue = analogRead(sensorPin);
- outputValue = map(sensorValue, 0, 1023, 0, 255);
- Serial.println(sensorValue);
- if (sensorValue < 100){
- score = score + 1;
- endLevel();
- delay(3000);
- }
- else{
- gameOver();
- }
- }
- //Level two, speak into the microphone above a certain dB to score the point
- void secondLevel(){
- startLevel();
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print("Level 2: Sound");
- delay(3000);
- u8x8.clear();
- u8x8.drawString(0,0,"Make noise");
- u8x8.drawString(0,1,"to pass the ");
- u8x8.drawString(0,2,"level.");
- delay(5000);
- int soundState = analogRead(soundPin);
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print(soundState);
- delay(3000);
- if(soundState > 50){
- score = score + 1;
- endLevel();
- delay(3000);
- }
- else{
- gameOver();
- }
- }
- //Level three, hold the button when the LED switches on.
- void thirdLevel(){
- startLevel();
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print("Level 3: Timing");
- delay(3000);
- u8x8.clear();
- u8x8.drawString(0,0,"Hold the button");
- u8x8.drawString(0,1,"when the LED");
- u8x8.drawString(0,2,"turns on.");
- delay(3000);
- u8x8.clear();
- delay(3000);
- checkState = 0;
- startTime = millis();
- digitalWrite(ledPin, HIGH);
- //Generate random number for how long the light stays on.
- int randomLight = random (800, 2000);
- delay(randomLight);
- digitalWrite(ledPin, LOW);
- delay(1000);
- buttonState = digitalRead(buttonPin);
- if (buttonState == HIGH){
- checkState = 1;
- }
- endTime = millis();
- duration = endTime - startTime;
- if (checkState == 1 && duration < 3000){
- score = score + 1;
- endLevel();
- }
- else{
- gameOver();
- }
- }
- //Level four, angle the rotary knob within a certain range to score the point.
- void fourthLevel(){
- startLevel();
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print("Level 4: Twist");
- u8x8.clear();
- u8x8.drawString(0,0,"Turn the ");
- u8x8.drawString(0,1,"rotary knob to");
- u8x8.drawString(0,2,"the middle.");
- delay(5000);
- float voltage;
- int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
- voltage = (float)sensor_value*ADC_REF/1023;
- float degrees = (voltage*FULL_ANGLE)/GROVE_VCC;
- //print readings on OLED display
- for(i=0; i <= 10; i++){
- u8x8.clear();
- u8x8.print(degrees);
- delay(1000);
- }
- delay(3000);
- if(degrees > 130 && degrees < 190){
- score = score + 1;
- endLevel();
- }
- else{
- gameOver();
- }
- }
- //Level five, skew the board within a positive X, Y and Z angle to score the point.
- //Note: our display of the coordinates is bugged and all appears on one line
- //however, this still provides some valuable information for the player even if it doesn't display "correctly".
- void fifthLevel(){
- startLevel();
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.print("Level 5: Move");
- delay(3000);
- u8x8.clear();
- u8x8.drawString(0,0,"Balance the");
- u8x8.drawString(0,1,"board to pass");
- u8x8.drawString(0,2,"the level.");
- delay(3000);
- u8x8.clear();
- u8x8.drawString(0,0,"Hint: ");
- u8x8.drawString(0,1,"X, Y, Z > 0");
- delay(3000);
- if (!LIS)
- {
- Serial.println("LIS3DHTR didn't connect.");
- while (1)
- ;
- return;
- }
- //Print readings on OLED screen
- for(i = 0; i <=10; i++){
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.clear();
- u8x8.drawString(0,0,"x:"); u8x8.print(LIS.getAccelerationX());
- u8x8.drawString(0,1,"y:"); u8x8.print(LIS.getAccelerationY());
- u8x8.drawString(0,2,"z:"); u8x8.print(LIS.getAccelerationZ());
- delay(1000);
- }
- if(LIS.getAccelerationX() > 0 && LIS.getAccelerationY() > 0 && LIS.getAccelerationZ() > 0){
- score = score + 1;
- endLevel();
- }
- else{
- gameOver();
- }
- }
- //The rest of the code below deals with playing the melody for our winning song.
- int tempo = 114;
- int melody[] = {
- NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //1
- NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8,
- NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2,
- NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16,
- NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //repeat from 1
- NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8,
- NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2,
- NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16,
- REST,4, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_D5,8, NOTE_E5,8, NOTE_CS5,-8,
- NOTE_B4,16, NOTE_A4,2, REST,4,
- REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,4, NOTE_A4,8, //7
- NOTE_A5,8, REST,8, NOTE_A5,8, NOTE_E5,-4, REST,4,
- NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_D5,8, NOTE_E5,8, REST,8,
- REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4,
- REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4,
- NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, REST,4,
- NOTE_D5,2, NOTE_E5,8, NOTE_FS5,8, NOTE_D5,8, //13
- NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, NOTE_A4,4,
- REST,2, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8,
- REST,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
- NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
- };
- int notes = sizeof(melody) / sizeof(melody[0]) / 2;
- int wholenote = (60000 * 4) / tempo;
- int divider = 0, noteDuration = 0;
- int length;
- void playSong(){
- for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
- divider = melody[thisNote + 1];
- if (divider > 0) {
- noteDuration = (wholenote) / divider;
- } else if (divider < 0) {
- noteDuration = (wholenote) / abs(divider);
- noteDuration *= 1.5; //
- }
- tone(buzzerPin, melody[thisNote], noteDuration * 0.9);
- delay(noteDuration);
- noTone(buzzerPin);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment