Digital Electronics Lecture 3
PART 1: This midi keyboard is different from the previous ones we have done. In this keyboard there is no delay when a button is pressed and when a sound is heard. This midi keyboard also has a variable sustain. I accomplished this using if statements that check for the exact moment a button is pressed and released to send MIDI information. This works more precisely, rather than using the send midi function off after the delay.
int ledPin1 = 28; //setting pins and variable names
int ledPin2 = 29;
int ledPin3 = 30;
int ledPin4 = 31;
int buttonPin1 = 33;
int buttonPin2 = 34;
int buttonPin3 = 35;
int buttonPin4 = 36;
bool buttonPress1= LOW; //boolian
bool lastButtonPress1= LOW;
bool buttonPress2= LOW;
bool lastButtonPress2= LOW;
bool buttonPress3= LOW;
bool lastButtonPress3= LOW;
bool buttonPress4= LOW;
bool lastButtonPress4= LOW;
void setup() {
Serial.begin(9600);
pinMode(ledPin1, OUTPUT); //pin mode
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void loop() { //checkbutton function
checkButton();
}
void checkButton(){
checkButton1();
checkButton2();
checkButton3();
checkButton4();
}
void checkButton1(){ //checkbutton function for each button
lastButtonPress1=buttonPress1;
buttonPress1=digitalRead(buttonPin1);
if( (buttonPress1 == HIGH) and (lastButtonPress1 == LOW)){
usbMIDI.sendNoteOn(60, 90, 1);
digitalWrite(ledPin1, HIGH);
delay(5); //setting the delay to 5
} else if (lastButtonPress1 == HIGH and buttonPress1== LOW){
usbMIDI.sendNoteOff(60, 0, 1);
digitalWrite(ledPin1, LOW);
delay(5);
}
}
void checkButton2(){
lastButtonPress2=buttonPress2;
buttonPress2=digitalRead(buttonPin2);
if( (buttonPress2 == HIGH) and (lastButtonPress2 == LOW)){
usbMIDI.sendNoteOn(64, 90, 1);
digitalWrite(ledPin2, HIGH);
delay(5);
} else if (lastButtonPress2 == HIGH and buttonPress2== LOW){
usbMIDI.sendNoteOff(64, 0, 1);
digitalWrite(ledPin2, LOW);
delay(5);
}
}
void checkButton3(){
lastButtonPress3=buttonPress3;
buttonPress3=digitalRead(buttonPin3);
if( (buttonPress3 == HIGH) and (lastButtonPress3 == LOW)){
usbMIDI.sendNoteOn(67, 90, 1);
digitalWrite(ledPin3, HIGH);
delay(5);
} else if (lastButtonPress3 == HIGH and buttonPress3 == LOW){
usbMIDI.sendNoteOff(67, 0, 1);
digitalWrite(ledPin3, LOW);
delay(5);
}
}
PART 2: Potentiometer demo
int potVal = 0; //setting up variables and starting values
int lastPotVal = 0;
void setup() { //serial begin
Serial.begin(9600);
}
void loop() { //loop using the map function.
lastPotVal = potVal;
potVal = map(analogRead(A13), 0 , 1023, 0, 10);
if(potVal != lastPotVal) {
Serial.println(potVal); //prints value in serial monitor
delay(10);
}
}