import processing.serial.*;

Serial teensySerial;

int inputVal = 0;

void setup() {

  frameRate(30);
  size(500, 500);

  //printArray(Serial.list());

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

void draw() {
  
  if (teensySerial.available() > 0) {
    
    inputVal = teensySerial.read();
    println(inputVal);
    
  }
  
  if (inputVal == 0) {
   
    background(200,0,90);
    
  } else if (inputVal == 1) {
   
    background(0,200,90);
    
  } else if (inputVal == 2) {
   
    background(90,0,200);
    
  } else if (inputVal == 3) {
   
    background(250,150,0);
    
  }
  
}

// obviously replace these with whatever pins your led and button are 
int buttonPin[4] = {33, 34, 35, 36};
int ledPin[4] = {7, 8, 9, 10};

bool lastButtonState[4];
bool buttonState[4];

bool switchedOn[4];

int currentStep = 0;

int delayLength = 200;

unsigned long lastStepTime;

void setup() {
  Serial.begin(9600);
  
  for (int i = 0; i<sizeof(buttonPin); i++){

    pinMode(i+33, INPUT);
    
  }
  for (int i = 0; i<sizeof(ledPin); i++){

    pinMode(i+7, OUTPUT);
    
  }
}

void loop() 
{

  delayLength = map(analogRead(13), 0, 1023, 100, 1000);
  
  stepForwards();
  checkButtons();
  updateLeds();
}

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[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() {
  for (int i = 0; i<4; i++) {
    if(switchedOn[i] == true || i == currentStep) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
  }
}

void stepForwards() {
  
  if (millis() > lastStepTime + delayLength) {

    lastStepTime = millis();
    
    usbMIDI.sendNoteOff(60, 0, 1);
    
    currentStep++;
    if (currentStep >= 4) {
      currentStep = 0;
    }

    Serial.write(currentStep);

    if (switchedOn[currentStep]) {

      usbMIDI.sendNoteOn(60, 127, 1);
      
    }
    
  }
  
}


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.