Stap 5: Trigger een MIDI noot met een knop
Met behulp van de eenvoudige knop ontdendering code uit de eerste klasse (u kunt ook deze ontdendering code die gebruikmaakt van millis() tijd te houden), kabellengte tot een knop op digitale pin 7 en gebruiken voor het activeren van een MIDI-Opmerking:
byte noteON = 144;//note on command int buttonPin = 7; boolean currentState = LOW;//stroage for current button state boolean lastState = LOW;//storage for last button state void setup(){ pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT Serial.begin(9600);//initialize Serial connection } void loop(){ currentState = digitalRead(buttonPin); if (currentState == HIGH && lastState == LOW){//if button has just been pressed MIDImessage(noteON, 60, 127);//turn note 60 on with 127 velocity delay(2);//crude form of button debouncing } else if(currentState == LOW && lastState == HIGH){ MIDImessage(noteON, 60, 0);//turn note 60 off delay(2);//crude form of button debouncing } lastState = currentState; } //send MIDI message void MIDImessage(byte command, byte data1, byte data2) { Serial.write(command); Serial.write(data1); Serial.write(data2); }
In deze schets, telkens wanneer die de knop wordt ingedrukt we een boodschap opmerking op, en telkens wanneer die de knop wordt vrijgegeven, we een signaal opmerking af. Nu proberen om de toonhoogte van de notitie in de potentiometer (aangesloten op A0) toe te voegen:
byte noteON = 144;//note on command byte note;//storage for currently playing note int buttonPin = 7; int potPin = A0; boolean currentState = LOW;//stroage for current button state boolean lastState = LOW;//storage for last button state void setup(){ pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT Serial.begin(9600);//initialize Serial connection } void loop(){ currentState = digitalRead(buttonPin); if (currentState == HIGH && lastState == LOW){//if button has just been pressed int currentPotVal = analogRead(potPin); note = map(currentPotVal, 0, 1023, 0, 127); MIDImessage(noteON, note, 127);//turn note on with 127 velocity delay(2);//crude form of button debouncing } else if(currentState == LOW && lastState == HIGH){ MIDImessage(noteON, note, 0);//turn note off delay(2);//crude form of button debouncing } lastState = currentState; } //send MIDI message void MIDImessage(byte command, byte data1, byte data2) { Serial.write(command); Serial.write(data1); Serial.write(data2); }
Nu we een paar meer notities in de mix toevoegen kunt, speelt deze schets een drie Opmerking zin op elke druk op de knop. U kunt de basis notitie wijzigen met de potmeter:
byte noteON = 144;//note on command byte note;//storage for currently playing note int buttonPin = 7; int potPin = A0; boolean currentState = LOW;//stroage for current button state boolean lastState = LOW;//storage for last button state void setup(){ pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT Serial.begin(9600);//initialize Serial connection } void loop(){ currentState = digitalRead(buttonPin); if (currentState == HIGH && lastState == LOW){//if button has just been pressed int currentPotVal = analogRead(potPin); note = map(currentPotVal, 0, 1023, 0, 127); int noteLength = 200; byte noteVelocity = 127; MIDImessage(noteON, note, noteVelocity);//base note delay(noteLength); MIDImessage(noteON, note, 0);//turn note off MIDImessage(noteON, note+7, noteVelocity);//fifth delay(noteLength); MIDImessage(noteON, note+7, 0);//turn note off MIDImessage(noteON, note+12, noteVelocity);//octave delay(noteLength); MIDImessage(noteON, note+12, 0); } lastState = currentState; } //send MIDI message void MIDImessage(byte command, byte data1, byte data2) { Serial.write(command); Serial.write(data1); Serial.write(data2); }
Tot slot kunnen we een terwijl lus gebruiken om de arpeggio blijven zolang wij druk op de knop. Terwijl loops in principe loopbare zijn als verklaringen. Als het argument binnen de while lus de haakjes geëvalueerd als true, dan de opdrachten die binnen de while lus worden uitgevoerd. Op het einde van de while lus, de Arduino controles om te zien of het argument binnen de while loops van haakjes klopt weer. Als het nog steeds waar is, is de while lus voert alle opdrachten in de culy accolades opnieuw, dit blijft voor altijd tot het binnen de while lus van haakjes resulteert in ONWAAR argument. In het onderstaande voorbeeld gebruik ik de while lus te blijven spelen de drie Opmerking uitdrukking voor zo lang als digitalRead(buttonPin) == HIGH.
byte noteON = 144;//note on command byte note;//storage for currently playing note int buttonPin = 7; int potPin = A0; boolean currentState = LOW;//stroage for current button state boolean lastState = LOW;//storage for last button state void setup(){ pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT Serial.begin(9600);//initialize Serial connection } void loop(){ currentState = digitalRead(buttonPin); if (currentState == HIGH && lastState == LOW){//if button has just been pressed int currentPotVal = analogRead(potPin); note = map(currentPotVal, 0, 1023, 0, 127); int noteLength = 200; byte noteVelocity = 127; while(digitalRead(buttonPin) == HIGH){//as long as the button is pressed, repeat the arpeggio MIDImessage(noteON, note, noteVelocity);//base note delay(noteLength); MIDImessage(noteON, note, 0);//turn note off MIDImessage(noteON, note+7, noteVelocity);//fifth delay(noteLength); MIDImessage(noteON, note+7, 0);//turn note off MIDImessage(noteON, note+12, noteVelocity);//octave delay(noteLength); MIDImessage(noteON, note+12, 0); } } lastState = currentState; } //send MIDI message void MIDImessage(byte command, byte data1, byte data2) { Serial.write(command); Serial.write(data1); Serial.write(data2); }