-
-
Save ramonimbao/dccc1d86d18cf0f5231c786b04b0dd74 to your computer and use it in GitHub Desktop.
Digital Hourglass Source Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2020 Ramon Imbao | |
Permission is hereby granted, free of charge, to any person obtaining a | |
copy of this software and associated documentation files (the "Software"), | |
to deal in the Software without restriction, including without limitation | |
the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
and/or sell copies of the Software, and to permit persons to whom the | |
Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_LSM303_U.h> | |
#include "LedControl.h" | |
#include <stddef.h> | |
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321); | |
LedControl lc=LedControl(5,3,4,2); // Pins: DIN,CLK,CS, # of Display connected | |
int fudge = 3.5; | |
byte currentStep = 0; | |
unsigned long currentTime_Pass = 0; | |
unsigned long previousTime_Pass = 0; | |
unsigned long currentTime_Step = 0; | |
unsigned long previousTime_Step = 0; | |
unsigned int interval_Pass = 1000; | |
unsigned int interval_Step = 100; | |
bool canPass = false; | |
enum DIRECTION { | |
UP, | |
DOWN, | |
LEFT, | |
RIGHT, | |
NONE | |
}; | |
void shuffle(byte *arr, size_t n) { | |
if (n > 1) { | |
size_t i; | |
for (i = 0; i < n - 1; i++) { | |
size_t j = i + rand() / (RAND_MAX / (n - i) + 1); | |
byte t = arr[j]; | |
arr[j] = arr[i]; | |
arr[i] = t; | |
} | |
} | |
} | |
byte gravity = NONE; | |
// 0 = upright | |
// 1 = upside down | |
// 2 = to the left | |
// 3 = to the right | |
byte matrix[2][64] = { | |
{ | |
1,1,1,1,1,1,0,0, | |
1,1,1,1,1,1,0,0, | |
1,1,1,1,1,1,1,1, | |
1,1,1,1,1,1,1,1, | |
1,1,1,1,1,1,1,1, | |
1,1,1,1,1,1,1,1, | |
1,1,1,1,1,1,1,1, | |
1,1,1,1,1,1,1,1, | |
}, | |
{ | |
0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0, | |
0,0,0,0,0,0,0,0, | |
}, | |
}; | |
void setup() { | |
// --- MAX7219 setup | |
// Wake up displays | |
lc.shutdown(0, false); | |
lc.shutdown(1, false); | |
// Set intensity levels | |
lc.setIntensity(0, 0); | |
lc.setIntensity(1, 0); | |
// Clear displays | |
lc.clearDisplay(0); | |
lc.clearDisplay(1); | |
// --- LSM303 setup | |
accel.begin(); | |
// -- Randomize seed | |
randomSeed(analogRead(0)); | |
} | |
void loop() { | |
currentTime_Pass = millis(); | |
currentTime_Step = millis(); | |
sensors_event_t event; | |
accel.getEvent(&event); | |
float ex = event.acceleration.x; | |
float ey = event.acceleration.y; | |
// --- Timing | |
if (currentTime_Pass - previousTime_Pass >= interval_Pass) { | |
canPass = true; | |
previousTime_Pass = currentTime_Pass; | |
} | |
// --- Simulation | |
// Upright | |
if (currentTime_Step - previousTime_Step >= interval_Step) { | |
if (gravity == UP) { | |
byte sm[4] = {1,2,3,4}; | |
byte currentState = random(1, 5); | |
shuffle(sm, 4); | |
// Center | |
for (byte y = 0; y < 7; y++) { | |
for (byte x = 1; x < 8; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x + 7] == 0) { // Free space below | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 7] = 2; | |
} | |
else if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x + 7] == 1) { // No free space below | |
byte left = matrix[i][y*8+x - 1]; | |
byte right = matrix[i][y*8+x + 8]; | |
if (left == 0 && right == 1) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 1] = 2; | |
} | |
else if (left == 1 && right == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 8] = 2; | |
} | |
else if (left == 0 && right == 0) { | |
if (random(2) == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 1] = 2; | |
} | |
else { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 8] = 2; | |
} | |
} | |
} | |
} | |
} | |
} | |
// V-Edge | |
for (byte y = 0; y < 7; y++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8] == 1 && matrix[i][y*8 + 8] == 0) { // Free space to the right | |
matrix[i][y*8] = 0; | |
matrix[i][y*8 + 8] = 2; | |
} | |
} | |
} | |
// H-Edge | |
for (byte x = 1; x < 8; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][56+x] == 1 && matrix[i][56+x - 1] == 0) { // Free space to the left | |
matrix[i][56+x] = 0; | |
matrix[i][56+x - 1] = 2; | |
} | |
} | |
} | |
// Point | |
if (matrix[0][56] == 1 && matrix[1][7] == 0 && canPass) { // Free space to the next matrix | |
matrix[0][56] = 0; | |
matrix[1][7] = 2; | |
canPass = false; | |
} | |
} | |
// Upside down | |
else if (gravity == DOWN) { | |
// Center | |
for (byte y = 1; y < 8; y++) { | |
for (byte x = 0; x < 7; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x - 7] == 0) { // Free space below | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 7] = 2; | |
} | |
else if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x - 7] == 1) { // No free space below | |
byte left = matrix[i][y*8+x + 1]; | |
byte right = matrix[i][y*8+x - 8]; | |
if (left == 0 && right == 1) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 1] = 2; | |
} | |
else if (left == 1 && right == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 8] = 2; | |
} | |
else if (left == 0 && right == 0) { | |
if (random(2) == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 1] = 2; | |
} | |
else { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 8] = 2; | |
} | |
} | |
} | |
} | |
} | |
} | |
// H-Edge | |
for (byte x = 0; x < 7; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][x] == 1 && matrix[i][x + 1] == 0) { // Free space to the left | |
matrix[i][x] = 0; | |
matrix[i][x + 1] = 2; | |
} | |
} | |
} | |
// V-Edge | |
for (byte y = 1; y < 8; y++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8+7] == 1 && matrix[i][y*8+7 - 8] == 0) { // Free space to the right | |
matrix[i][y*8+7] = 0; | |
matrix[i][y*8+7 - 8] = 2; | |
} | |
} | |
} | |
// Point | |
if (matrix[1][7] == 1 && matrix[0][56] == 0 && canPass) { // Free space to the next matrix | |
matrix[1][7] = 0; | |
matrix[0][56] = 2; | |
canPass = false; | |
} | |
} | |
// Left | |
else if (gravity == LEFT) { | |
// Center | |
for (byte y = 1; y < 8; y++) { | |
for (byte x = 1; x < 8; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x - 9] == 0) { // Free space below | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 9] = 2; | |
} | |
else if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x - 9] == 1) { // No free space below | |
byte left = matrix[i][y*8+x - 8]; | |
byte right = matrix[i][y*8+x - 1]; | |
if (left == 0 && right == 1) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 8] = 2; | |
} | |
else if (left == 1 && right == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 1] = 2; | |
} | |
else if (left == 0 && right == 0) { | |
if (random(2) == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 8] = 2; | |
} | |
else { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x - 1] = 2; | |
} | |
} | |
} | |
} | |
} | |
} | |
// H-Edge | |
for (byte x = 1; x < 8; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][x] == 1 && matrix[i][x - 1] == 0) { // Free space to the right | |
matrix[i][x] = 0; | |
matrix[i][x - 1] = 2; | |
} | |
} | |
} | |
// V-Edge | |
for (byte y = 1; y < 8; y++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8] == 1 && matrix[i][y*8 - 8] == 0) { // Free space to the left | |
matrix[i][y*8] = 0; | |
matrix[i][y*8 - 8] = 2; | |
} | |
} | |
} | |
} | |
// Right | |
else if (gravity == RIGHT) { | |
// Center | |
for (byte y = 0; y < 7; y++) { | |
for (byte x = 0; x < 7; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x + 9] == 0) { // Free space below | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 9] = 2; | |
} | |
else if (matrix[i][y*8+x] == 1 && matrix[i][y*8+x + 9] == 1) { // No free space below | |
byte left = matrix[i][y*8+x + 8]; | |
byte right = matrix[i][y*8+x + 1]; | |
if (left == 0 && right == 1) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 8] = 2; | |
} | |
else if (left == 1 && right == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 1] = 2; | |
} | |
else if (left == 0 && right == 0) { | |
if (random(2) == 0) { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 8] = 2; | |
} | |
else { | |
matrix[i][y*8+x] = 0; | |
matrix[i][y*8+x + 1] = 2; | |
} | |
} | |
} | |
} | |
} | |
} | |
// V-Edge | |
for (byte y = 0; y < 7; y++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][y*8+7] == 1 && matrix[i][y*8+7 + 8] == 0) { // Free space to the left | |
matrix[i][y*8+7] = 0; | |
matrix[i][y*8+7 + 8] = 2; | |
} | |
} | |
} | |
// H-Edge | |
for (byte x = 0; x < 7; x++) { | |
for (byte i = 0; i < 2; i++) { | |
if (matrix[i][56+x] == 1 && matrix[i][56+x + 1] == 0) { // Free space to the right | |
matrix[i][56+x] = 0; | |
matrix[i][56+x + 1] = 2; | |
} | |
} | |
} | |
} | |
// Normalize matrix | |
for (byte y = 0; y < 8; y++) { | |
for (byte x = 0; x < 8; x++) { | |
if (matrix[0][y*8+x] >= 1) matrix[0][y*8+x] = 1; | |
if (matrix[1][y*8+x] >= 1) matrix[1][y*8+x] = 1; | |
} | |
} | |
// -- Accelerometer | |
if ((ex >= 0 - fudge && ex <= 0 + fudge) && (ey >= 10 - fudge && ey <= 10 + fudge)) { | |
// Upright | |
gravity = UP; | |
} | |
else if ((ex >= 0 - fudge && ex <= 0 + fudge) && (ey >= -10 - fudge && ey <= -10 + fudge)) { | |
// Upside down | |
gravity = DOWN; | |
} | |
else if ((ex >= 10 - fudge && ex <= 10 + fudge) && (ey >= 0 - fudge && ey <= 0 + fudge)) { | |
// Left | |
gravity = LEFT; | |
} | |
else if ((ex >= -10 - fudge && ex <= -10 + fudge) && (ey >= 0 - fudge && ey <= 0 + fudge)) { | |
// Right | |
gravity = RIGHT; | |
} | |
// --- Display | |
for(byte y = 0; y < 8; y++) { | |
for (byte x = 0; x < 8; x++) { | |
lc.setLed(0, y, x, matrix[0][y*8+x]); | |
lc.setLed(1, 7-y, 7-x, matrix[1][y*8+x]); | |
} | |
} | |
previousTime_Step = currentTime_Step; | |
} | |
//delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment