Digital Electronics Lecture 3

The new button code allows us to have more control over the sound made by pressing the button. In the old code, the button could only play one sound at a time, and only for a set length. With the new code, the sound will sound as long as the button is being pressed, and multiple buttons can be pressed at once. The way that this works is by constantly checking whether or not the button is being pressed. The sound will only turn on if the button is on after just recently being off. Conversely, the sound will only turn off at the moment the being is not being pressed after recently having been pressed. This is to say that while the button is being held, the sound will neither turn on or off, and while the button is not being pressed nothing will happen either.

The code:

bool buttonState = LOW;
bool lastButtonState = LOW;
bool buttonState2 = LOW;
bool lastButtonState2 = LOW;
bool buttonState3 = LOW;
bool lastButtonState3 = LOW;
bool buttonState4 = LOW;
bool lastButtonState4 = LOW;

int buttonPin = 37;
int buttonPin2 = 36;
int buttonPin3 = 35;
int buttonPin4 = 34;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

}

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

void checkButton1(){
  lastButtonState = buttonState;
  buttonState = digitalRead(buttonPin);
  if(lastButtonState == LOW and buttonState == HIGH){
    usbMIDI.sendNoteOn(64, 127, 1);
    delay(5);
    Serial.println("on");
  } else if(lastButtonState == HIGH and buttonState == LOW){
    usbMIDI.sendNoteOff(64, 0, 1);
    delay(5);
    Serial.println("off");
  }
}

void checkButton2(){
  lastButtonState2 = buttonState2;
  buttonState2 = digitalRead(buttonPin2);
  if(lastButtonState2 == LOW and buttonState2 == HIGH){
    usbMIDI.sendNoteOn(67, 127, 1);
    delay(5);
    Serial.println("on");
  } else if(lastButtonState2 == HIGH and buttonState2 == LOW){
    usbMIDI.sendNoteOff(67, 0, 1);
    delay(5);
    Serial.println("off");
  }
}
void checkButton3(){
  lastButtonState3 = buttonState3;
  buttonState3 = digitalRead(buttonPin3);
  if(lastButtonState3 == LOW and buttonState3 == HIGH){
    usbMIDI.sendNoteOn(71, 127, 1);
    delay(5);
    Serial.println("on");
  } else if(lastButtonState3 == HIGH and buttonState3 == LOW){
    usbMIDI.sendNoteOff(71, 0, 1);
    delay(5);
    Serial.println("off");
  }
}
void checkButton4(){
  lastButtonState4 = buttonState4;
  buttonState4 = digitalRead(buttonPin4);
  if(lastButtonState4 == LOW and buttonState4 == HIGH){
    usbMIDI.sendNoteOn(74, 127, 1);
    delay(5);
    Serial.println("on");
  } else if(lastButtonState4 == HIGH and buttonState4 == LOW){
    usbMIDI.sendNoteOff(74, 0, 1);
    delay(5);
    Serial.println("off");
  }
}

int potVal = 0;
int lastPotVal = 0;
int mappedPotVal = 0;
int lastMappedPotVal = 0;

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

void loop() {
  lastMappedPotVal = mappedPotVal;
  potVal = analogRead(A14);
  mappedPotVal = map(potVal, 0, 1023, 10, 20);
  if(mappedPotVal != lastMappedPotVal){
    Serial.println(mappedPotVal);
  }
 
}

Leave a comment

Design a site like this with WordPress.com
Get started