プログラムの変更 |
 |
#include <SD.h>
#include <SPI.h>
#include <arduino.h>
#include <MusicPlayer.h>
const int knockSensor = A4; // the piezo is
connected to analog pin 0
int sensorReading = 0; // variable to store
the value read from the sensor pin
const int threshold = 10; // threshold value
to decide when the detected sound is a knock
or not
bool on_off_flg = false;
int led = 9; // the PWM pin the LED is
attached to
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
player.beginInMidiFmt();
player.midiWriteData(0xB0, 0x07, 120);
}
void loop()
{
sensorReading = analogRead(knockSensor);
Serial.println(sensorReading);
if (sensorReading >= threshold && on_off_flg==false)
{
player.midiWriteData(0x90, 0x40,
120);
analogWrite(led, 255);
on_off_flg=true;
}
else if (sensorReading < threshold && on_off_flg==true)
{
player.midiWriteData(0x80, 0x40,
120);
analogWrite(led, 0);
on_off_flg=false;
}
}
|
|
midi_single_toneのプロジェクトに戻って、上のようにプログラムを書き換えます。プログラムをボードに転送します。ピエゾをたたくと、音がしてLEDが点灯します。 |
電圧計のみつないだ場合、5V付近を表示します。また「analogWrite(led,
170);」と記述すると電圧計は3.2Vの表示でした。 |
|
|