我正在设计一个基于arduino-uno的电子竖琴,使用LDR作为琴弦(按下音符时按下LDR)。它是以midi为基础的。它目前有四个LDR,按下时可以播放正确的音符。当按下两个LDR来扩大范围时,我试图让它发挥不同的音符。当我有一对LDR的代码但是当我添加代码一秒时,这听起来非常错误,播放咆哮的声音。
这是一对LDR的代码:
for (nt=0;nt<notes;nt++)
{
val[nt]=analogRead(Det[nt]);
delay(1);
if (val[0]<threshold[1]&&val[1]<threshold[1]){
if(prev_nts[nt] == false) {
digitalWrite(LED[0], HIGH); //TURN led ON if value below threshold
digitalWrite(LED[1], HIGH);
noteOn(0, music[0], vol[nt]); //note (middle number) 40-62, (channel, pitch, volume from array)
prev_nts[nt] = true;
}}
else {
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
//TURN led OFF if value below threshold
noteOff(0, music[0], 0); //note (middle number) 40-62
prev_nts[nt] = false;
}
}
这是我对两对LDR的代码:
for (nt=0;nt<notes;nt++)
{
val[nt]=analogRead(Det[nt]);
delay(1);
if (val[0]<threshold[1]&&val[1]<threshold[1]){
if(prev_nts[nt] == false) {
digitalWrite(LED[0], HIGH); //TURN led ON if value below threshold
digitalWrite(LED[1], HIGH);
noteOn(0, music[0], vol[nt]); //note (middle number) 40-62, (channel, pitch, volume from array)
prev_nts[nt] = true;
}}
else {
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
//TURN led OFF if value below threshold
noteOff(0, music[0], 0); //note (middle number) 40-62
prev_nts[nt] = false;
}
if (val[2]<threshold[2]&&val[3]<threshold[3]){
if(prev_nts[nt] == false) {
digitalWrite(LED[2], HIGH); //TURN led ON if value below threshold
digitalWrite(LED[3], HIGH);
noteOn(0, music[1], vol[nt]); //note (middle number) 40-62, (channel, pitch, volume from array)
prev_nts[nt] = true;
}}
else {
digitalWrite(LED[2], LOW);
digitalWrite(LED[3], LOW);
//TURN led OFF if value below threshold
noteOff(0, music[1], 0); //note (middle number) 40-62
prev_nts[nt] = false;
}
}
这是我的全部代码:
// 4 channel photodetector and LED indicator.
// Kate Sugden, August 2017
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
// pins for photodetectors and LED indicators
byte ledPin = 13; //MIDI traffic inidicator
int Det[] = {A0,A1,A2,A3}; // defines Detecotr pins
int LED[] = {11,10,9,8}; // defines LED indicator pins
int notes = 4; //number of notes in harp
int val [] = {0,0,0,0}; // sets all the values of the detector readings to zero - need as many as notes
int threshold [] = {0,0,0,0};
boolean prev_nts[] = {false,false,false,false};
int music[] = {48,50,52,53};
boolean notePressed = false;
// defined delays and thresholds
int nt = 0; // note number
int timer = 10; // delay option
//MIDI
byte note = 0; //The MIDI note value to be played
byte resetMIDI = 4; //Tied to VS1053 Reset line
// int instrument = 118; //Harp is 47, 80 Ocarina, 46 Pizzicato strings (most convincing) percussion 36, 118 synth drum
int instrument[] = {118,117,49,115};
int definst=46; //default instrument
int vol[]={100,100,100,100}; //sets volume (127 is max)
void setup()
{
Serial.begin(9600);
// Serial.begin(31250);
// we make all the declarations at once
for (nt=0;nt<notes;nt++)
{
pinMode(Det[nt], INPUT); //declares detectors
pinMode(LED[nt], OUTPUT); //declares indicators
val[nt]=analogRead(Det[nt]); //this reads the LDR values once
threshold[nt]=val[nt]-20; // this then calculates a threshold value for each channel based on starting ambient light and sets the threshold X below this. .
}
//this bit configures the midi
mySerial.begin(31250);
pinMode(resetMIDI, OUTPUT);
digitalWrite(resetMIDI, LOW);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
talkMIDI(0xB0, 0x07, vol[0]); //0xB0 is channel message, set channel volume (3rd number) to near max (127)
talkMIDI(0xB0, 0, 0x79);
talkMIDI(0xC0, definst, 0);
}
void loop()
{
for (nt=0;nt<notes;nt++)
{
val[nt]=analogRead(Det[nt]);
delay(1);
if (val[0]<threshold[1]&&val[1]<threshold[1]){
if(prev_nts[nt] == false) {
digitalWrite(LED[0], HIGH); //TURN led ON if value below threshold
digitalWrite(LED[1], HIGH);
noteOn(0, music[0], vol[nt]); //note (middle number) 40-62, (channel, pitch, volume from array)
prev_nts[nt] = true;
}}
else {
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
//TURN led OFF if value below threshold
noteOff(0, music[0], 0); //note (middle number) 40-62
prev_nts[nt] = false;
}
if (val[2]<threshold[2]&&val[3]<threshold[3]){
Serial.println(notePressed); //never reaching here
if(prev_nts[nt] == false) {
digitalWrite(LED[2], HIGH); //TURN led ON if value below threshold
digitalWrite(LED[3], HIGH);
noteOn(0, music[1], vol[nt]); //note (middle number) 40-62, (channel, pitch, volume from array)
prev_nts[nt] = true;
}}
else {
digitalWrite(LED[2], LOW);
digitalWrite(LED[3], LOW);
//TURN led OFF if value below threshold
noteOff(0, music[1], 0); //note (middle number) 40-62
prev_nts[nt] = false;
}
}
}
void noteOn(byte channel, byte note, byte attack_velocity) {
talkMIDI( (0x90 | channel), note, attack_velocity);
}
//Send a MIDI note-off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) {
talkMIDI( (0x80 | channel), note, release_velocity);
}
void talkMIDI(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin, HIGH);
mySerial.write(cmd);
mySerial.write(data1);
//Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes
//(sort of: http://253.ccarh.org/handout/midiprotocol/)
if( (cmd & 0xF0) <= 0xB0)
mySerial.write(data2);
digitalWrite(ledPin, LOW);
}
这也是我第一次使用arduino编程,所以我可能不会意识到简单的错误。我通常用Java和Python编程。