Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <ADXL345.h>
- #include <FastLED.h>
- ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
- #define NUM_LEDS 12
- #define DATA_PIN 3
- #define CLOCK_PIN 13
- #define BRIGHTNESS 255
- #define NUM_LEDS_IN_SEGMENT 2
- struct CRGB leds[NUM_LEDS];
- // general variables
- int sensitivity = 220; // pitch to define side that is lit
- int fadeIn = 10; // fade in led time when moving
- int fadeOut = 15; // fade out led time when moving
- int state;
- int stateOld;
- int staticThreshold = 5; // variable to define when the unit is slightly moving
- int mode = 3; // 3 initilisation
- int modeOld;
- int breatheSpeed = 1;
- int level = 0;
- int levelOld = 0;
- int gameEnd = 0;
- int gameWin = 0;
- int gameLose = 0;
- int gameFail = 0;
- // core xyz
- int x, y, z;
- int xa, ya, za, xb, yb, zb;
- int xaOld, yaOld, zaOld, xbOld, ybOld, zbOld;
- int fr, ba, le, ri, to, bo;
- // game variables
- //int gameOn = 3000;
- //int difficulty = 1;
- //int accuracy = 50;
- int randNumber;
- int randNumberOld;
- String sides[6] = {"fr", "ba", "le", "ri", "to", "bo"};
- int puzzleSteps = 10;
- int puzzleMax = 100;
- byte puzzle1[100];
- //int puzzle2[10] = {3, 5, 3, 5, 3, 5, 3, 5, 3, 5};
- byte sideState[6][4] = {
- {ba, to, fr, bo},
- {ri, to, le, bo},
- {ri, ba, le, fr},
- {ba, to, fr, bo},
- {ri, to, le, bo},
- {ri, ba, le, fr}
- };
- // HSV colour values
- //0 red, 32 orange, 64 yellow, 96 green, 128 aqua, 160 blue, 192 purple, 224 pink, 255 red
- int ledColour = 160;
- int ledColourReset = 160;
- int ledColourNew;
- int ledColourOld;
- int gHue = 0;
- int ledColourSides;
- // time variables
- unsigned long previousMillisSensor = 0; // will store last time sensor was updated
- const long intervalSensor = 1; // interval at which to check sensor
- unsigned long previousMillisLeds = 0; // will store last time LED was updated
- const long intervalLeds = 100; // interval at which to operate leds
- unsigned long previousMillisBreathe = 0; // will store last time sensor was updated
- const long intervalBreathe = 5000; // interval at which to check sensor
- unsigned long previousMillisGame = 0; // will store last time sensor was updated
- const long intervalGame = 10000; // will store last time sensor was updated
- unsigned long previousMillisPause = 0; // will store last time sensor was updated
- const long intervalPause = 1000; // interval at which to check sensor
- void setup()
- {
- adxl.powerOn();
- //look of tap movement on this axes - 1 == on; 0 == off
- // adxl.setTapDetectionOnX(0);
- // adxl.setTapDetectionOnY(0);
- // adxl.setTapDetectionOnZ(1);
- //set values for what is a tap, and what is a double tap (0-255)
- // adxl.setTapThreshold(50); //62.5mg per increment
- // adxl.setTapDuration(15); //625μs per increment
- // adxl.setDoubleTapLatency(80); //1.25ms per increment
- // adxl.setDoubleTapWindow(200); //1.25ms per increment
- //setting all interupts to take place on int pin 1
- //I had issues with int pin 2, was unable to reset it
- // adxl.setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT, ADXL345_INT1_PIN );
- // adxl.setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT, ADXL345_INT1_PIN );
- //register interupt actions - 1 == on; 0 == off
- // adxl.setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
- // adxl.setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
- // Wire.begin(); // join i2c bus (address optional for master)
- Serial.begin(9600); // start serial for output
- delay(2000);
- FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, BRG>(leds, NUM_LEDS);
- FastLED.clear();
- FastLED.setBrightness(BRIGHTNESS);
- randomSeed(analogRead(0)); // random seed generator
- }
- void loop() {
- randomPuzzleGenerator();
- readXyz();
- checkPosition();
- showLeds();
- // stateRead(); // game mode
- // pattern1();
- // if (level == 10) {
- // gameWin++;
- // }
- //
- // if (gameEnd==3) {
- //// pattern1();
- // }
- // EVERY_N_MILLISECONDS( 500 ) {addGlitter(20);}
- serialText();
- } // end of void loop
- void randomPuzzleGenerator () {
- if ( (level == 0) && (puzzle1[0] == 0 || puzzle1[1] == 0 || puzzle1[2] == 0 || puzzle1[3] == 0 || puzzle1[4] == 0 || puzzle1[5] == 0) ) {
- randNumber = random(6); // generate number between 1 and 6 inclusive
- for (int i = 0; i < puzzleMax; i++) {
- puzzle1[i] = randNumber; // push a number into the puzzle string
- randNumberOld = randNumber; // save the number
- randNumber = random(6); // generate a new number
- while (randNumber == randNumberOld) randNumber = random(6); // if its the same number, get a new number
- while (randNumberOld == 2 && randNumber == 5) randNumber = random(6); // 2 & 5 // if an opposite number is generated, get a new number
- while (randNumberOld == 5 && randNumber == 2) randNumber = random(6); // 5 & 2
- while (randNumberOld == 1 && randNumber == 4) randNumber = random(6); // 1 & 4
- while (randNumberOld == 4 && randNumber == 1) randNumber = random(6); // 4 & 1
- while (randNumberOld == 0 && randNumber == 3) randNumber = random(6); // 0 & 3
- while (randNumberOld == 3 && randNumber == 0) randNumber = random(6); // 3 & 0
- }
- for (int i = 0; i < puzzleSteps; i++) {
- Serial.print(puzzle1[i]);
- }
- Serial.println();
- }
- }
- void readXyz () { // read accelerometer, map to leds, set states
- // read the accelerometer
- adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z
- // map the readXyz figures
- xa = map(x, -255, 255, 255, 0); // left
- xb = map(x, -255, 255, 0, 255); // right
- ya = map(y, -255, 255, 255, 0); // front
- yb = map(y, -255, 255, 0, 255); // back
- za = map(z, -255, 255, 0, 255); // top
- zb = map(z, -255, 255, 255, 0); // bottom
- EVERY_N_MILLISECONDS( 20 ) {
- ba -= fadeOut;
- fr -= fadeOut;
- le -= fadeOut;
- ri -= fadeOut;
- to -= fadeOut;
- bo -= fadeOut;
- }
- if (xa > sensitivity) le = 255; if (le <= 0) le = 0; if (le >= 255) {
- le = 255;
- state = 1;
- }
- if (ya > sensitivity) fr = 255; if (fr <= 0) fr = 0; if (fr >= 255) {
- fr = 255;
- state = 2;
- }
- if (za > sensitivity) to = 255; if (to <= 0) to = 0; if (to >= 255) {
- to = 255;
- state = 3;
- }
- if (xb > sensitivity) ri = 255; if (ri <= 0) ri = 0; if (ri >= 255) {
- ri = 255;
- state = 4;
- }
- if (yb > sensitivity) ba = 255; if (ba <= 0) ba = 0; if (ba >= 255) {
- ba = 255;
- state = 5;
- }
- if (zb > sensitivity) bo = 255; if (bo <= 0) bo = 0; if (bo >= 255) {
- bo = 255;
- state = 6;
- }
- } // end of readXyz
- void checkPosition () { // main gameplay
- unsigned long currentMillisGame = millis();
- Serial.print(currentMillisGame - previousMillisGame); Serial.print(" || ");
- ledColour = map( (currentMillisGame - previousMillisGame), 0, intervalGame, ledColourOld, 255); // fade from last colour
- for (int i = 0; i < puzzleSteps; i++) {
- if (level == i && puzzle1[i] == state && (currentMillisGame - previousMillisGame <= intervalGame)) {
- previousMillisGame = currentMillisGame;
- levelWin();
- level++;
- } else if ((currentMillisGame - previousMillisGame >= intervalGame)) {
- previousMillisGame = currentMillisGame;
- levelFail();
- level = 0;
- for (int i = 0; i < puzzleSteps; i++) {
- puzzle1[i] = 0;
- }
- }
- }
- } // end of void checkPosition
- void levelWinOld() {
- if (state == 1) {
- ledColour = 96;
- ba = 255;
- to = 255;
- fr = 255;
- bo = 255;
- }
- if (state == 2) {
- ledColour = 96;
- ri = 255;
- to = 255;
- le = 255;
- bo = 255;
- }
- if (state == 3) {
- ledColour = 96;
- ri = 255;
- ba = 255;
- le = 255;
- fr = 255;
- }
- if (state == 4) {
- ledColour = 96;
- ba = 255;
- to = 255;
- fr = 255;
- bo = 255;
- }
- if (state == 5) {
- ledColour = 96;
- ri = 255;
- to = 255;
- le = 255;
- bo = 255;
- }
- if (state == 6) {
- ledColour = 96;
- ri = 255;
- ba = 255;
- le = 255;
- fr = 255;
- }
- ledColourOld = ledColour;
- // puzzleSteps++;
- // gameEnd=0;
- } // end of levelWin
- void levelFailOld() {
- if (state == 1) {
- ledColour = 0;
- ba = 255;
- to = 255;
- fr = 255;
- bo = 255;
- }
- if (state == 2) {
- ledColour = 0;
- ri = 255;
- to = 255;
- le = 255;
- bo = 255;
- }
- if (state == 3) {
- ledColour = 0;
- ri = 255;
- ba = 255;
- le = 255;
- fr = 255;
- }
- if (state == 4) {
- ledColour = 0;
- ba = 255;
- to = 255;
- fr = 255;
- bo = 255;
- }
- if (state == 5) {
- ledColour = 0;
- ri = 255;
- to = 255;
- le = 255;
- bo = 255;
- }
- if (state == 6) {
- ledColour = 0;
- ri = 255;
- ba = 255;
- le = 255;
- fr = 255;
- }
- ledColourOld = ledColour;
- // puzzleSteps=10;
- // gameEnd++;
- } // end of levelFail
- void levelWin() {
- ledColourSides = 96;
- for (int j = 0; j < 6; j++) {
- if (state == j) {
- for (int i = 0; i < 3; i++) {
- sideState[state - 1][i] = {255};
- }
- // ledColourOld = ledColour;
- // gameWin++;
- // gameEnd=0;
- }
- }
- } // end of levelWin
- void levelFail() {
- ledColourSides = 0;
- for (int j = 0; j < 6; j++) {
- if (state == j) {
- for (int i = 0; i < 4; i++) {
- sideState[state - 1][i] = {255};
- Serial.println();
- Serial.println(sideState[state - 1][i]);
- Serial.println();
- }
- // ledColourOld = ledColour;
- // gameFail++;
- // gameEnd++;
- }
- }
- } // end of levelWin
- void showLeds() {
- for (int i = 0; i < NUM_LEDS_IN_SEGMENT; i++) {
- leds[i] = CHSV( ledColour, 255, ba); // back
- leds[i + NUM_LEDS_IN_SEGMENT] = CHSV( ledColour, 255, le); // left
- leds[i + 2 * NUM_LEDS_IN_SEGMENT] = CHSV( ledColour, 255, fr); // front
- leds[i + 3 * NUM_LEDS_IN_SEGMENT] = CHSV( ledColour, 255, ri); // right
- leds[i + 4 * NUM_LEDS_IN_SEGMENT] = CHSV( ledColour, 255, to); // top
- leds[i + 5 * NUM_LEDS_IN_SEGMENT] = CHSV( ledColour, 255, bo); // bottom
- }
- FastLED.show();
- } // end of showLeds
- void serialText() {
- Serial.print(le); Serial.print(" ");
- Serial.print(ri); Serial.print(" | ");
- Serial.print(ba); Serial.print(" ");
- Serial.print(fr); Serial.print(" | ");
- Serial.print(to); Serial.print(" ");
- Serial.print(bo); Serial.print(" || ");
- Serial.print(" state: "); Serial.print(state);
- Serial.print(" mode: "); Serial.print(mode);
- Serial.print(" level: "); Serial.print(level);
- Serial.print(" gameEnd: "); Serial.print(gameEnd);
- // Serial.print( " lvlOld: "); Serial.print(levelOld);
- Serial.println();
- } // end of void serialText
- void addGlitter( fract8 chanceOfGlitter) {
- if ( random8() < chanceOfGlitter) {
- leds[ random16(NUM_LEDS) ] += CRGB::White;
- }
- FastLED.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement