//underground accelleration
#include "Wire.h"
//Per accelerometro
//#include "SparkFunLSM6DSO.h"
#include <Adafruit_MPU6050.h>
//#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
//Per matrice
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <LedControl.h>
//-----------START - DISPLAY------------------------------
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 8 // numero di display collegati
#define CLK_PIN 12 // CLK or SCK
#define DATA_PIN 11 // DATA or MOSI in
#define CS_PIN 10 // CS or SS
#define SPEED_TIME 75 // Velocità transizione
#define PAUSE_TIME 0
#define MAX_MESG 20
// Hardware SPI connection
//MD_Parola MATRIX_P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
//MD_MAX72XX MATRIX_M = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
LedControl MATRIX = LedControl(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
//-----------END - DISPLAY------------------------------
// LSM6DSO accelerometro; //SparkFun
Adafruit_MPU6050 accelerometro; //Adafruit simulazione
//Adafruit_LIS3DH accelerometro; //Adafruit realta
float maxAcc;
float xByGyro, yByGyro, zByGyro; //variabili globali x, y, z della rotazione
float xByAcc, yByAcc, zByAcc; //variabili globali x, y, z della accellerazione
float z_angle;
float XY_Acc;
void setup() {
Serial.begin(115200);
delay(500);
//-----------START - INIZIALIZZAZIONE accelerometro------------------------------
Wire.begin();
delay(10);
while (!accelerometro.begin()) {
Serial.println("Impossibile connettersi all'accelerometro,");
delay(1000);
}
Serial.println("Pronti!");
/* Solo x SparkFun
if (accelerometro.initialize(BASIC_SETTINGS))
{
Serial.println("Impostazioni di base caricate.");
}
*/
accelerometro.setAccelerometerRange(MPU6050_RANGE_2_G);
Serial.print("Range accelerometro: ");
switch (accelerometro.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
maxAcc = 28;
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
maxAcc = 39.24;
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
maxAcc = 78.48;
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
maxAcc = 156.96;
Serial.println("+-16G");
break;
}
accelerometro.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Range giroscopio: ");
switch (accelerometro.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
accelerometro.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (accelerometro.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
Serial.println("");
delay(100);
//-----------END - INIZIALIZZAZIONE accelerometro------------------------------
//-----------START - INIZIALIZZAZIONE MATRICE------------------------------
MATRIX.shutdown(0, false);
MATRIX.setIntensity(0, 0);
MATRIX.clearDisplay(0);
//Definisco delle zone: grafiche "pre-confezionate" da visualizzare
//MATRIX_P.setZone(0, MAX_DEVICES - MAX_DEVICES, MAX_DEVICES - 1);
//MATRIX_P.setZone(1, MAX_DEVICES - MAX_DEVICES, MAX_DEVICES - 1);
//Cosa c'è all'interno delle zone e la sua tabulazione
//MATRIX_P.displayZoneText(1, szTime, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
//MATRIX_P.displayZoneText(0, szMesg, PA_CENTER, SPEED_TIME, 0, PA_PRINT , PA_NO_EFFECT);
//-----------END - INIZIALIZZAZIONE MATRICE------------------------------
}
void loop() {
//-----------START - MAIN ACCELLEROMETRO------------------------------
sensors_event_t a, g, temp;
accelerometro.getEvent(&a, &g, &temp);
xByGyro = g.gyro.x;
yByGyro = g.gyro.y;
zByGyro = g.gyro.z;
xByAcc = a.acceleration.x;
yByAcc = a.acceleration.y;
zByAcc = a.acceleration.z;
z_angle = acos(constrain(zByAcc, -1, 1)/1)*180/PI;
if (xByAcc < 0)
XY_Acc = -sqrt(pow(xByAcc, 2.0f) + pow(yByAcc, 2.0f));
else
XY_Acc = sqrt(pow(xByAcc, 2.0f) + pow(yByAcc, 2.0f));
//Serial.println(XY_Acc);
//Proporzione x accensione led
float numberLED_on;
//numberLED_on = map(XY_Acc, 0, maxAcc, 0, MAX_DEVICES*8-1);
//Serial.println(numberLED_on);
//Serial.println("");
numberLED_on = (XY_Acc*(MAX_DEVICES*8-1))/maxAcc;
Serial.println(numberLED_on);
// Accendi un LED proporzionalmente all'accelerazione
for (int i = 0; i < MAX_DEVICES*8; i++) {
if (i < numberLED_on) {
MATRIX.setLed(0, 0, i, true); // Accendi il LED corrispondente
} else {
MATRIX.setLed(0, 0, i, false); // Spegni il LED corrispondente
}
}
delay(100);
/*
for (int row = 0; row < numberLED_on; row++) {
MATRIX.setRow(0, row, 0b11111111);
}
delay(200);
for (int row = 0; row < numberLED_on; row++) {
MATRIX.setRow(0, row, 0b11111111);
}
delay(200);
*/
//-----------END - MAIN ACCELLEROMETRO------------------------------
// Accendi i LED proporzionalmente all'accelerazione
for (int i = 0; i < 8 * MAX_DEVICES; i++) {
if (i < numberLED_on) {
MATRIX.setLed(0, i, i % 8, true); // Accendi il LED corrispondente
} else {
MATRIX.setLed(0, i, i % 8, false); // Spegni il LED corrispondente
}
}
delay(200);
/*
//-----------START - MAIN MATRICE------------------------------
static uint32_t lastTime = 0; // Memory (ms)
static uint8_t display = 0; // Current display mode
static bool flasher = false; // Seconds passing flasher
MATRIX_P.displayAnimate();
if (MATRIX_P.getZoneStatus(0))
{
switch (display)
{
case 0: // Temperature deg Celsius
MATRIX_P.setPause(0, 1000); //tempo di visualizzazione di inizio e fine animazioni
MATRIX_P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_UP); //effetto di entrata e uscita del testo nella zona 0
display++;
dtostrf(celsius, 3, 1, szMesg); //float to string - dtostrf (float_value, min_width, num_digits_after_decimal, where_to_store_string)
strcat(szMesg, "$"); //concatena le stringhe
break;
case 1: // Temperature deg Fahrenheit
MATRIX_P.setTextEffect(0, PA_SCROLL_UP, PA_SCROLL_DOWN);
display++;
dtostrf(fahrenheit, 3, 1, szMesg);
strcat(szMesg, "&");
break;
case 2: // Humidity
MATRIX_P.setTextEffect(0, PA_SCROLL_DOWN, PA_SCROLL_LEFT);
display++;
dtostrf(humidity, 3, 0, szMesg);
strcat(szMesg, "%UR");
break;
case 3: // Clock
MATRIX_P.setFont(0, numeric7Seg);
MATRIX_P.setTextEffect(0, PA_PRINT, PA_NO_EFFECT);
MATRIX_P.setPause(0, 0);
if ((millis() - lastTime) >= 1000)
{
lastTime = millis();
getTime(szMesg, flasher);
flasher = !flasher;
}
if ((seconds == 00) && (seconds <= 30)) {
display++;
MATRIX_P.setTextEffect(0, PA_PRINT, PA_WIPE_CURSOR);
}
break;
case 4: // Day of week
MATRIX_P.setFont(0, nullptr);
MATRIX_P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
display++;
dow2str(wday, szMesg, MAX_MESG);
break;
default: // Calendar
MATRIX_P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
display = 0;
getDate(szMesg);
break;
}
MATRIX_P.displayReset(0); // Rest zone zero
}
//-----------END - MAIN MATRICE------------------------------
*/
}