Digital Electronics Final Project (!!!)

Explanation

Overview:

My final project is an 8-Step, 4-Channel Drum Sequencer that incorporates NeoPixels, and a potentiometer that sends CC messages to a DAW. In the video below, I chose to use Ableton to demo my project. There are three major components to this code which I will break down in the sections below.

Drum Sequencer

My first step in working on this project involved wiring and writing code for the actual drum sequencer itself. While the wiring was fairly simple and repetitious, the length of the sequencer required careful organization and conservative use of the real estate on the breadboard. In terms of writing the code, I primarily manipulated and expanded on the multi-channel drum sequencer code from Lab 9. I added variables and altered the code to account for four more steps, four more LED’s and four channels total to create an 8-step, 4-channel sequencer. I also included the same potentiometer that alters the tempo of the sequencer.

NeoPixels

For the next part of the code, I wanted to include a way to see the channel the user has selected using NeoPixels. Instead of different colored LED’s, I programmed two different NeoPixels to different colors that correspond to each of the four channels. As I pressed the button that changes the channel, I programmed both of the NeoPixels to change to a different color. This way, it is easy to tell which channel the drum sequencer is editing, and the colors are really pretty too. The first channel is white, the second channel is green, the third channel is red, and the fourth channel is blue.

Control Value (CC)

My last step in this project was to add a potentiometer that would manipulate one of the CC values in the DAW (in this case, Ableton). The particular parameter itself is variable based on the MIDI mapping in the DAW. In the demo below, I chose to alter the delay value with my potentiometer. I employed the code we wrote in the third lecture assignment, so that the potentiometer only sends messages when the knob is adjusted. One improvement I may consider making to the code in the future is adding a button that will activate the CC value’s ability to be changed by the DAW, as some minor noise occasionally occurs as a result of the analog nature of the potentiometer.

Video Demo:

Code:

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(2, 35, NEO_RGB);


int buttonPin[8] = {32, 31, 30, 29, 28, 27, 26, 25};
int ledPin[8] = {22, 21, 20, 19, 18, 17, 16, 15};
int totalLedPins = 8;
int totalButtonPins = 8;

int tempoPot = A15;
int tempoValue = 0;

unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 8;

int channelDisplayed = 0;
int totalChannels = 4;
int channelButtonPin = 33;
bool lastChannelButtonState = LOW;
bool channelButtonState = LOW;

bool lastButtonState[8] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
bool buttonState[8] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};

bool switchedOn[4][8] = {
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW },
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW },
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW },
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW },

};

int midiNotes[4] = {36, 40, 42, 46};
int totalMidiNotes = 4;

int potCC = A17;
int ccVal = 0;
int lastCCVal = 0;

void setup() {
  for (int i = 0; i < totalLedPins; i ++) {
    pinMode(ledPin[i], OUTPUT);
  }
  for (int i = 0; i < totalButtonPins; i ++) {
    pinMode(buttonPin[i], INPUT);
  }
  pinMode(channelButtonPin, INPUT);

  neopixel.begin();
  neopixel.clear();
  neopixel.show();
}

void loop() {

  checkButtons();
  updateLeds();
  stepForwards();


  checkChannelButton();
  updateChannelLeds();
  sendCCVal();

}

void checkButtons() {
  for (int i = 0; i < totalButtonPins; i++) {
    lastButtonState[i] = buttonState[i];
    buttonState[i] = digitalRead(buttonPin[i]);
    if (lastButtonState[i] == LOW && buttonState[i] == HIGH) {
      switchedOn[channelDisplayed][i] = !switchedOn[channelDisplayed][i];
      delay(5);
    } else if (lastButtonState[i] == HIGH && buttonState[i] == LOW) {
      delay(5);
    }
  }
}

void updateLeds() {
  for (int i = 0; i < totalLedPins; i++) {
    if (switchedOn[channelDisplayed][i] == true) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
  }
  digitalWrite(ledPin[currentStep], HIGH);
}

void stepForwards() {
  tempoValue = analogRead(A15);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    for (int i = 0; i < totalMidiNotes; i++) {
      usbMIDI.sendNoteOff(midiNotes[i], 127, 1);
    }
    countUp();
    for (int i = 0; i < totalMidiNotes; i++)
      if (switchedOn[i][currentStep] == HIGH) {
        usbMIDI.sendNoteOn(midiNotes[i], 127, 1);
      }
  }
}

