Created
May 15, 2021 07:15
-
-
Save botamochi6277/d7ade932122ed557d0b456c21e0ec2fe to your computer and use it in GitHub Desktop.
M5Atom using Microphone
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
/** | |
* @file M5AtomMic.ino | |
* @author botamochi6277 | |
* @brief Analog Microphone Test fot M5Atom Series | |
* @version 0.1 | |
* @date 2021-05-07 | |
* | |
* | |
* @licence MIT LICENCE | |
* | |
*/ | |
#include "M5Atom.h" | |
int const ANALOG_PIN = 33; | |
// buffer for moving avarage filter | |
int const NUM_BUFFER = 5; | |
int buffer[NUM_BUFFER]; | |
void setup() | |
{ | |
M5.begin(true, false, true); | |
pinMode(ANALOG_PIN, INPUT); | |
M5.IMU.Init(); | |
} | |
void loop() | |
{ | |
M5.update(); | |
int value = analogRead(ANALOG_PIN); // 0--4096 | |
int v = abs(value - 2048); | |
v = map(v, 0, 2048, 0, 255); | |
pushBack(buffer, v); // update buffer values | |
int magnitude = average(buffer); | |
// CHSV (uint8_t ih, uint8_t is, uint8_t iv) | |
CHSV color = CHSV(127, 255, magnitude); | |
M5.dis.clear(); | |
if (magnitude > 32) | |
{ | |
M5.dis.drawpix(0, color); | |
} | |
Serial.print(value); | |
Serial.print(","); | |
Serial.println(magnitude); | |
delay(50); | |
} | |
int average(int *values) | |
{ | |
int sum = 0; | |
for (int i = 0; i < NUM_BUFFER; i++) | |
{ | |
sum += values[i]; | |
} | |
return sum / NUM_BUFFER; | |
} | |
int pushBack(int *values, int new_value) | |
{ | |
for (int i = NUM_BUFFER - 1; i > 0; i--) | |
{ | |
values[i] = values[i - 1]; | |
} | |
values[0] = new_value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment