Qiita Conference 2025

こにふぁー (@konifar)

提案のレベルを上げる - 推進力のあるエンジニアになるための思考法

1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

俺は、「女子に」うける、サウンドメーターを作る(6)(最終章)マイク入力

Last updated at Posted at 2025-03-28

いろいろ、注意

  • 過去ログを見よ
  • 新規タブのわかる人
  • GPIOの5が入力
  • モノラル
  • 最低限、動く。(デジタルフィルターの設計も悪くセンスもない(音響効果人から言うとネットワークもしくは、ダイヤグラムが、悪い(想像)))(今度、聞いてみる(予定は、みてい))
  • いろいろ、言い訳すると、個人開発で環境とか時間の制限があり「タイトル(表題)」が先行して、しまってます。(規約にふれるぐらい、ひどかった、タイトル詐欺とメモ利用)(もう反省)
  • 本文は、制作中で明後日(20250330)の朝ぐらいが目標(睡眠時間をちょっとけづつて完了(20250328))
  • ブロック図(ネットワーク、ダイヤグラム)(設計がだめだめ)(値は、適当)(たぶん理想ダイオードが先でローパスフィルターが後)(たぶん、ここでローパスフィルター(平均)を入れるとレベル(中心(0値))だけになる)

image_original (68).jpg

目的

サウンドメータをマイクで動く様にする

結果

image_original (67).jpg

プログラム



//秋月のOLEDとアイテンドウのOLEDのアドレスは3C
//ssd1306_8ch_SoundLevelMeter_mic_1_uno_1


//ヘッダー
#include <Arduino.h>
//#include <avr/pgmspace.h>
#include <pgmspace.h>
#include <Wire.h>
#include "heart1.h"


//定義
#define MAX_PAGE                   (7)
#define MAX_COL                    (127)

#define COMMAND_MODE               0x80 // continuation bit is set!
#define DATA_MODE                  0x40

#define SET_COLUMN_ADDRESS         0x21 // takes two bytes, start address and end address of display data RAM
#define SET_PAGE_ADDRESS           0x22 // takes two bytes, start address and end address of display data RAM

#define SET_MEMORY_ADDRESSING_MODE 0x20 // takes one byte as given above
#define HORIZONTAL_ADDRESSING_MODE 0x00


//I2Cに配列を転送する
void write_s(uint8_t *str1, uint8_t len1) {

  Wire.beginTransmission(  0x3c  );

  for (int ii = 0; ii < len1; ii++) {

    //一文字出力
    Wire.write(*str1 ++);

  }//for

  Wire.endTransmission();

}//write_s


//セットページアドレス
void setPageAddress(uint8_t start, uint8_t end)
{
  uint8_t databytes[6] = {COMMAND_MODE, SET_PAGE_ADDRESS, COMMAND_MODE, start, COMMAND_MODE, end};
  write_s(databytes, 6);
}//setPageAddress


//セットカラムアクセス
void setColumnAddress(uint8_t start, uint8_t end)
{
  uint8_t databytes[6] = {COMMAND_MODE, SET_COLUMN_ADDRESS, COMMAND_MODE, start, COMMAND_MODE, end};
  write_s(databytes, 6);
}//setColumnAddress


//セットメモリーアドレシングモード
void setMemoryAddressingMode()
{
  uint8_t databytes[4] = {COMMAND_MODE, SET_MEMORY_ADDRESSING_MODE, COMMAND_MODE, HORIZONTAL_ADDRESSING_MODE};
  write_s(databytes, 4);
}//setMemoryAddressingMode


int L1 = 0;
int L2 = 0;
int L3 = 0;
int L4 = 0;

int L5 = 0;
int L6 = 0;
int L7 = 0;
int L8 = 0;