void checkChannelButton() {

  lastChannelButtonState = channelButtonState;
  channelButtonState = digitalRead(channelButtonPin);

  if (lastChannelButtonState == LOW and channelButtonState == HIGH) {
    countUpChannel();
    delay(5);
  }
  if (lastChannelButtonState == HIGH and channelButtonState == LOW) {
    delay(5);
  }
}

void updateChannelLeds() {                       //NeoPixel Code

  if (channelDisplayed == 0) {
    neopixel.setPixelColor(0, 225, 225, 225);
    neopixel.setPixelColor(1, 225, 225, 225);
    neopixel.show();
  }
  if (channelDisplayed == 1) {
    neopixel.setPixelColor(0, 0, 225, 0);
    neopixel.setPixelColor(1, 0, 225, 0);
    neopixel.show();
  }
  if (channelDisplayed == 2) {
    neopixel.setPixelColor(0, 225, 0, 0);
    neopixel.setPixelColor(1, 225, 0, 0);
    neopixel.show();
  }
  if (channelDisplayed == 3) {
    neopixel.setPixelColor(0, 0, 125, 125);
    neopixel.setPixelColor(1, 0, 125, 125);
    neopixel.show();
  }

  neopixel.show();
}



void countUpChannel() {
  channelDisplayed++;
  if (channelDisplayed == totalChannels) {
    channelDisplayed = 0;
  }
}

void countUp() {
  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;
  }
}

void sendCCVal() {                            //CC Value Code

  usbMIDI.read();
  lastCCVal = ccVal;
  ccVal = map(analogRead(potCC), 0, 1023, 0, 127);

  if (ccVal != lastCCVal) {
    Serial.println(ccVal);
    usbMIDI.sendControlChange(1, ccVal, 1);
  }
}


Digital Electronics Lab #11

Arduino Code:

#include "Button.h"

Button buttonOne(33, 60);
Button buttonTwo(34, 62);
Button buttonThree(35, 64);
Button buttonFour(36, 65);


void setup() {
  Serial.begin(9600);
  buttonOne.pressHandler(onPress);
  buttonOne.releaseHandler(onRelease);
  
  buttonTwo.pressHandler(onPress);
  buttonTwo.releaseHandler(onRelease);
  
  buttonThree.pressHandler(onPress);
  buttonThree.releaseHandler(onRelease);
  
  buttonFour.pressHandler(onPress);
  buttonFour.releaseHandler(onRelease);
}

void loop() {
  buttonOne.process();
  buttonTwo.process();
  buttonThree.process();
  buttonFour.process();
  
}

void onPress(int buttonNumber) {
  Serial.print(buttonNumber);
  usbMIDI.sendNoteOn(buttonNumber,127,1);
  Serial.println(" pressed");
}

void onRelease(int buttonNumber) {
  Serial.print(buttonNumber);
  usbMIDI.sendNoteOff(buttonNumber,127,1);
  Serial.println(" released");

}

Explain:

Main Program

This program seems to establish each of the buttons individually, and allows them to undergo a particular process, depending on the function “button.process();” which is defined in another part of the program. In this case, it will just print the status of the button in the serial monitor and from what we added, send a particular MIDI note. What is most new to me in this program is the code in the setup section. It seems that another part of the code has established pressed and released states of the buttons (“on press” and “on release” statuses), so that the button will behave in certain ways in these states.

Button.cpp

This programs looks very similar to the button code we used earlier in the semester, that basically codes the button to behave in the same way as a musical keyboard button or key, where it will do something when pressed, and stop only when released. The button of the code, with the “pressHandler” and “releaseHandler” seems to establish the button statuses that can be seen in the main program, but I’m not too sure what they do, and how it’s that different from including just the regular button code in your code.

Button.h

I have no idea what this last part of the code is doing and how it is behaving within this entire code. It seems to be creating the key variables and states for the other code to follow. I’m not sure what public and private means in this context; it seems that all of the public variables can be seen in the main button code, while the private variables can only be seen in the Button.cpp code. Overall, I think this part of the code just creates the variables, and the rest of the code uses them in creative ways to simplify the buttonCode so that is easy to add more buttons, change pin numbers, and add additional functions.

Digital Electronics Lecture #9

Video:

Arduino Code:

int ledPin[4] = {7, 8, 9, 10};
int totalLedPins = 4;

int tempoPot = A13;
int tempoValue = 0;

bool switchedOn[4] = {false, false, false, false};

unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 4;

void setup() {
  for (int i = 0; i < totalLedPins; i ++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
    updateLeds();
    stepForwards();
    
  }



void updateLeds() {                                       //update the LEDs based on the status of the button
  for (int i = 0; i < totalLedPins; i++) {
    if (switchedOn[i] == true) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
  }
  digitalWrite(ledPin[currentStep], HIGH);             //ensure that the LED of the current step is always HIGH (results in consistent blinking as the steps are raised or lowered)
}

void stepForwards() {                                 //step forwards, and send a MIDI note for each engaged step
  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    countUp();
    Serial.write(currentStep);
    if (switchedOn[currentStep] == HIGH) {

    }
  }
}

void countUp() {                                    //countUp function (for stepForwards)
  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;
  }
}
  
                                                

Processing Code:

import processing.serial.*;

Serial teensySerial;

int val = 0;
int r = 0;
int g= 0;
int b= 0;



void setup() {
  frameRate(30);
  size(500, 500);

  //printArray(Serial.list());
  String usbPortName = Serial.list()[5];
  print(usbPortName);
  teensySerial= new Serial(this, usbPortName, 9600);
}

void draw () {
  if (teensySerial.available() > 0) {
    val = teensySerial.read();
    println(val);
  }

  if (val == 0) {

    r= 200;
    g= 200;
    b= 0;
    background(r, g, b);
  }

  if (val == 1) {
    r= 0;
    g= 0;
    b= 200;
    background(r, g, b);
  }

  if (val == 2) {
    r= 0;
    g= 200;
    b= 0;
    background(r, g, b);
  }

  if (val == 3) {
    r= 200;
    g= 0;
    b= 100;
    background(r, g, b);
  }
}

Digital Electronics Lab #10

Video:

Arduino Code:

int potPin = A14;
int potVal = 0;
int mappedPotVal = 0;

int potPin2 = A15;
int potVal2 = 0;
int mappedPotVal2 = 0;

int potPin3 = A13;
int potVal3 = 0;
int mappedPotVal3 = 0;

int potPin4 = A12;
int potVal4 = 0;
int mappedPotVal4 = 0;

int potPin5 = A16;
int potVal5 = 0;
int mappedPotVal5 = 0;

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

}

void loop() {

  Serial.write(0);


  potVal = analogRead(potPin);
  mappedPotVal = map(potVal, 0, 1023, 1, 255);
  Serial.write(mappedPotVal);


  potVal2 = analogRead(potPin2);
  mappedPotVal2 = map(potVal2, 0, 1023, 1, 255);
  Serial.write(mappedPotVal2);


  potVal3 = analogRead(potPin3);
  mappedPotVal3 = map(potVal3, 0, 1023, 1, 255);
  Serial.write(mappedPotVal3);


  potVal4 = analogRead(potPin4);
  mappedPotVal4 = map(potVal4, 0, 1023, 1, 255);
  Serial.write(mappedPotVal4);

  potVal5 = analogRead(potPin5);
  mappedPotVal5 = map(potVal5, 0, 1023, 1, 255);
  Serial.write(mappedPotVal5);

  delay(50);

}

Processing Code:

import processing.serial.*;

Serial teensySerial;

int inputVal = 0;
int inputVal2 = 0;
int inputVal3 = 0;
int inputVal4 = 0;
int inputVal5 = 0;
int inputVal6 = 0;

int circleSize = 0;
int circleY = 0;
int circleX = 0;
int circleSpeedX = 0;
int circleSpeedY = 0;

void setup() {
  frameRate(30);
  size(500, 500);

  //printArray(Serial.list());
  String usbPortName = Serial.list()[5];
  print(usbPortName);
  teensySerial= new Serial(this, usbPortName, 9600);
}

void draw() {
  if (teensySerial.available() >= 6) {
    inputVal= teensySerial.read();
    if (inputVal == 0) {
      inputVal2 = teensySerial.read();
      inputVal3 = teensySerial.read();
      inputVal4 = teensySerial.read();
      inputVal5 = teensySerial.read();
      inputVal6 = teensySerial.read ();
    }
  }

  background(inputVal6, 200, 200);

  fill(0, 100, inputVal5);

  circleSize = (int)map(inputVal2, 1, 255, 0, 500);
  
  circleX = (int)map(inputVal4, 1, 255, 0, 500);

  circleSpeedY = (int)map(inputVal3, 1, 255, -50, 50);


  circleY += circleSpeedY;
  if (circleY > 500 + circleSize/2) {
    circleY = -circleSize/2;
  } else if (circleY < -circleSize/2) {
    circleY = 500 + circleSize/2;
  }

  circle(circleX, circleY, circleSize);
}

