Hola como estan? soy nuevo nuevo, queria comentarles que se muy poco de programación y algo pude hacer, resulta que este código lo saqué de un proyecto que encontré en internet el cual consiste en usar el arduino nano como una batería midi mediante sensores, también en la entrada digital se le puede poner un switch para activar el bombo de la batería. Mi problema es que al pisar el switch, si, activa el bombo pero suena muy bajo en el programa, es como si mandara una señal muy baja el switch, es como pasaría con los sensores si los golpeamos despacio, mandaria un voltaje muy bajo y se traduciría a un sonido muy bajito. Como puedo hacer que al apretar el switch el bombo suene fuerte? osea, que se tome un valor alto? la verdad del código este no entiendo nada, por favor ayuda.
//Piezo defines
#define hihat 4
#define pedalbombo 3
#define NUM_PIEZOS 8
#define PAD2_THRESHOLD 10
#define PAD3_THRESHOLD 50
#define PAD4_THRESHOLD 10
#define PAD5_THRESHOLD 20 //MIN/MAX VELOCITIES/sensitivity...
#define PAD6_THRESHOLD 10
#define PAD7_THRESHOLD 10
#define PAD8_THRESHOLD 200
#define START_SLOT 0 //0 START ANALOG INPUTS
//MIDI note defines for each trigger
//pad1 kick + // <these are the assigned midi notes, change to suit.
#define PAD2_NOTE 38 //pad2 snare +
#define PAD3_NOTE 49 //pad3 crash 1
#define PAD4_NOTE 22 //pad4 HAT SWITCHES MIDI NOTES BETWEEN 17/22.(CLOSED/OPEN HAT)
#define PAD5_NOTE 53 //pad5 ride bell + //(SEE LINES 129&231=22)TO CHANGE CLOSED HAT SOUND
#define PAD6_NOTE 91 //pad6 ride cymbal + MIDI NOTE NUMBERS..
#define PAD7_NOTE 48 //pad7 mid tom
#define PAD8_NOTE 41 //pad8 lo floor tom
//MIDI defines
#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define MAX_MIDI_VELOCITY 127
//MIDI baud rate
#define SERIAL_RATE 256000
//Program defines
//ALL TIME MEASURED IN MILLISECONDS
#define SIGNAL_BUFFER_SIZE 50
#define PEAK_BUFFER_SIZE 10
#define MAX_TIME_BETWEEN_PEAKS 10
#define MIN_TIME_BETWEEN_NOTES 10
//map that holds the mux slots of the piezos
unsigned short slotMap[NUM_PIEZOS];
//map that holds the respective note to each piezo
unsigned short noteMap[NUM_PIEZOS];
//map that holds the respective threshold to each piezo
unsigned short thresholdMap[NUM_PIEZOS];
//Ring buffers to store analog signal and peaks
short currentSignalIndex[NUM_PIEZOS];
short currentPeakIndex[NUM_PIEZOS];
unsigned short signalBuffer[NUM_PIEZOS][SIGNAL_BUFFER_SIZE];
unsigned short peakBuffer[NUM_PIEZOS][PEAK_BUFFER_SIZE];
boolean noteReady[NUM_PIEZOS];
unsigned short noteReadyVelocity[NUM_PIEZOS];
boolean isLastPeakZeroed[NUM_PIEZOS];
unsigned long lastPeakTime[NUM_PIEZOS];
unsigned long lastNoteTime[NUM_PIEZOS];
//Variables used for switch mod
byte hihat_switch = 0;
unsigned long switch_pressed = 0;
byte bombo_switch = 0;
unsigned long bomboswitch_pressed = 0;
void setup()
{
//pinMode(hihat, INPUT_PULLUP);
pinMode(pedalbombo, INPUT_PULLUP);
Serial.begin(SERIAL_RATE);
//initialize globals
for(short i=0; i<NUM_PIEZOS; ++i)
{
currentSignalIndex[i] = 0;
currentPeakIndex[i] = 0;
memset(signalBuffer[i],0,sizeof(signalBuffer[i]));
memset(peakBuffer[i],0,sizeof(peakBuffer[i]));
noteReady[i] = false;
noteReadyVelocity[i] = 0;
isLastPeakZeroed[i] = true;
lastPeakTime[i] = 0;
lastNoteTime[i] = 0;
slotMap[i] = START_SLOT + i;
}
thresholdMap[1] = PAD2_THRESHOLD;
thresholdMap[2] = PAD3_THRESHOLD;
thresholdMap[3] = PAD4_THRESHOLD; //hi hat switch controlled
thresholdMap[4] = PAD5_THRESHOLD;
thresholdMap[5] = PAD6_THRESHOLD;
thresholdMap[6] = PAD7_THRESHOLD;
thresholdMap[7] = PAD8_THRESHOLD;
noteMap[1] = PAD2_NOTE;
noteMap[2] = PAD3_NOTE;
noteMap[3] = PAD4_NOTE; // hi hat switch controlled
noteMap[4] = PAD5_NOTE;
noteMap[5] = PAD6_NOTE;
noteMap[6] = PAD7_NOTE;
noteMap[7] = PAD8_NOTE;
}
void loop()
{
unsigned long currentTime = millis();
//if(currentTime > (switch_pressed + 2))
//{
//hihat_switch = (((hihat_switch << 1) | digitalRead(3)) & 3 );
//if(hihat_switch == 2){
//noteFire(36, 5); // <CHANGE THIS NUMBER '22' FOR 'OPEN HAT' SOUND.
//switch_pressed = currentTime;
//}
if(currentTime > (bomboswitch_pressed + 2))
{
bombo_switch = (((bombo_switch << 1) | digitalRead(3)) & 3 );
if(bombo_switch == 2){
noteFire(36, 10);
bomboswitch_pressed = currentTime;
}
}
for(short i=0; i<NUM_PIEZOS; ++i)
{
//get a new signal from analog read
unsigned short newSignal = analogRead(slotMap[i]);
signalBuffer[i][currentSignalIndex[i]] = newSignal;
//if new signal is 0
if(newSignal < thresholdMap[i])
{
if(!isLastPeakZeroed[i] && (currentTime - lastPeakTime[i]) > MAX_TIME_BETWEEN_PEAKS)
{
recordNewPeak(i,0);
}
else
{
//get previous signal
short prevSignalIndex = currentSignalIndex[i]-1;
if(prevSignalIndex < 0) prevSignalIndex = SIGNAL_BUFFER_SIZE-1;
unsigned short prevSignal = signalBuffer[i][prevSignalIndex];
unsigned short newPeak = 0;
//find the wave peak if previous signal was not 0 by going
//through previous signal values until another 0 is reached
while(prevSignal >= thresholdMap[i])
{
if(signalBuffer[i][prevSignalIndex] > newPeak)
{
newPeak = signalBuffer[i][prevSignalIndex];
}
//decrement previous signal index, and get previous signal
prevSignalIndex--;
if(prevSignalIndex < 0) prevSignalIndex = SIGNAL_BUFFER_SIZE-1;
prevSignal = signalBuffer[i][prevSignalIndex];
}
if(newPeak > 0)
{
recordNewPeak(i, newPeak);
}
}
}
currentSignalIndex[i]++;
if(currentSignalIndex[i] == SIGNAL_BUFFER_SIZE) currentSignalIndex[i] = 0;
}
}
void recordNewPeak(short slot, short newPeak)
{
isLastPeakZeroed[slot] = (newPeak == 0);
unsigned long currentTime = millis();
lastPeakTime[slot] = currentTime;
//new peak recorded (newPeak)
peakBuffer[slot][currentPeakIndex[slot]] = newPeak;
//1 of 3 cases can happen:
// 1) note ready - if new peak >= previous peak
// 2) note fire - if new peak < previous peak and previous peak was a note ready
// 3) no note - if new peak < previous peak and previous peak was NOT note ready
//get previous peak
short prevPeakIndex = currentPeakIndex[slot]-1;
if(prevPeakIndex < 0) prevPeakIndex = PEAK_BUFFER_SIZE-1;
unsigned short prevPeak = peakBuffer[slot][prevPeakIndex];
if(newPeak > prevPeak && (currentTime - lastNoteTime[slot])>MIN_TIME_BETWEEN_NOTES)
{
noteReady[slot] = true;
if(newPeak > noteReadyVelocity[slot])
noteReadyVelocity[slot] = newPeak;
}
else if(newPeak < prevPeak && noteReady[slot])
{
noteFire(noteMap[slot], noteReadyVelocity[slot]);
noteReady[slot] = false;
noteReadyVelocity[slot] = 0;
lastNoteTime[slot] = currentTime;
}
currentPeakIndex[slot]++;
if(currentPeakIndex[slot] == PEAK_BUFFER_SIZE) currentPeakIndex[slot] = 0;
}
void noteFire(unsigned short note, unsigned short velocity)
{
if(velocity > MAX_MIDI_VELOCITY)
velocity = MAX_MIDI_VELOCITY;
//if((note == 17) && (hihat_switch == 0)) //if pad 4 is hit (note 17) and switch is pressed change note to 22 //
//{ //
//note = 22; // <CHANGE THIS NUMBER '22' FOR 'OPEN HAT' SOUND.
//}
if((note == NUM_PIEZOS) && (bombo_switch == 0))
{
note = 36;
}
midiNoteOn(note, velocity);
midiNoteOff(note, velocity); //
}
void midiNoteOn(byte note, byte midiVelocity)
{
Serial.write(NOTE_ON_CMD);
Serial.write(note);
Serial.write(midiVelocity);
}
void midiNoteOff(byte note, byte midiVelocity)
{
Serial.write(NOTE_OFF_CMD);
Serial.write(note);
Serial.write(midiVelocity);
}