3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

IoTLT (IoTや電子工作ネタなど)Advent Calendar 2024

Day 17

MIDIシンセサイザユニットとM5StickCPlusで、エアードラム

Last updated at Posted at 2024-12-16

M5Stack用MIDIシンセサイザユニット(SAM2695)を使って、エアードラムを作った。

image.png

M5StickCPlusの内蔵IMUセンサーの角速度(ジャイロ)センサーを使って、振った時に画面が光って、ドラム音がでるようにした。
M5StickCPlusのボタンAを押すと、音を15種類変更することができるようにした。
左右2セット用意することで、エアードラムで遊べる。

スクリーンショット 2024-12-16 20.04.08.png

エアードラムの動画はこちら

#include <M5StickCPlus.h>
#include "M5UnitSynth.h"

M5UnitSynth synth;

// 加速度データ格納用

float accX, accY, accZ;
float gyroX, gyroY, gyroZ;
float gyroMagnitude; 
float gyroMagnitudelimit = 800; //right1000 left800

int mode = 9;  //default for 57:Crash Cymbal 2

void setup() {
  M5.begin();
  M5.Lcd.setRotation(0); // 縦表示に設定
  M5.Lcd.fillScreen(TFT_BLACK); // 画面を黒で初期化
  M5.Lcd.setTextColor(TFT_WHITE);
  M5.Lcd.setCursor(50, 100, 4);
  M5.Lcd.print("start");
  M5.IMU.Init();
  Serial.begin(115200);
  synth.begin(&Serial2, UNIT_SYNTH_BAUD, 33, 32);
  synth.setInstrument(0, 9, SynthDrum);  // synth drum
}

void loop() {
  M5.update();
  auto now = millis();

  M5.IMU.getAccelData(&accX, &accY, &accZ);
  M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ);
  gyroMagnitude = sqrt((gyroX * gyroX) + (gyroY * gyroY) + (gyroZ * gyroZ));

  if (M5.BtnA.wasPressed()){
    mode = (mode + 1) % 15;
      
      switch (mode) {
        case 0:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Snare Drum1");
          break;
        case 1:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Hand Clap");
          break;
        case 2:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Closed Hi Hat");
          break;
        case 3:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Pedal Hi-Hat");
          break;
        case 4:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Crash Cymbal1");
          break;
        case 5:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Ride Cymbal1");
          break;
        case 6:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Tambourine");
          break;
        case 7:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Splash Cymbal");
          break;
        case 8:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Cowbell");
          break;
        case 9:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Crash Cymbal2");
          break;
        case 10:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("High Timbale");
          break;
        case 11:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Low Timbale");
          break;
        case 12:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Low Agogo");
          break;
        case 13:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Long Whisle");
          break;
        case 14:
          M5.Lcd.fillScreen(TFT_BLACK);
          M5.Lcd.setCursor(0, 100, 4);
          M5.Lcd.print("Short Guiro");
          break;
      }
  }  
    
  if(gyroMagnitude > gyroMagnitudelimit){
      switch (mode) {
        case 0:
          synth.setNoteOn(9, 38, 127); //38: Snare Drum1
          glitter();  
          break;
        case 1:
          synth.setNoteOn(9, 39, 127); //39: Hand Clap
          glitter();
          break;
        case 2:
          synth.setNoteOn(9, 42, 127); //42: Closed Hi Hat
          glitter();  
          break;
        case 3:
          synth.setNoteOn(9, 44, 127); //44: Pedal Hi-Hat
          glitter();
          break;
        case 4:
          synth.setNoteOn(9, 49, 127); //49: Crash Cymbal 1
          glitter();  
          break;
        case 5:
          synth.setNoteOn(9, 51, 127); //51: Ride Cymbal 1
          glitter();
          break;
        case 6:
          synth.setNoteOn(9, 54, 127); //54: Tambourine
          glitter();  
          break;
        case 7:
          synth.setNoteOn(9, 55, 127); //55: Splash Cymbal
          glitter();
          break;
        case 8:
          synth.setNoteOn(9, 56, 127); //56: Cowbell
          glitter();  
          break;
        case 9:
          synth.setNoteOn(9, 57, 127); //57: Crash Cymbal 2
          glitter();
          break;
        case 10:
          synth.setNoteOn(9, 65, 127); //65: High Timbale
          glitter();  
          break;
        case 11:
          synth.setNoteOn(9, 66, 127); //66: Low Timbale
          glitter();
        break;
        case 12:
          synth.setNoteOn(9, 68, 127); //68: Low Agogo
          glitter();  
          break;
        case 13:
          synth.setNoteOn(9, 72, 127); //72: Long Whisle
          glitter();
          break;
        case 14:
          synth.setNoteOn(9, 73, 127); //73: Short Guiro
          glitter();
          break;
      }
  }
}


void glitter(){
  M5.Lcd.fillRect(60, 110, 15, 20, WHITE);
  delay(15);
  M5.Lcd.fillRect(45, 80, 45, 80, WHITE);
  delay(15);
  M5.Lcd.fillRect(30, 50, 75, 140, WHITE);
  delay(15);
  M5.Lcd.fillScreen(WHITE);
  delay(20);
  M5.Lcd.fillScreen(BLACK);
}

おわりに

MIDIシンセサイザユニットを使い、振ることでドラム音が鳴る仕組みを作りました。この小さな軽いユニットを楽しむ方法として考えたのがエアードラムです。ドラム音・パーカッション音が身近になりました。

3
1
0

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?