Digital Electronics Lab #9

Video:

Code:

int buttonPin[4] = {33, 34, 35, 36};
int ledPin[4] = {7, 8, 9, 10};
int totalLedPins = 4;
int totalButtonPins = 4;

int tempoPot = A13;
int tempoValue = 0;

unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 4;

int channelDisplayed = 0;
int totalChannels = 3;
int channelLedPins[3] = {28, 29, 30};
int channelButtonPin = 37;
bool lastChannelButtonState = LOW;
bool channelButtonState = LOW;


bool lastButtonState[4] = {LOW, LOW, LOW, LOW};
bool buttonState[4] = {LOW, LOW, LOW, LOW};

bool switchedOn[3][4] = {
  { LOW, LOW, LOW, LOW },
  { LOW, LOW, LOW, LOW },
  { LOW, LOW, LOW, LOW }
};

int midiNotes[3] = { 40, 42, 44 };
int totalMidiNotes = 3;



void setup() {
  for (int i = 0; i < totalLedPins; i ++) {
    pinMode(ledPin[i], OUTPUT);
  }
  for (int i = 0; i < totalButtonPins; i ++) {
    pinMode(buttonPin[i], INPUT);
  }

  for (int i = 0; i < 3; i++) {
    pinMode(channelLedPins[i], OUTPUT);
  }
  pinMode(channelButtonPin, INPUT);
}
void loop() {


  checkButtons();
  updateLeds();
  stepForwards();

  checkChannelButton();
  updateChannelLeds();

}


void checkButtons() {                                     
  for (int i = 0; i < totalButtonPins; i++) {
    lastButtonState[i] = buttonState[i];
    buttonState[i] = digitalRead(buttonPin[i]);
    if (lastButtonState[i] == LOW && buttonState[i] == HIGH) {
      switchedOn[channelDisplayed][i] = !switchedOn[channelDisplayed][i];                         
      delay(5);
    } else if (lastButtonState[i] == HIGH && buttonState[i] == LOW) {
      delay(5);
    }
  }
}


void updateLeds() {                                     
  for (int i = 0; i < totalLedPins; i++) {
    if (switchedOn[channelDisplayed][i] == true) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
  }
  digitalWrite(ledPin[currentStep], HIGH);            
}

void stepForwards() {                                 
  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    for (int i = 0; i < totalMidiNotes; i++) {
      usbMIDI.sendNoteOff(midiNotes[i], 127, 1);
    }
    countUp();
    for (int i = 0; i < totalMidiNotes; i++)
      if (switchedOn[i][currentStep] == HIGH) {
        usbMIDI.sendNoteOn(midiNotes[i], 127, 1);
      }
  }
}

void checkChannelButton() {

  lastChannelButtonState = channelButtonState;
  channelButtonState = digitalRead(channelButtonPin);

  if (lastChannelButtonState == LOW and channelButtonState == HIGH) {
    countUpChannel();
    delay(5);
  }
  if (lastChannelButtonState == HIGH and channelButtonState == LOW) {
    delay(5);
  }
}


void updateChannelLeds() {

  for (int i = 0; i < totalChannels; i++) {
    if (i == channelDisplayed) {
      digitalWrite(channelLedPins[i], HIGH);
    } else {
      digitalWrite(channelLedPins[i], LOW);
    }
  }
}

void countUpChannel() {
  channelDisplayed++;
  if (channelDisplayed == totalChannels) {
    channelDisplayed = 0;
  }
}


void countUp() {                                   
  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;
  }
}

Digital Electronics Lab #8

Video:

Code:

int buttonPin[4] = {33, 34, 35, 36};
int ledPin[4] = {7, 8, 9, 10};
int totalLedPins = 4;
int totalButtonPins = 4;

int tempoPot = A13;
int tempoValue = 0;

int slideSwitch = 31;

unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 4;


bool lastButtonState[4] = {LOW, LOW, LOW, LOW};
bool buttonState[4] = {LOW, LOW, LOW, LOW};

bool switchedOn[4] = {false, false, false, false};

