Diferencia entre revisiones de «Mesa musical»

De fabwiki
Ir a la navegación Ir a la búsqueda
Sin resumen de edición
 
(No se muestran 3 ediciones intermedias del mismo usuario)
Línea 1: Línea 1:
=Fotos=
<gallery>
Mesa musical 1.jpg
Mesa musical 2.jpg
</gallery>
=Código=
<syntaxhighlight lang=c++>
<syntaxhighlight lang=c++>
#include <float.h>
#include <float.h>
Línea 5: Línea 12:
#include <Adafruit_ADXL345_U.h>
#include <Adafruit_ADXL345_U.h>


const int data_points = 10;
const int data_points = 5;
const int speaker_pin = 3;
const int speaker_pin = 3;


const float base_duration_ms = 40;
const float base_freq = 100.0;
const float dynamic_pitch_factor = 5.0;
const float dynamic_pitch_factor = 5.0;
const float dynamic_duration_factor = 10.0;
const float dynamic_duration_factor = 10.0;
const float base_duration_ms = 40;
const float note_interval = 1.059463;
const float base_freq = 100.0;
const float detection_threshold = 0.4;
const float note_interval = 1.05946;
const float detection_threshold = 0.5;
const float sensitivity_factor = 20.0;
const float sensitivity_factor = 20.0;
const float degree_offset = 3.0;
const float degree_offset = 3.0;
const float baseline_iir_filter_coef = 0.6;
const float baseline_iir_filter_coef = 0.6;
const range_t desired_range = ADXL345_RANGE_4_G;
const dataRate_t desired_rate = ADXL345_DATARATE_3200_HZ;


#define NUM_NOTES 32
#define NUM_NOTES 33


int notes[NUM_NOTES] = {0, 2, 3, 5, 7, 8, 10};
int notes[NUM_NOTES] = {0, 2, 3, 5, 7, 8, 10};
Línea 27: Línea 36:
   Serial.begin(115200);
   Serial.begin(115200);
   while (!accel.begin()) {
   while (!accel.begin()) {
     Serial.println("accel.begin() failed, we will retry");
     Serial.println("accel.begin() failed. We will retry.");
     delay(100);
     delay(100);
   }
   }


  const range_t desired_range = ADXL345_RANGE_4_G;
   while (accel.getRange() != desired_range) {
   while (accel.getRange() != desired_range) {
     Serial.println("setting range");
     Serial.println("setting range");
Línea 38: Línea 46:
   }
   }


  const dataRate_t desired_rate = ADXL345_DATARATE_3200_HZ;
   while (accel.getDataRate() != desired_rate) {
   while (accel.getDataRate() != desired_rate) {
     Serial.println("setting rate");
     Serial.println("setting rate");
Línea 71: Línea 78:


     const int first_index = 0;
     const int first_index = 0;
     const int last_index = sizeof(notes) / sizeof(*notes) - 1;
     const int last_index = NUM_NOTES - 1;
     degree = max(degree, first_index);
     degree = max(degree, first_index);
     degree = min(degree, last_index);
     degree = min(degree, last_index);

Revisión actual - 19:31 18 nov 2026

Fotos

Código

#include <float.h>

#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

const int data_points = 5;
const int speaker_pin = 3;

const float base_duration_ms = 40;
const float base_freq = 100.0;
const float dynamic_pitch_factor = 5.0;
const float dynamic_duration_factor = 10.0;
const float note_interval = 1.059463;
const float detection_threshold = 0.4;
const float sensitivity_factor = 20.0;
const float degree_offset = 3.0;
const float baseline_iir_filter_coef = 0.6;
const range_t desired_range = ADXL345_RANGE_4_G;
const dataRate_t desired_rate = ADXL345_DATARATE_3200_HZ;

#define NUM_NOTES 33

int notes[NUM_NOTES] = {0, 2, 3, 5, 7, 8, 10};

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();

void setup(void) {
  Serial.begin(115200);
  while (!accel.begin()) {
    Serial.println("accel.begin() failed. We will retry.");
    delay(100);
  }

  while (accel.getRange() != desired_range) {
    Serial.println("setting range");
    accel.setRange(desired_range);
    delay(100);
  }

  while (accel.getDataRate() != desired_rate) {
    Serial.println("setting rate");
    accel.setDataRate(desired_rate);
    delay(100);
  }

  pinMode(speaker_pin, OUTPUT);

  for (int i = 7; i < NUM_NOTES; i++) {
    notes[i] = notes[i - 7] + 12;
  }
  Serial.println("done with setup");
}

float old_max = 50;

void loop(void) {
  float max = FLT_MAX * -1;
  sensors_event_t event;
  for (int i = 0; i < data_points; i++) {
    accel.getEvent(&event);
    max = max(event.acceleration.x, max);
  }
  
  max = sensitivity_factor * sqrt(sqrt(max));

  const bool detected = (max - old_max) > detection_threshold;

  if (detected) {
    int degree = dynamic_pitch_factor * (max - old_max) - degree_offset;

    const int first_index = 0;
    const int last_index = NUM_NOTES - 1;
    degree = max(degree, first_index);
    degree = min(degree, last_index);
    
    const float freq = base_freq * pow(note_interval, notes[degree]);
    noTone(speaker_pin);
    tone(speaker_pin, freq, base_duration_ms + dynamic_duration_factor * degree);

    delay(degree);
    old_max = max;
  }
  
  old_max = baseline_iir_filter_coef * old_max + (1 - baseline_iir_filter_coef) * max;
}