Digital Lab Final Project

The Cairns Drum Sequencer V1 

What is it?

This device is an 8 step drum machine that uses the LEDs to show which channel is selected, when midi is being read, and when the sequencer is stepping. This drum machine also includes a potentiometer that allows the user to adjust the velocity on each channel. 

Video Demonstration:

How it Works…

Ableton Setup:

The Ableton setup isnt too difficult, but there are a few key things to get this working…

  1. Open up a project and upload the program to the Teensy. 
  2. Open a drum kit/MIDI track that you would like to sequence, and make sure the input is “Teensy MIDI”. 
  1. Arm the drum track and make sure the sounds that are part of drum kit are on these 5 notes: C2, G#1, E1, A#1, F#1.

The Sounds:

The sounds used in the demo are from an Ableton Live 10 standard pack which can be purchased on the Ableton website, but the best part about this drum sequencer is that you don’t have to use stock drum kits! Since the user can pick their own sounds and build their own drum kit that they can control with the sequencer.

Changing Channels/Scroll:

To change which channel is active, the user presses the scroll button on the bottom right of the drum sequencer. This feature was relatively easy to implement since we had done a lab previously that required us to make a 4 channel step sequencer. The code for this feature was unchanged from the previous 4 channel sequencer lab. 

The Sequencer:

The sequencer is relatively easy to use, the LEDs will count up the steps and if you press a step button the corresponding LED will become lit and it will stay lit until you press the button again. 

Changing Velocity: 

To change the velocity of each channel, the user can turn the potentiometer located in the upper right portion of the sequencer. To accomplish this feature, I made it so that the velocity was standardized to 90 for each channel at the beginning of the program. The velocity of each channel would then be controlled by the potentiometer using the range 1-127 (the minimum and maximum velocity) through the analogRead function. 

Future Features:

While making this project I had debated whether or not completing an Ableton synced tempo feature, where the drum sequencer would only sequence forward whenever it received a MIDI note from MIDI channel 1 in Ableton. This would allow the user to syncnronize the project tempo and the sequencer tempo,  in any daw. However as I began experimenting with the feature it proved harder to implement than anticipated so I have decided to save this feature for future versions. 

Here is some of the code from the in development sequencer:

If you hadn’t noticed, there is a small red LED on the right side of the drum sequencer which wasn’t blinking. In the future version that will include project synced tempo, this LED blinks whenever MIDI is being read by the daw, giving the user some insight into when the system is reading MIDI data. 

My Arduino Code:

int ledPin[8] = {2, 3, 4, 5, 7, 9, 11, 24};
int numLeds = 8;
int channelLeds[5] = {22, 20, 18, 16, 14};
int numChannelLeds = 5;
int buttonPin[9] = {37, 39, 38, 32, 14, 33, 34, 35, 12};
int tempo = 0; 
int mappedPotValTempo = 0;
int currentStep = 0;
int channelVelocity[5] = {90, 90, 90, 90, 90};
bool buttonState[9] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
bool lastButtonState[9] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
bool switchedOn[5][8] = { //this formatting was a challenge.
  {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},
  {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
};
int activeChannel = 0;
int noteVals[5] = {40, 42, 44, 46, 48};
unsigned long lastStepTime = 0;

void setup() {
  for (int i = 0; i < 9; i++) {      //Sets I/O for all LEDs and button pins
    pinMode(buttonPin[i], INPUT);
  }
  for (int i = 0; i < numLeds; i++) {      
    pinMode(ledPin[i], OUTPUT);
  }
  for (int i = 0; i < numChannelLeds; i++) {
    pinMode(channelLeds[i], OUTPUT);
  }
}

void loop() {
  velocityPot();
  changeChannels();
  checkButtons();
  updateLeds();
  stepForwards();
}

void changeChannels() { 
  digitalWrite(channelLeds[activeChannel], LOW);

  lastButtonState[8] = buttonState[8];
  buttonState[8] = digitalRead(buttonPin[8]);
  
  if(lastButtonState[8] == LOW && buttonState[8] == HIGH) {
      activeChannel++; 
      if (activeChannel == 5) {
        activeChannel = 0;
      }
      delay(5);
  }
  digitalWrite(channelLeds[activeChannel], HIGH);
}

void checkButtons() { //checks buttons and keeps them in sequence.
  for (int i = 0; i < 8; i++) { 
    lastButtonState[i] = buttonState[i];
    buttonState[i] = digitalRead(buttonPin[i]);
    if(lastButtonState[i] == LOW && buttonState[i] == HIGH) {
      switchedOn[activeChannel][i] = !switchedOn[activeChannel][i]; 
      delay(5);
    } else if(lastButtonState[i] == HIGH && buttonState[i] == LOW) {
      delay(5);
    }
  }
}

void updateLeds() {
  for (int i = 0; i < 8; i++) {   
    if(switchedOn[activeChannel][i] == HIGH or i == currentStep) { //so that it blinks if the button isn't pushed and stays lit if it is
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
}}

void stepForwards() {  
  tempo = 500;
  if (millis() >= lastStepTime + tempo) {           //tell Teensy when to start running the function with millis()
    
    lastStepTime = millis();
    for (int i = 0; i < 5; i++) {
      usbMIDI.sendNoteOff(noteVals[i], 0, 1);
    }
    currentStep++;                                  //increase counter
    
    if (currentStep == 8) {
      currentStep = 0;                              //reset counter
    }
    for (int i = 0; i < 5; i++) {
      if (switchedOn[i][currentStep] == true) {
          usbMIDI.sendNoteOn(noteVals[i], channelVelocity[i], 1);
       }
    }
  }
}

void velocityPot() {
  channelVelocity[activeChannel] = map(analogRead(A21), 0, 1023, 0, 127);
  // checks if a button is pressed
  // if so (analog read pot) map the analog value to 0-127
  // And set the channelVelocity[activeChannel] to that analog value
}

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);
  Serial.println(" pressed");
  usbMIDI.sendNoteOn(buttonNumber, 127, 1);
}

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