//再表示
void display(void) {

  //範囲の設定 (OLED内部のx,yカウンターを初期化してホームポジション0,0に)
  setPageAddress(0, MAX_PAGE);  // all pages
  setColumnAddress(0, MAX_COL); // all columns

  static uint8_t databytes_ON[2] = {DATA_MODE, 0xFF};
  static uint8_t databytes_OFF[2] = {DATA_MODE, 0x00};

  //L1の回数分、ループする
  for (int i = 0; i < (L1 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 0) + i );
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L1); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i

  //L2の回数分、ループする
  for (int i = 0; i < (L2 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 1) + i );
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L2); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i

  //L3の回数分、ループする
  for (int i = 0; i < (L3 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 2) + i );
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L3); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i

  //L4の回数分、ループする
  for (int i = 0; i < (L4 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 3) + i );
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L4); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i


  //L5の回数分、ループする
  for (int i = 0; i < (L5 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 4) + i);
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L5); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i

  //L6の回数分、ループする
  for (int i = 0; i < (L6 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 5) + i);
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L6); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i

  //L7の回数分、ループする
  for (int i = 0; i < (L7 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 6) + i);
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L7); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i

  //L8の回数分、ループする
  for (int i = 0; i < (L8 + 1); i++) {
    databytes_ON[1] = pgm_read_byte_near(heart1 + (128 * 7) + i);
    write_s(databytes_ON, 2); //0xFFの事
  }//for i
  for (int i = 0; i < ((128 - 1) - L8); i++) {
    write_s(databytes_OFF, 2); //0x00の事
  }//for i

}//display


//SSD1306の初期化
void display_begin(void) {

  //I2Cの初期化
  //Wire.setSDA(A5); //SDA I2Cのポートの変更
  //Wire.setSCL(A4); //SCL I2Cのポートの変更
  Wire.begin(); //Arduino UNO Rev3
  delay(200);

  //SSD1306の初期化スペル(魔法)
  //0x80,0x8D,0x80,0x14,0x80,0xAF
  write_s( (uint8_t*) "\200\215\200\024\200\257", 6);
  delay(100);

  //セットメモリーアドレシングモード (画面の終端に来たら画面の先頭に)
  setMemoryAddressingMode();

}//display_begin


//初期化
void setup() {

  //SSD1306の初期化
  display_begin();

  //再表示
  //display();

}//setup


//メインループ
void loop() {

  //L1 = L2 = L3 = L4 = random(128);
  //L5 = L6 = L7 = L8 = random(128);


  //アナログ ↓開始
  float s; //input
  float cl; //L ch
  float cr; //R ch

  //L1 L2 L3 L4
  cl = 0.0f;
  for(int i=0;i<8;i++){ //平均化する
  //センサー入力する
    s = ((float)analogRead(5)); //センサーの値
    s = s * (3.3f / 4096.0f);   //電圧こと
    cl = cl + s;
  }
  cl = cl / 8;
  cl = cl - 1.650f;      //中心にする
  if(cl < 0) {cl = 0;}   //マイナス側のカット(マイクだから+-あり)
  cl = cl * 256;         //倍率を掛ける(好みで変える)
  if(cl > 127) {cl = 127;} //MAXを超えた物をカット
  //cl = 64; //debug
  L1 = L2 = L3 = L4 = (int)cl;          //キャストして転記

  //L2
  cr = 0.0f;
  for(int i=0;i<8;i++){ //平均化する
    //センサー入力する
    //s = (float)(analogRead(6)); //センサーの値
    s = (float)(analogRead(5)); //センサーの値 //モノラルの時
    s = s * (3.3f / 4096.0f);   //電圧こと
    cr = cr + s;
  }
  cr = cr / 8;
  cr = cr - 1.650f;      //中心にする
  if(cr < 0) {cr = 0;}   //マイナス側のカット(マイクだから+-あり)
  cr = cr * 256;         //倍率を掛ける(好みで変える)
  if(cr > 127) {cr = 127;} //MAXを超えた物をカット
  //cr = 64+8; //debug
  L5 = L6 = L7 = L8 = (int)cr;          //キャストして転記 
  //アナログ ↑終了


  //再表示
  display();

  //delay(1000); //1秒待つ
  delay(1); //待つ

}//loop


heart1.h



// 'heart128x64', 128x64px heart1.h
const unsigned char heart1 [] PROGMEM = {

  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0x7f, 0x7f, 0x3f, 0x1f, 0x1f, 0x0f, 0x0f, 0x0f, 0x07, 0x07, 0x07, 0x03, 0x03, 0x03, 0x03, 0x03, 
  0x03, 0x03, 0x03, 0x03, 0x03, 0x07, 0x07, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x03, 0x01, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0x3f, 0x1f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xf0, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x0f, 
  0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x80, 0x80, 0xc0, 0xe0, 0xf0, 0xf0, 0xf8, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf0, 
  0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x07, 0x0f, 0x0f, 0x1f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xfc, 0xf8, 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0f, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0xc0, 0x80, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe0, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xfe, 0xfe, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 
  0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff

};


1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?