void setup() {
  for (int i = 0; i < totalLedPins; i ++) {
    pinMode(ledPin[i], OUTPUT);
  }
  for (int i = 0; i < totalButtonPins; i ++) {
    pinMode(buttonPin[i], INPUT);
  }

  pinMode(slideSwitch, INPUT);

}

void loop() {

  if (digitalRead(slideSwitch) == LOW) {                        //stepForwards
    checkButtons();
    updateLeds();
    stepForwards();
  }

  if (digitalRead(slideSwitch) == HIGH) {                     //stepBackwards
    checkButtons();
    updateLeds();
    stepBackwards();
  }
}

void checkButtons() {                                        //check the status of the button
  for (int i = 0; i < totalButtonPins; i++) {
    lastButtonState[i] = buttonState[i];
    buttonState[i] = digitalRead(buttonPin[i]);
    if (lastButtonState[i] == LOW && buttonState[i] == HIGH) {
      switchedOn[i] = !switchedOn[i];                         // this means: if switchedOn is true then set it to false, if switchedOn is false then set it to true
      delay(5);
    } else if (lastButtonState[i] == HIGH && buttonState[i] == LOW) {
      delay(5);
    }
  }
}


void updateLeds() {                                       //update the LEDs based on the status of the button
  for (int i = 0; i < totalLedPins; i++) {
    if (switchedOn[i] == true) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
  }
  digitalWrite(ledPin[currentStep], HIGH);             //ensure that the LED of the current step is always HIGH (results in consistent blinking as the steps are raised or lowered)
}

void stepForwards() {                                 //step forwards, and send a MIDI note for each engaged step
  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    usbMIDI.sendNoteOff(44, 127, 1);
    countUp();
    if (switchedOn[currentStep] == HIGH) {
      usbMIDI.sendNoteOn(44, 127, 1);
    }
  }
}

void stepBackwards() {                              //step backwards and send a MIDI note for each engaged step
  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    usbMIDI.sendNoteOff(44, 127, 1);
    countDown();
    if (switchedOn[currentStep] == HIGH) {
      usbMIDI.sendNoteOn(44, 127, 1);
    }
  }
}





void countUp() {                                    //countUp function (for stepForwards)
  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;
  }
}
  
                                                  //countDown function (for stepBackwards)
void countDown() {
  currentStep--;
  if (currentStep < 0) {
    currentStep = 3;
  }
}

Digital Electronics Lab #7

Push-On Push-Off Button Explanation:

The push-on push-off button concept has three major parts: checkButton(); flipButtonState();, and updateLed();.

The checkButton(); function is very similar to the code we’ve used in the past for MIDI notes. First, we set lastButton1State equal to button1State, and button1State equal to the status of the buttonPin (pushed or un-pushed). From there, we establish that if button1State is HIGH, meaning that the button is currently pressed (button1State=HIGH), but wasn’t pressed a moment ago, so lastButton1State is LOW, we flipButtonState();. A delay of 5 milliseconds is included as a slight buffer to avoid any electronics noise. Using the second half of the conditional “else if,” we establish that the code will just continue if the button was previously on a moment ago, but is not being pushed at the moment (lastButtonState=HIGH, buttonState=LOW);. So, in this case it will start over, and if the button isn’t pressed again, it will set button1State to LOW, and therefore not flipButtonState according to the conditional. It will only flipButtonState if we push the button.

To better understand the purpose of the checkButton function though, it is important to understand what flipButtonState is doing and what updateLed is doing. flipButtonState perpetually flips switchedOn between true and false. switchedOn is set equal to false from our variables, so according to the conditional, it will be set to true. However, there is another part of the conditional “if(switchedOn == true) { switchedOn = false;,” which will set switchedOn to false again, and the cycle would theoretically continue forever if placed alone in the loop. Here, when we push the button, triggering the flipButtonState function, we make switchedOn true according to the conditionals. When we push the button again, however, we set switchedOn to false again. This influences what happens in our updateLed(); function, allowing the LED to be turned on and off, respectively.

When switchedOn is true, the function updateLed(); will turn the LED on, whereas if it is false, it will turn the LED off. When the button is pressed, because of the nature of the checkButton(); and flipButtonState(); function, switchedOn will become true and we will turn the LED on. On the contrary, when we hit the button again, switchedOn is changed back to false, and subsequently shuts off the LED.

Video:

Code:

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(2, 32, NEO_RGB);

int redValue = 0;
int redPot = 0;
int greenValue = 0;
int greenPot = 0;
int blueValue = 0;
int bluePot = 0;
int buttonPin = 31;

bool lastButton1State = LOW;
bool button1State = LOW;

bool switchedOn = false;


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

  neopixel.begin();
  neopixel.clear();
  neopixel.show();
  pinMode(buttonPin, INPUT);
}