Digital Electronics Lab 10

Video:

Processing Code:

import processing.serial.*;

Serial teensySerial;

int inputVal=0;
int inputVal2=0;
int inputVal3 = 0;
int inputVal4=0;
int inputVal5=0;
int circleSize=0;
int circleX=0;
int circleSpeed =0;
int circleY=0;

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

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

void draw() {

  if (teensySerial.available()>=6) {
    inputVal = teensySerial.read();
    if (inputVal == 0) {
      inputVal=teensySerial.read();
      inputVal2=teensySerial.read();
      inputVal3=teensySerial.read();
      inputVal4=teensySerial.read();
      inputVal5=teensySerial.read();
    
    }
  }
  background(inputVal4, 0, inputVal5);
  fill(0, 0, inputVal);
  circleSize= (int)map(inputVal2, 1, 255, 0, 500);
  circleSpeed=(int)map(inputVal3, 1, 255, 1, 50);
  circleX+=circleSpeed;
  if (circleX>500) {
    circleX=0;
  }
  circle(circleX, 250, circleSize);
 
}

Arduino Code:

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

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

int potPin3= A16;
int potVal3 = 0;
int mappedPotVal3 = 0;

int potPin4=A17;
int potVal4 = 0;
int mappedPotVal4 = 0;

int potPin5= A13;
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);
}


Digital Lecture Assignment 9

Video:

Processing Code:

import processing.serial.*;

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

int shapeX=0;
int shapeY=0;
int shapeSize=0;

void setup()
{
  frameRate(30);
  size(600, 600);
  printArray(Serial.list());
  String portName= Serial.list()[12];
  teensySerial= new Serial(this, portName, 9600);

  while (teensySerial. available() > 0) {
    teensySerial.read();
  }
}
void draw() {
  if (teensySerial.available() > 0) {
    val = teensySerial.read();
    if (val==0) {
      r = 200;
      g=0;
      b=0;
    } else if (val ==1) {
      r=200;
      g=200;
      b=0;
    } else if (val==2) {
      r=0;
      g=200;
      b=0;
    } else if (val==3) {
      r=0;
      g=200;
      b=200;
    }
  }
  noStroke();
  fill(r, g, b);
  rect(0, 0, 600,600);
}

Arduino Code:

int ledPins[4] = {29, 30, 31, 32};
int potPin = A18;
int potVal = 0;
int ledSpeed = 0;

void setup() {
  Serial.begin(9600);
  
  for (int i = 0; i < 4; i++) {
    pinMode (ledPins[i], OUTPUT);
  }
}

