Diferencia entre revisiones de «Adxl345»

Sin resumen de edición
Sin resumen de edición
 
Línea 1: Línea 1:
=1=
<syntaxhighlight lang=c++>
<syntaxhighlight lang=c++>
#include <float.h>
#include <float.h>
Línea 7: Línea 6:


const int data_points = 10;
const int data_points = 10;
const int detection_ratio = 3.0;
const int speaker_pin = 3;
const int buzzer_pin = 11;


Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();
const float dynamic_pitch_factor = 5;
 
const float dynamic_duration_factor = 10;
void setup(void) {
const int base_duration_ms = 40;
  while (!accel.begin()) {
const float base_freq = 100;
    delay(100);
const float note_interval = 1.05946;
  }
const float detection_threshold = 0.5;
 
  accel.setRange(ADXL345_RANGE_16_G);
  accel.setDataRate(ADXL345_DATARATE_3200_HZ);
  pinMode(buzzer_pin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}


void loop(void) {
#define NUM_NOTES 32
  static float old_diff = 1.0;
  sensors_event_t event;
 
  float max = FLT_MAX * -1;
  float min = FLT_MAX;


  for (int i = 0; i < data_points; i++) {
int notes[NUM_NOTES] = {0, 2, 3, 5, 7, 8, 10};
    accel.getEvent(&event);
    max = max(event.acceleration.x, max);
    min = min(event.acceleration.x, min);
  }
 
  const float new_diff = max - min;
  const bool detected = (new_diff / old_diff) > detection_ratio;
 
  digitalWrite(buzzer_pin, detected);
  digitalWrite(LED_BUILTIN, detected);
 
  old_diff = new_diff;
}
</syntaxhighlight>
 
=2=
<syntaxhighlight lang=c++>
#include <float.h>
 
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
 
const int data_points = 10;
const int speaker_pin = 3;
//const int notes[] = {0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23, 24, 26, 28, 29, 31, 33, 35, 36};
const int notes[] = {-3, -1, 0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23, 24, 26, 28, 29, 31, 33, 35, 36};
const int dynamic_pitch_factor = 5;
const int dynamic_duration_factor = 30;
const int base_duration_ms = 40;
const float base_freq = 160;
const float note_interval = 1.05946;
const float detection_threshold = 0.4;


Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();


void setup(void) {
void setup(void) {
  Serial.begin(115200);
   while (!accel.begin()) {
   while (!accel.begin()) {
     delay(100);
     delay(100);
   }
   }


   accel.setRange(ADXL345_RANGE_16_G);
   accel.setRange(ADXL345_RANGE_2_G);
   accel.setDataRate(ADXL345_DATARATE_3200_HZ);
   accel.setDataRate(ADXL345_DATARATE_3200_HZ);
   pinMode(speaker_pin, OUTPUT);
   pinMode(speaker_pin, OUTPUT);
  for (int i = 7; i < NUM_NOTES; i++) {
    notes[i] = notes[i - 7] + 12;
  }
}
}
float old_max = 50;


void loop(void) {
void loop(void) {
  static float old_max = 0;
   float max = FLT_MAX * -1;
   float max = FLT_MAX * -1;
 
  sensors_event_t event;
   for (int i = 0; i < data_points; i++) {
   for (int i = 0; i < data_points; i++) {
    sensors_event_t event;
     accel.getEvent(&event);
     accel.getEvent(&event);
     max = max(event.acceleration.x, max);
     max = max(event.acceleration.x, max);
    if (false) {
    Serial.print(event.acceleration.x);
    Serial.print(" ");
    Serial.print(event.acceleration.y);
    Serial.print(" ");
    Serial.println(event.acceleration.z);
    }   
   }
   }
  max = 20 * sqrt(sqrt(max));


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


   if (detected) {
   if (detected) {
     int degree = dynamic_pitch_factor * (max - old_max) - 4;
     int degree = dynamic_pitch_factor * (max - old_max)- 1.5;
     degree = max(degree, 0);
    const int first_index = 1;
     degree = min(degree, (sizeof(notes) - 1));
    const int last_index = sizeof(notes) / sizeof(*notes) - 1;
     degree = max(degree, first_index);
     degree = min(degree, last_index);
     const float freq = base_freq * pow(note_interval, notes[degree]);
     const float freq = base_freq * pow(note_interval, notes[degree]);
     tone(speaker_pin, freq, base_duration_ms + dynamic_duration_factor * degree);
     tone(speaker_pin, freq, base_duration_ms + dynamic_duration_factor * degree);
    const String s = String(max) + " " + String(old_max) + " " + degree + " " + notes[degree] + "\n";
    Serial.print(s);
    delay(degree);
    old_max = max;
   }
   }


   old_max = max;
   old_max = 0.6 * old_max + 0.4 * max;
}
}
</syntaxhighlight>
</syntaxhighlight>