void loop() {
  checkButton();
  updateLed();

}

void colorChange() {
  
  redPot = analogRead(A14);
  redValue = map(redPot,0,1023,0,225);

  greenPot = analogRead(A15);
  greenValue = map(greenPot,0,1023,0,225);

  bluePot = analogRead(A16);
  blueValue = map(bluePot,0,1023,0,225);

  neopixel.setPixelColor(0, redValue, greenValue, blueValue);
  neopixel.setPixelColor(1, redValue, greenValue, blueValue);
  
  
  neopixel.show();
}

void colorOff() {

  neopixel.setPixelColor(0, 0, 0, 0);
  neopixel.setPixelColor(1, 0, 0, 0);
  
  neopixel.show();
}

void checkButton() {
  lastButton1State = button1State;
  button1State = digitalRead(buttonPin);
  if (lastButton1State == LOW && button1State == HIGH) {
    flipButtonState();
    delay(5);
  } else if (lastButton1State == HIGH && button1State == LOW) {
    delay(5);
  }
}

void flipButtonState() {
  if (switchedOn == true) {
    switchedOn = false;
  } else if (switchedOn == false) {
    switchedOn = true;
  }
}

void updateLed() {
  if (switchedOn == true) {
    colorChange();
  } else {
    colorOff();
  }
}


Digital Electronics Lab #6

Code:

int octaveSwitch = 30;                                          //switch variables
int powerSwitch = 29;
int directionSwitch = 28;

int tempoVal = 0;                                               //tempo variables
int lastTempoVal = 0;
int tempoPin = A13;

int octave = 12;                                                //octave switch variable


int ledPinArray[4] = {7, 8, 9, 10};                             //ledPin array
int totalLedPins = 4;

int notePotArray[4] = {A14, A15, A16, A17};                     //notePot array
int totalNotePots = 4;

int MIDINotes[4] = {0, 0, 0, 0};                                //MIDINotes array

void setup() {

  pinMode(octaveSwitch, INPUT);                                 //pinMode for switches
  pinMode(powerSwitch, INPUT);
  pinMode(directionSwitch, INPUT);

  for (int i = 0; i < totalLedPins; i ++) {                    //pinMode for LED loop
    pinMode(ledPinArray[i], OUTPUT);
  }

}
void loop() {

  if (digitalRead(powerSwitch) == HIGH) {                      //power swtich

    if (digitalRead(directionSwitch) == HIGH) {                //backward function

      sequenceBackward();
      checkOctave();
    }

    if (digitalRead(directionSwitch) == LOW) {                //forward function
      sequenceForward();
      checkOctave();
    }

  }
}




void sequenceForward() {

  tempoVal = map(analogRead(tempoPin), 0, 1023, 100, 1000);                 //tempoVal

  for (int i = 0; i < totalNotePots; i ++) {                                //forward loop
    MIDINotes[i] = map(analogRead(notePotArray[i]), 0, 1023, 60, 72);
    usbMIDI.sendNoteOn(MIDINotes[i] - octave, 127, 1);
    digitalWrite(ledPinArray[i], HIGH);
    delay(tempoVal);
    usbMIDI.sendNoteOff(MIDINotes[i] - octave, 0, 1);
    digitalWrite(ledPinArray[i], LOW);
    delay(tempoVal);
  }
}

void sequenceBackward() {

  tempoVal = map(analogRead(tempoPin), 0, 1023, 100, 1000);               //tempoVal

  for (int i = 3; i >= 0; i--) {                                          //backward loop
    MIDINotes[i] = map(analogRead(notePotArray[i]), 0, 1023, 60, 72);
    usbMIDI.sendNoteOn(MIDINotes[i] - octave, 127, 1);
    digitalWrite(ledPinArray[i], HIGH);
    delay(tempoVal);
    usbMIDI.sendNoteOff(MIDINotes[i] - octave, 0, 1);
    digitalWrite(ledPinArray[i], LOW);
    delay(tempoVal);
  }
}

void checkOctave() {                                  //checkOctave function
  if (digitalRead(octaveSwitch) == LOW) {             //if switch is off, octave is 0, won't add anything to the pitches
    octave = 0;
  }
  if (digitalRead(octaveSwitch) == HIGH) {            //if switch is on, octave is 12, will subtract an octave from the pitches
    octave = 12;
  }
}