void loop() {
  Serial.write(1);

  potVal = analogRead(potPin);
  ledSpeed = map(potVal, 0, 1023, 100, 700);
  
  for (int i = 0; i < 4; i++) {
    digitalWrite(ledPins[i], HIGH);
    Serial.write(i);
    delay(ledSpeed);
    digitalWrite(ledPins[i], LOW);
  }
}

Digital Electronics Lab 9 (Multi-Channel Step Sequencer)

Video:

Code:

int ledPin[4] = {29, 30, 31, 32};
int channelLeds[3] = {18, 17, 16};
int buttonPin[5] = {33, 34, 35, 36, 27};
int tempo = 0; 
int mappedPotValTempo = 0;
int currentStep = 0;
bool buttonState[5] = {LOW, LOW, LOW, LOW, LOW};
bool lastButtonState[5] = {LOW, LOW, LOW, LOW, LOW};
bool switchedOn[3][4] = { //this formatting was a challenge.
  {LOW, LOW, LOW, LOW},
  {LOW, LOW, LOW, LOW},
  {LOW, LOW, LOW, LOW},
};
int activeChannel = 0;
int noteVals[3] = {40, 42, 44};
unsigned long lastStepTime = 0;

void setup() {
  for (int i = 0; i < 5; i++) {      //Sets I/O for all LEDs and button pins
    pinMode(buttonPin[i], INPUT);
  }
  for (int i = 0; i < 4; i++) {      
    pinMode(ledPin[i], OUTPUT);
  }
  for (int i = 0; i < 3; i++) {
    pinMode(channelLeds[i], OUTPUT);
  }
}

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

void changeChannels() { 
  digitalWrite(channelLeds[activeChannel], LOW);

  lastButtonState[4] = buttonState[4];
  buttonState[4] = digitalRead(buttonPin[4]);
  
  if(lastButtonState[4] == LOW && buttonState[4] == HIGH) {
      activeChannel++; 
      if (activeChannel == 3) {
        activeChannel = 0;
      }
      delay(5);
  }
  digitalWrite(channelLeds[activeChannel], HIGH);
}

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

