Stap 6: Code
Nu is het tijd voor het uploaden van de code aan uw Arduino. Hier is het:snare
Const int NumSnareReadings = 7;
int SnareReadings [NumSnareReadings];
int SnareIndex = 0;
int SnareTotal = 0;
int SnareAverage = 0;
int SnareMax = 700;
int SnarePin = A2;
Hi hat
Const int NumHatReadings = 7;
int HatReadings [NumHatReadings];
int HatIndex = 0;
int HatTotal = 0;
int HatAverage = 0;
int HatMax = 700;
int HatPin = A0;
bass-pedaal
Const int NumBassReadings = 7;
int BassReadings [NumBassReadings];
int BassIndex = 0;
int BassTotal = 0;
int BassAverage = 0;
int BassMax = 700;
int BassPin = A4;
crash (hi hat pedaal omhoog)
int PedalPin = A3;
int licht = analogRead(A3);
Const int NumCrashReadings = 7;
int CrashReadings [NumCrashReadings];
int CrashIndex = 0;
int CrashTotal = 0;
int CrashAverage = 0;
int CrashMax = 700;
int CrashPin = A5;
int drempel = 125;
Millis functie te geven van een vertraging tussen de noten van de MIDI
lange PreviousMillis1 = 0;
lange PreviousMillis2 = 0;
lange PreviousMillis3 = 0;
lange PreviousMillis4 = 0;
lange Interval1 = 100;
lange Interval2 = 100;
lange Interval3 = 100;
lange Interval4 = 100;
VOID Setup {}
Serial.begin(31250);
pinMode (SnarePin, INPUT);
pinMode (HatPin, INPUT);
pinMode (BassPin, INPUT);
pinMode (PedalPin, INPUT);
analogReference(EXTERNAL);
voor (int ThisSnareReading = 0; ThisSnareReading < NumSnareReadings; ThisSnareReading++)
SnareReadings [ThisSnareReading] = 0;
voor (int ThisHatReading = 0; ThisHatReading < NumHatReadings; ThisHatReading++)
HatReadings [ThisHatReading] = 0;
voor (int ThisBassReading = 0; ThisBassReading < NumBassReadings; ThisBassReading++)
BassReadings [ThisBassReading] = 0;
}
void loop)
{
unsigned long CurrentMillis = millis();
snare
SnareTotal = SnareTotal - SnareReadings [SnareIndex];
SnareReadings[SnareIndex]=analogRead(SnarePin);
SnareTotal = SnareTotal + SnareReadings [SnareIndex];
SnareIndex = SnareIndex + 1;
Als (SnareIndex > = NumSnareReadings)
SnareIndex = 0;
SnareAverage = SnareTotal / NumSnareReadings;
Als (SnareAverage > SnareMax & & (CurrentMillis-PreviousMillis1) > Interval1)
{
PreviousMillis1 = CurrentMillis;
playMidiNote (1, 38, 127);
}
Hi hat
HatTotal = HatTotal - HatReadings [HatIndex];
HatReadings[HatIndex]=analogRead(HatPin);
HatTotal = HatTotal + HatReadings [HatIndex];
HatIndex = HatIndex + 1;
Als (HatIndex > = NumHatReadings)
{HatIndex = 0;
HatAverage = HatTotal / NumHatReadings;
Als (HatAverage > HatMax & & (CurrentMillis-PreviousMillis2) > Interval2 & & analogRead(A3) < drempel)
{playMidiNote (1, 42, 127);}
}
crash
CrashTotal = CrashTotal - CrashReadings [CrashIndex];
CrashReadings[CrashIndex]=analogRead(CrashPin);
CrashTotal = CrashTotal + CrashReadings [CrashIndex];
CrashIndex = CrashIndex + 1;
Als (CrashIndex > = NumCrashReadings)
{CrashIndex = 0;
CrashAverage = CrashTotal / NumCrashReadings;
Als (CrashAverage > CrashMax & & (CurrentMillis-PreviousMillis4) > Interval4 & & analogRead(A3) > = drempel)
{playMidiNote (1, 46, 127);}
}
bass-pedaal
BassTotal = BassTotal - BassReadings [BassIndex];
BassReadings[BassIndex]=analogRead(BassPin);
BassTotal = BassTotal + BassReadings [BassIndex];
BassIndex = BassIndex + 1;
Als (BassIndex > = NumBassReadings)
BassIndex = 0;
BassAverage = BassTotal / NumBassReadings;
Als (BassAverage > BassMax & & (CurrentMillis-PreviousMillis3 > Interval3))
{
PreviousMillis3 = CurrentMillis;
playMidiNote(1,36, 127);
}
}
VOID playMidiNote (byte channel, byte opmerking, byte snelheid)
{
byte midiMessage = 0x90 + (kanaal - 1);
Serial.write(midiMessage);
Serial.write(Note);
Serial.write(Velocity);
}