Digital Lecture 3

Aidan Singh

Part 1: A Better Synth

This synth is different from the previous ones I’ve made in that it is polyphonic, has a variable sustain, and no perceivable delay. This is due to the use of if statements that check for the exact moment a button is pressed and released to send MIDI information, rather than using delays to turn off MIDI notes.



int ledPin1 = 28;
int ledPin2 = 29;
int ledPin3 = 30;
int ledPin4 = 31;
int buttonPin1 = 33;
int buttonPin2 = 34;
int buttonPin3 = 35;
int buttonPin4 = 36;
bool buttonState1= LOW;
bool lastButtonState1= LOW;
bool buttonState2= LOW;
bool lastButtonState2= LOW;
bool buttonState3= LOW;
bool lastButtonState3= LOW;
bool buttonState4= LOW;
bool lastButtonState4= LOW;

void setup() {
    Serial.begin(9600);

    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);

    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
    pinMode(buttonPin3, INPUT);
    pinMode(buttonPin4, INPUT);

}

void loop() {
  checkButtons();

}

void checkButtons(){
  checkButton1();
  checkButton2();
  checkButton3();
  checkButton4();
  
}

void checkButton1(){
  lastButtonState1=buttonState1;
  buttonState1=digitalRead(buttonPin1);
  if( (buttonState1 == HIGH) and (lastButtonState1 == LOW)){
    usbMIDI.sendNoteOn(60, 90, 1);
    digitalWrite(ledPin1, HIGH);
    delay(5);
  } else if (lastButtonState1 == HIGH and buttonState1== LOW){
    usbMIDI.sendNoteOff(60, 0, 1);
    digitalWrite(ledPin1, LOW);
    delay(5);
  }
}

void checkButton2(){
  lastButtonState2=buttonState2;
  buttonState2=digitalRead(buttonPin2);
  if( (buttonState2 == HIGH) and (lastButtonState2 == LOW)){
    usbMIDI.sendNoteOn(64, 90, 1);
    digitalWrite(ledPin2, HIGH);
    delay(5);
  } else if (lastButtonState2 == HIGH and buttonState2== LOW){
    usbMIDI.sendNoteOff(64, 0, 1);
    digitalWrite(ledPin2, LOW);
    delay(5);
  }
}

void checkButton3(){
  lastButtonState3=buttonState3;
  buttonState3=digitalRead(buttonPin3);
  if( (buttonState3 == HIGH) and (lastButtonState3 == LOW)){
    usbMIDI.sendNoteOn(67, 90, 1);
    digitalWrite(ledPin3, HIGH);
    delay(5);
  } else if (lastButtonState3 == HIGH and buttonState3 == LOW){
    usbMIDI.sendNoteOff(67, 0, 1);
    digitalWrite(ledPin3, LOW);
    delay(5);
  }
}

void checkButton4(){
  lastButtonState4=buttonState4;
  buttonState4=digitalRead(buttonPin4);
  if( (buttonState4 == HIGH) and (lastButtonState4 == LOW)){
    usbMIDI.sendNoteOn(72, 90, 1);
    digitalWrite(ledPin4, HIGH);
    delay(5);
  } else if (lastButtonState4 == HIGH and buttonState4 == LOW){
    usbMIDI.sendNoteOff(72, 0, 1);
    digitalWrite(ledPin4, LOW);
    delay(5);
  }
}


Part 2: Demonstrating Better Potentiometer Use

int potVal = 0;
int lastPotVal = 0;


void setup() {
  Serial.begin(9600);

}

void loop() {
  lastPotVal = potVal;
  potVal = map(analogRead(A13), 0 , 1023, 0, 10);
  if(potVal != lastPotVal) {
    Serial.println(potVal);
    delay(10);
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started