void updateLeds() {
  for (int i = 0; i < 4; i++) {   
    if(switchedOn[activeChannel][i] == HIGH or i == currentStep) { //so that it blinks if the button isn't pushed and stays lit if it is
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
}}

void stepForwards() {  
  tempo = analogRead(A19);
  if (millis() >= lastStepTime + tempo) {           //tell Teensy when to start running the function with millis()
    
    lastStepTime = millis();
    for (int i = 0; i < 3; i++) {
      usbMIDI.sendNoteOff(noteVals[i], 0, 1);
    }
    currentStep++;                                  //increase counter
    
    if (currentStep == 4) {
      currentStep = 0;                              //reset counter
    }
    for (int i = 0; i < 3; i++) {
      if (switchedOn[i][currentStep] == true) {
          usbMIDI.sendNoteOn(noteVals[i], 127, 1);
       }
    }
  }
}

Digital Electronics Lab 8 (Step Sequencer)

Code:

int ledPin[4] = {29, 30, 31, 32};
int buttonPin[4] = {33, 34, 35, 36};
bool switchedOn[4] = {false, false, false, false};
bool buttonState[4] = {LOW, LOW, LOW, LOW};
bool lastButtonState[4] = {LOW, LOW, LOW, LOW};
int tempo = 0; 
int mappedPotValTempo = 0;
int currentStep = 0;
unsigned long lastStepTime = 0;

void setup() {
  for (int i = 0; i < 4; i++) {      //use a for loop to set all LEDs and button pins
    pinMode(ledPin[i], OUTPUT);
    pinMode(buttonPin[i], INPUT);
  }
}

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

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

void flipbuttonstate(int i) { //for organization
  if(switchedOn[i]== true){
        switchedOn[i]= false;
      }
      else if(switchedOn[i]== false){
        switchedOn[i]= true;
      }
}

void updateLeds() {
  for (int i = 0; i < 4; i++) {   
    if(switchedOn[i] == true or i == currentStep) { //so that it blinks if the button isn't pushed and stays lit if it is
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
}}

void stepForwards() {  
  tempo = analogRead(A19);
  if (millis() >= lastStepTime + tempo) {           //tell Teensy when to start running the function with millis()
    
    lastStepTime = millis();
    usbMIDI.sendNoteOff(40, 0, 1);
    currentStep++;                                  //increase counter
    
    if (currentStep == 4) {
      currentStep = 0;                              //reset counter
    }
    
    if (switchedOn[currentStep] == true) {
      usbMIDI.sendNoteOn(40, 127, 1);
    }
  }
}

Video:

Digital Electronics Lab 7

“Push” ON/OFF Button:

The “Push” Button works in 4 steps: First we store the “button states” within 3 boolean values, which controls the button and the LEDs. The checkButton() is used to check to make sure the last button state equals the current button state. If this is true, it will then take the values stored in after the button is turned “on” and then proceed to call the flipButtonState() so that it can keep track of the state that is active. This is the value that is used to actually turn the LED on or off.

The Code:

#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(2, 32, NEO_RGB);

int red = 0;
int green = 0;
int blue = 0;
int mappedRed = 0;
int mappedGreen = 0;
int mappedBlue = 0;

int buttonPin = 36;
//this is for the on off push button
bool lastButton1State = LOW;
bool button1State = LOW;
bool switchedOn = false;

void setup() { //setup neo pixel
  neopixel.begin();
  neopixel.clear();
  neopixel.show();
  pinMode(buttonPin, INPUT);
}

void loop() {
  checkButton();
  updatefunction();
  
  red = analogRead(A16); //set as analog read
  green = analogRead(A15);
  blue = analogRead(A14);

  mappedRed = map(red, 0, 1023, 0, 255);
  mappedGreen = map(green, 0, 1023, 0, 255);
  mappedBlue = map(blue, 0, 1023, 0, 255);

  neopixel.setPixelColor (0, mappedRed, mappedGreen, mappedBlue); //define colors/set colors
  neopixel.setPixelColor (1, mappedRed, mappedGreen, mappedBlue);
}

void checkButton() {
  lastButton1State = button1State;
  button1State = digitalRead(buttonPin);
  if(lastButton1State == LOW && button1State == HIGH) {
    flipButtonState();
    delay(5);
  } else if(lastButton1State == HIGH && button1State == LOW) {
    delay(5);
  }
}
//this function defines "off and on"
void flipButtonState() {
    if(switchedOn == true) {
      switchedOn = false;
    } else if(switchedOn == false) {
      switchedOn = true;
    }  
}

void updatefunction() {
  if(switchedOn == true) {
    neopixel.show();
  } else {
    neopixel.setPixelColor (0, 0, 0, 0);
    neopixel.setPixelColor (1, 0, 0, 0);
    neopixel.show();
  }
}

Video:

Digital Electronics Lab 6

Video:

Code:

int potVals[4] = {0, 0, 0, 0};               //array for potentiometers
int mappedPotVals[4] = {0, 0, 0, 0};         //array for mapped pot values
int analogPinNum [4] = {A14, A15, A16, A17}; //array for analog pin numbers
int ledPins[4] = {29, 30, 31, 32};           //array for LEDs

int tempoPot = 0;
int frontBackToggle = 39;
int octaveToggle = 38;
int mappedPotValTempo = 0;
int currentStep = 0;                         //"counter" variable
unsigned long lastStepTime = 0;              //this is the timetracker setup

void setup() {
  for (int i = 0; i < 4; i++) {      //setting LEDs as outputs
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(octaveToggle, INPUT);
  pinMode(frontBackToggle, INPUT);
}
void loop() {
  if (digitalRead(frontBackToggle) == HIGH) {  //this checks which sequence direction is active.
    sequenceForward();
  } else {
    sequenceBackward();
  }
}

void sequenceForward() { //sequence forward
  tempoPot = analogRead(A18);
  mappedPotValTempo = map(tempoPot, 0, 1023, 100, 1000);   //set up tempo pot

  if (millis() >= lastStepTime + mappedPotValTempo) {      //using millis() to tell the teensy when to start seqenceing.

    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], LOW);
    usbMIDI.sendNoteOff(mappedPotVals[currentStep], 127, 1);

    if (currentStep == 3) {
      currentStep = 0;
    } else {
      currentStep++;
    }

    potVals[currentStep] = analogRead(analogPinNum[currentStep]);
    mappedPotVals[currentStep] = map(potVals[currentStep], 0, 1023, 60, 72);   //maps pots to note/MIDI values

    if (digitalRead(octaveToggle) == HIGH) {                 
      for (int i = 0; i < 4; i++) {
        mappedPotVals[i] = mappedPotVals[i] + 12;
      }
    }

    digitalWrite(ledPins[currentStep], HIGH);
    usbMIDI.sendNoteOn(mappedPotVals[currentStep], 127, 1);
  }
}

void sequenceBackward() { //sequence backwards
  tempoPot = analogRead(A18);
  mappedPotValTempo = map(tempoPot, 0, 1023, 100, 1000);   //tempo pot

  if (millis() >= lastStepTime + mappedPotValTempo) {     

    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], LOW);
    usbMIDI.sendNoteOff(mappedPotVals[currentStep], 127, 1);

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

    potVals[currentStep] = analogRead(analogPinNum[currentStep]);
    mappedPotVals[currentStep] = map(potVals[currentStep], 0, 1023, 60, 72);   

    if (digitalRead(octaveToggle) == HIGH) {                 
      for (int i = 0; i < 4; i++) {
        mappedPotVals[i] = mappedPotVals[i] + 12;
      }
    }

    digitalWrite(ledPins[currentStep], HIGH);
    usbMIDI.sendNoteOn(mappedPotVals[currentStep], 127, 1);
  }
}

