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);
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started