Video:

Digital Electronics Lecture Assignment #5

Code:

int ledPinArray[4] = {7, 8, 9, 10};
int totalLedPins = 4;
int tempoValue = 0;
int slideSwitch = 30;
unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 4;

void setup() {

  pinMode(slideSwitch, INPUT);                                 

  for (int i = 0; i < totalLedPins; i ++) {                   
    pinMode(ledPinArray[i], OUTPUT);
  }


}

void loop() {
  if (digitalRead(slideSwitch) == LOW) {
    stepForwards();
  }

  if (digitalRead(slideSwitch) == HIGH) {
    stepBackwards();
  }
}


void stepForwards() {

  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    digitalWrite(ledPinArray[currentStep], LOW);
    countUp();
    digitalWrite(ledPinArray[currentStep], HIGH);

  }
}

void stepBackwards() {

  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    digitalWrite(ledPinArray[currentStep], LOW);
    countDown();
    digitalWrite(ledPinArray[currentStep], HIGH);


  }
}


void countUp() {

  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;

  }
}

void countDown() {

  currentStep--;
  if (currentStep < 0) {
    currentStep = 3;
  }

}


Video:

Digital Electronics Lab #5

Video:

Code:

int octaveSwitch = 30;                                          //switch variables
int powerSwitch = 29;
int directionSwitch = 28;

int tempoVal = 0;                                               //tempo variables
int lastTempoVal = 0;
int tempoPin = A13;

int octave = 12;                                                //octave switch variable


int ledPinArray[4] = {7, 8, 9, 10};                             //ledPin array
int totalLedPins = 4;

int notePotArray[4] = {A14, A15, A16, A17};                     //notePot array
int totalNotePots = 4;

int MIDINotes[4] = {0, 0, 0, 0};                                //MIDINotes array

void setup() {

  pinMode(octaveSwitch, INPUT);                                 //pinMode for switches
  pinMode(powerSwitch, INPUT);
  pinMode(directionSwitch, INPUT);

  for (int i = 0; i < totalLedPins; i ++) {                    //pinMode for LED loop
    pinMode(ledPinArray[i], OUTPUT);
  }

}
void loop() {

  if (digitalRead(powerSwitch) == HIGH) {                      //power swtich

    if (digitalRead(directionSwitch) == HIGH) {                //backward function

      sequenceBackward();
      checkOctave();
    }

    if (digitalRead(directionSwitch) == LOW) {                //forward function
      sequenceForward();
      checkOctave();
    }

  }
}




void sequenceForward() {

  tempoVal = map(analogRead(tempoPin), 0, 1023, 100, 1000);                 //tempoVal

  for (int i = 0; i < totalNotePots; i ++) {                                //forward loop
    MIDINotes[i] = map(analogRead(notePotArray[i]), 0, 1023, 60, 72);
    usbMIDI.sendNoteOn(MIDINotes[i] - octave, 127, 1);
    digitalWrite(ledPinArray[i], HIGH);
    delay(tempoVal);
    usbMIDI.sendNoteOff(MIDINotes[i] - octave, 0, 1);
    digitalWrite(ledPinArray[i], LOW);
    delay(tempoVal);
  }
}

void sequenceBackward() {

  tempoVal = map(analogRead(tempoPin), 0, 1023, 100, 1000);               //tempoVal

  for (int i = 3; i >= 0; i--) {                                          //backward loop
    MIDINotes[i] = map(analogRead(notePotArray[i]), 0, 1023, 60, 72);
    usbMIDI.sendNoteOn(MIDINotes[i] - octave, 127, 1);
    digitalWrite(ledPinArray[i], HIGH);
    delay(tempoVal);
    usbMIDI.sendNoteOff(MIDINotes[i] - octave, 0, 1);
    digitalWrite(ledPinArray[i], LOW);
    delay(tempoVal);
  }
}

void checkOctave() {                                  //checkOctave function
  if (digitalRead(octaveSwitch) == LOW) {             //if switch is off, octave is 0, won't add anything to the pitches
    octave = 0;
  }
  if (digitalRead(octaveSwitch) == HIGH) {            // if switch is on, octave is 12, will subtract an octave from the pitches
    octave = 12;
  }
}

Design a site like this with WordPress.com
Get started