Digital Lecture Assignment 6

Video:

My Code:

int ledPins[4] = {29, 30, 31, 32};   // LED array
int toggle = 34;
int tempo = 0;
int currentStep = 0;                 //"counter" variable
unsigned long lastStepTime = 0;      

void setup() {
  for (int i = 0; i < 4; i++) {      //for loop to set up all LEDs
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(toggle, INPUT);
}

void loop() {
  tempo = analogRead(A14); //setup tempo analog input
  if (digitalRead(toggle) == HIGH) {
    stepForwards();
  }else {
    stepBackwards();
  }
}

void stepForwards() {  
  if (millis() >= lastStepTime + tempo) {           //run the function with millis() instead of delay
    if (currentStep == 0) {                         //LED off
      digitalWrite(ledPins[3], LOW);
    }else {
      digitalWrite(ledPins[currentStep - 1], LOW);
    }
    
    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], HIGH);
    currentStep++;                                  //increase "counter"
    
    if (currentStep == 4) {
      currentStep = 0;                              //reset "counter"
    }
  }
}

void stepBackwards() {  
  if (millis() >= lastStepTime + tempo) {          
    if (currentStep == 3) {                         
      digitalWrite(ledPins[0], LOW);
    }else {
      digitalWrite(ledPins[currentStep + 1], LOW);
    }
    
    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], HIGH);
  
    if (currentStep == 0) {
      currentStep = 3;                              
    }else {
      currentStep--;                                
    }
  }
}

Digital Lecture Assignment 5

Code:

int buttonPins[2] = {33, 34};        //array for button pins
int ledPins[4] = {29, 30, 31, 32};   //array for LEDs

int potValue = 0;                    //variable for input pin of potentiometer
int toggle = 36;                     //variable for input pin of toggle switch

void setup() { //setup
  for (int i = 0; i < 4; i++) {      //using a for loop to set all LEDs as outputs
    pinMode(ledPins[i], OUTPUT);
  }
  for (int i = 0; i < 2; i++) {      //using a for loop to set all button pins as inputs
    pinMode(buttonPins[i], INPUT);
  }
  pinMode(toggle, INPUT);            //setting pin 36 as an input

}

void loop() {
  potValue = analogRead(A16);       //variable for input pin of potentiometer
  
  if (digitalRead(buttonPins[0]) == HIGH) {
    for (int i = 0; i < 4; i++) {   //use a for loop to turn on all LEDs
    digitalWrite(ledPins[i], HIGH);
  }
    delay(potValue);                   
    
    for (int i = 0; i < 4; i++) {   //use a for loop to turn off all LEDs
    digitalWrite(ledPins[i], LOW);
  }
    delay(potValue);                   
  }
  
  if (digitalRead(buttonPins[1]) == HIGH) {
    if (digitalRead(toggle) == HIGH) {
      for (int i = 0; i < 4; i++){
        digitalWrite(ledPins[i], HIGH); //LED on
        delay(potValue);                
        digitalWrite(ledPins[i], LOW);  //LED off
      }
    }
    if (digitalRead(toggle) == LOW) {
      for (int i = 3; i >= 0; i--){
        digitalWrite(ledPins[i], HIGH); //LED on
        delay(potValue);                
        digitalWrite(ledPins[i], LOW);  //LED off
      }
    }
  }
}      

Demo:

Design a site like this with WordPress.com
Get started