LoginSignup
2
0

More than 3 years have passed since last update.

段々速くなるメトロノーム

Last updated at Posted at 2021-02-11

M5Stackで段々スピードアップするメトロノームを作ってみました。練習用によかろうと思います。
スピードアップするメトロノームアプリってスマートフォン用にいろいろ出ているので今更ではあるのですが。

IMG_5811.jpg

metro.ino

/*
 *    Speedup Metronome
 *    
 *    2021/2/11
 * 
 */

#include <M5Stack.h>

int vol=9;
int pwdth = 10;

double  tempo=100.0;

double upspeed = 1.0005;

unsigned long tic;
unsigned long intval;
unsigned long lastms;
int ton1;
int runn=0;
int hz = 440;

void setup() {
  M5.begin(true, false, true);
  M5.Power.begin();

  ledcSetup(TONE_PIN_CHANNEL, 0, 10);
  ledcAttachPin(SPEAKER_PIN, TONE_PIN_CHANNEL);

  Serial.begin(115200);
  M5.Lcd.clear(BLACK);
  M5.Lcd.setTextSize(2);
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.println("*** Speedup Metronome ***");

  intval  = (60.0/ (float)tempo) * 1000.0;

  ton1 = 100;  // tone on time msec
  tic = 0;
  lastms = millis(); 

  Serial.printf("intvl %d",intval);

}

int xpos=0;
int ypos=90;
int xpos2=210;  //
int ypos2=130;

bool dodisp = true;
bool toneout = false;

void loop()
{
   M5.update();

   if(M5.BtnC.wasReleased()){
        runn = runn ^ 1;
        if(!runn){
        ToneOff();
        }
       }

   if(M5.BtnA.wasReleased()){
      tempo = tempo-1.0;
      intval  = (60.0/ (float)tempo) * 1000.0;
      if(tempo < 20){
        tempo = 20;
      }

      dodisp=true;
      }

   if(M5.BtnB.wasReleased()){
      tempo = tempo+1.0;
      intval  = (60.0/ (float)tempo) * 1000.0;
      if(tempo > 320){
        tempo = 320;
      }
      dodisp=true;
      }

    if(dodisp){
      int ofset=0;
      M5.Lcd.setTextSize(2);
      if((int)tempo<100){
        ofset = 64;
        M5.Lcd.fillRect(xpos, ypos,64,86,BLACK);
        }
      M5.Lcd.drawNumber((int)tempo, xpos+ofset, ypos, 7);

      M5.Lcd.drawChar('.', xpos2-20, ypos, 7);

      M5.Lcd.setTextSize(1);
      M5.Lcd.drawNumber((tempo*10-(int)tempo*10), xpos2, ypos2, 7);

      dodisp = false;
     }


   if(millis() ==  lastms){
        return;
    }
    lastms = millis();

    if(!runn){
        return;
    }

    tic++ ;
    if(tic >=  intval){
      ToneOn(hz,vol);
      tic = 0;
      Serial.println("ON");
      toneout=true;
      } 

     if((tic > ton1) && toneout){
         ToneOff();
         toneout = false;
         Serial.println("OFF");
         tempo = tempo * upspeed;
         intval  = (60.0/ (float)tempo) * 1000.0;
         dodisp = true;
       }

}


/*
   Tone On/Off
*/

void ToneOn(uint16_t freq, uint16_t vol) {
  ledcSetup(TONE_PIN_CHANNEL, freq, pwdth);
  ledcWrite(TONE_PIN_CHANNEL,0x1FF>>(9-vol));
}

void ToneOff() {
  ledcWriteTone(TONE_PIN_CHANNEL, 0);
  digitalWrite(SPEAKER_PIN, 0);
}

このスケッチでは1クリック毎にテンポが速くなりますが、これですと速いほど速くなるスピードが速くなるので真面目に練習に使うと結構大変です。一定時間、例えば5秒とかづつに一回0.1%テンポアップとかがよいかもしれません。

 

2
0
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
2
0