Final Project Documentation: Leo Shirky

Project Idea:

I wanted to use the instrument that Gopika and I made for one of the earlier assignments in a creative way. Given that I had an instrument that people could play, I decided to make a project where people could choose a song that they wanted to hear, and then play that song on the instrument.

I had originally planned to have the only sound come from the Piezo buzzer, however the Professor told me about a program called Sforzando, which is a software which allows the sound of musical instruments to be played from a computer. I coded four songs that I wanted to be played from the computer in Processing, and connected that to Sforzando, allowing the songs to sound much better than if they had been played on the Piezo buzzer.

The serial communication happened between Processing and Arduino by lighting up LEDs depending on which note was being played in each song. These LEDs also lit up when a note was played on the instrument. The instrument itself used an Ultrasonic Distance Sensor, and had 10cm ranges that mapped on to a specific note, which would then be played from the Piezo buzzer.

Videos:

Reflections:

There were some aspects of the project that I wish I could have improved, but either didn’t have the time or the knowledge to.

The first is that I would have requested to use speakers, rather than my computer speakers. I hadn’t taken into account that the ambient noise of the show would drown out even the loudest volume on my computer, which had always been too loud in tests. The result was that it could be hard to pick out the melody that was being played from the computer.

I wanted to play instrument through Sforzando rather than the Piezo buzzer, as the buzzer doesn’t sound as good and was quieter than the computer speakers. Unfortunately, despite spending hours trying to figure it out, I just couldn’t get this type of serial communication to work in this direction. I had to settle for the buzzer in order to finish the rest of the project in time for the show.

The last major improvement would have been to have multiple octaves, rather than just one. Unfortunately this would have required more space and would have been inconvenient to play, so I settled on just the one octave. I also couldn’t add flat or sharp notes, as I had a limited amount of space and decreasing the distance that counts as a note below about 10 cm makes the instrument much harder to play.

Code:

Arduino:

#define echoPinC 12
#define trigPinC 11

#include "pitches.h"

String notes[7] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4"};

int notesH[7] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4};

long durationC;
int distanceC;

int bNote = 0;
int aNote = 0;
int gNote = 0;
int fNote = 0;
int eNote = 0;
int dNote = 0;
int cNote = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);

//USDS code
  pinMode(trigPinC, OUTPUT);
  pinMode(echoPinC, INPUT);
}

void loop() {
//USDS Code
  digitalWrite(trigPinC, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinC, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinC, LOW);
  durationC = pulseIn(echoPinC, HIGH);
  distanceC = durationC * 0.034 / 2; 

  Serial.println("DistanceC: ");
  Serial.print(distanceC);
  Serial.print(" cm");
  Serial.print("mapped: ");

  int constrainedInput=constrain(distanceC, 1, 70);
  int mappedNote=map(constrainedInput, 1, 70, 0, 6);
  Serial.println(notes[mappedNote]);

  if(distanceC < 80){
    tone(10, notesH[mappedNote]);
  }else{
    noTone(10);
  }
    if(distanceC > 80){
    noTone(10);
  }
  if(mappedNote == 0){
    digitalWrite(2, HIGH);
  }else{
    digitalWrite(2, LOW);
  }
  if(mappedNote == 1){
    digitalWrite(3, HIGH);
  }else{
    digitalWrite(3, LOW);
  }
  if(mappedNote == 2){
    digitalWrite(4, HIGH);
  }else{
    digitalWrite(4, LOW);
  }
  if(mappedNote == 3){
    digitalWrite(5, HIGH);
  }else{
    digitalWrite(5, LOW);
  }
  if(mappedNote == 4){
    digitalWrite(6, HIGH);
  }else{
    digitalWrite(6, LOW);
  }
  if(mappedNote == 5){
    digitalWrite(7, HIGH);
  }else{
    digitalWrite(7, LOW);
  }
  if(mappedNote == 6){
    digitalWrite(8, HIGH);
  }else{
    digitalWrite(8, LOW);
  }
  if(distanceC > 80){
    digitalWrite(8, LOW);
  }

    while (Serial.available()) {
    bNote = Serial.parseInt();
    aNote = Serial.parseInt();
    gNote = Serial.parseInt();
    fNote = Serial.parseInt();
    eNote = Serial.parseInt();
    dNote = Serial.parseInt();
    cNote = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(8, bNote);
      digitalWrite(7, aNote);
      digitalWrite(6, gNote);
      digitalWrite(5, fNote);
      digitalWrite(4, eNote);
      digitalWrite(3, dNote);
      digitalWrite(2, cNote);
      
      
      Serial.print(mappedNote);
      Serial.print(',');
    }
  }
}

//NEW TAB

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Processing:

//this is for serial communication
import processing.serial.*;
Serial myPort;

  int channel = 0;
  int midiB4 = 71;
  int midiAB4 = 70;
  int midiA4 = 69;
  int midiGA4 = 68;
  int midiG4 = 67;
  int midiFG4 = 66;
  int midiF4 = 65;
  int midiE4 = 64;
  int midiDE4 = 63;
  int midiD4 = 62;
  int midiCD4 = 61;
  int midiC4 = 60;
  
  int velocity = 127;
  int timeInterval = 25; //keep at 25
  
  int wholeNote = 64*timeInterval;
  int halfNote = 32*timeInterval;
  int thirdNote = 24*timeInterval;
  int quarterNote = 16*timeInterval;
  int eighthNote = 8*timeInterval;
  int sixteenthNote = 4*timeInterval;
  int thirtysecondNote = 2*timeInterval;
  int sixtyfourthNote = 1*timeInterval;
  
  int freq=0;
  
  boolean bNoteOnOff=false;
  boolean aNoteOnOff=false;
  boolean gNoteOnOff=false;
  boolean fNoteOnOff=false;
  boolean eNoteOnOff=false;
  boolean dNoteOnOff=false;
  boolean cNoteOnOff=false;
  
  PImage beethoven;
  PImage happyBirthday;
  PImage littleApple;
  PImage uae;

//this is for MIDI
import themidibus.*; //Import the library

MidiBus myBus; // The MidiBus

void setup(){
  size(displayWidth, displayHeight);
  background(255);
  
  beethoven = loadImage("beethoven.png");
  happyBirthday = loadImage("happyBirthday.png");
  littleApple = loadImage("littleApple.png");
  uae = loadImage("uae.png");
  
  image(beethoven, 0, 0, displayWidth/2, displayHeight/2);
  image(happyBirthday, displayWidth/2, 0, displayWidth/2, displayHeight/2);
  image(littleApple, 0, displayHeight/2, displayWidth/2, displayHeight/2);
  image(uae, displayWidth/2, displayHeight/2, displayWidth/2, displayHeight/2);
  
  //this is for serial communication
  printArray(Serial.list());
  String portname=Serial.list()[4];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  
  textSize(40);
  fill(255);
  text("Press: 1", displayWidth/4, displayHeight/2 - 30);
  fill(0);
  text("Press: 2", displayWidth - displayWidth/4, displayHeight/2 - 30);
  text("Press: 3", displayWidth/4, displayHeight/2 + 20);
  text("Press: 4", displayWidth - displayWidth/4, displayHeight/2 + 20);
  
  MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
  myBus = new MidiBus(this, -1, "sforzando"); // Create a new MidiBus with no input device and the default Java Sound Synthesizer as the output device.
}


void draw(){
  
  int value = 90;
  int channel = 0;
  
  myBus.sendControllerChange(channel, freq, value); // Send a controllerChange
  delay(2000);
  println("Frequency:" );
  print(freq);
}

void keyPressed(){
  switch(key){
    case'1': playOdeToJoy();
    break;
    case'2': playHappyBirthday();
    break;
    case'3': playXiaoPingGuo();
    break;
    case'4': playIshyBilady();
    break;
  }
}


void noteOn(int channel, int pitch, int velocity) {
  // Receive a noteOn
  println();
  println("Note On:");
  println("--------");
  println("Channel:"+channel);
  println("Pitch:"+pitch);
  println("Velocity:"+velocity);
}

void noteOff(int channel, int pitch, int velocity) {
  // Receive a noteOff
  println();
  println("Note Off:");
  println("--------");
  println("Channel:"+channel);
  println("Pitch:"+pitch);
  println("Velocity:"+velocity);
}

void controllerChange(int channel, int number, int value) {
  // Receive a controllerChange
  println();
  println("Controller Change:");
  println("--------");
  println("Channel:"+channel);
  println("Number:"+number);
  println("Value:"+value);
}

void delay(int time) {
  int current = millis();
  while (millis () < current+time) Thread.yield();
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==1){
      freq=(int)map(values[0],0,6,60,71);
    }
  }
  myPort.write(int(cNoteOnOff)+","+int(dNoteOnOff)+","+int(eNoteOnOff)+","+int(fNoteOnOff)+","+int(gNoteOnOff)+","+int(aNoteOnOff)+","+int(bNoteOnOff)+"\n");
}


//NEW TAB

void playHappyBirthday(){
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(halfNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(halfNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiA4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = true;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiA4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiB4, velocity); // Send a Midi noteOn
  bNoteOnOff = true;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiB4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiB4, velocity); // Send a Midi noteOn
  bNoteOnOff = true;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiB4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiA4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = true;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiA4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(halfNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  int number = 0;
  int value = 90;

  myBus.sendControllerChange(channel, number, value); // Send a controllerChange
  delay(2000);
}

//NEW TAB

void playIshyBilady(){
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiA4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = true;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiA4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiA4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = true;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiA4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(halfNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  int number = 0;
  int value = 90;

  myBus.sendControllerChange(channel, number, value); // Send a controllerChange
  delay(2000);
}


//NEW TAB

void playOdeToJoy(){

  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiE4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = true;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(thirdNote);
  myBus.sendNoteOff(channel, midiE4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(halfNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;

  int number = 0;
  int value = 90;

  myBus.sendControllerChange(channel, number, value); // Send a controllerChange
  delay(2000);
}


//NEW TAB

void playXiaoPingGuo(){
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiAB4, velocity); // Send a Midi noteOn
  bNoteOnOff = true;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiAB4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiAB4, velocity); // Send a Midi noteOn
  bNoteOnOff = true;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiAB4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiG4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = true;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiG4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  delay(eighthNote);
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiAB4, velocity); // Send a Midi noteOn
  bNoteOnOff = true;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiAB4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiC4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = true;
  delay(eighthNote);
  myBus.sendNoteOff(channel, midiC4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiF4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = true;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiF4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiD4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = true;
  cNoteOnOff = false;
  delay(quarterNote);
  myBus.sendNoteOff(channel, midiD4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiA4, velocity); // Send a Midi noteOn
  bNoteOnOff = false;
  aNoteOnOff = true;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(thirdNote);
  myBus.sendNoteOff(channel, midiA4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  myBus.sendNoteOn(channel, midiAB4, velocity); // Send a Midi noteOn
  bNoteOnOff = true;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  delay(halfNote);
  myBus.sendNoteOff(channel, midiAB4, velocity); // Send a Midi nodeOff
  bNoteOnOff = false;
  aNoteOnOff = false;
  gNoteOnOff = false;
  fNoteOnOff = false;
  eNoteOnOff = false;
  dNoteOnOff = false;
  cNoteOnOff = false;
  
  int number = 0;
  int value = 90;

  myBus.sendControllerChange(channel, number, value); // Send a controllerChange
  delay(2000);
  
}

 

Final Project: Music Mimicry

So, I changed my mind about which idea to pursue for my final project. I decided to use the instrument Gopika and I made a few weeks ago, and have users mimic a snippet of a song that was played by using the instrument.

Unfortunately I wasn’t able to complete user testing yet, however I have someone lined up after class, and I will update this post with their feedback.

The professor suggested that I use Sforzando and Processing to play the songs, so that they sound better than the buzzer on an Arduino board, and so that is what I will be doing this class.

However, I have a working version that uses the Piezo buzzer on the Arduino, which plays a section of Ode to Joy, which the user than then mimic by playing the instrument themselves.

#define echoPinC 12
#define trigPinC 11
# include "pitches.h"

String notes6[7] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4"};

int notesLow[7] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4};

const int buttonPin = 2;
const int buzzer = 4;
int songLength = 17;
char notes[] = "eefggfedccdeedd ";
int beats[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
int tempo = 288;

long durationC;
int distanceC;

void setup() {

  pinMode(trigPinC, OUTPUT);
  pinMode(echoPinC, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buzzer, OUTPUT);

  digitalWrite(buttonPin, LOW);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trigPinC, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinC, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinC, LOW);
  durationC = pulseIn(echoPinC, HIGH);
  distanceC = durationC * 0.034 / 2; 

  Serial.print("DistanceC: ");
  Serial.print(distanceC);
  Serial.print(" cm");
  Serial.print("mapped: ");


  digitalRead(buttonPin);
  if(buttonPin == LOW){
    Serial.println("Button Not Pushed");
  }
  

  int constrainedInput=constrain(distanceC, 0, 70);
  int mappedNoteC = map(constrainedInput, 1, 70, 0, 7);
  
  if(distanceC > 70){
  noTone(4);
  }else{
    tone(4, notesLow[mappedNoteC]);
 }
  
  Serial.println(notes6[mappedNoteC]);

  int i, duration;

  if (digitalRead(buttonPin) == HIGH){
    for (i = 0; i < songLength; i++){
      duration = beats[i]*tempo;

      if (notes[i] == ' '){
        delay(duration);
      }else{
        tone(buzzer, frequency(notes[i]), duration);
        delay(duration);
      }
      delay(tempo/10);
    }
  }
}

int frequency(char note){

  int i;

  const int numNotes = 8;

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  for (i = 0; i < numNotes; i++){
    if (names[i] == note){
      return(frequencies[i]);
    }
  }
  return(0);
}

 

Final Project Commitment

identify what you think will be hardest to complete, and start building it. Post the final idea, material/space needs, group/solo arrangement, and your progress on the project so far.

Idea:

I have committed to the screaming plane simulator as my final project idea, which will be a solo project.

Materials:

I have decided to not use the Orientation Sensor, and instead have the project be a 2D simulation, with pitch controlled from the Arduino. I will need a slider analog input for controlling pitch. I believe that everything else I will need is in the Arduino boxes we already have.

Challenges:

I haven’t made anything with a sprite sheet yet, and in my searches so far I have not found any sprite sheets of airplanes that are what I am looking for. Either I will have to create my own sprite sheet, or look much harder. I also haven’t used my computer microphone for anything yet, and so figuring out how to take in that input will be something I will need to learn.

Work So Far:

So far I have worked out what mechanisms I want to be present in my simulator (ie takeoff from an airport, landing in another airport, a progress bar, high scores, time-reducing tokens, obstacles to be avoided, and sound input to increase speed). I have an early version of my train simulator code from when I had planned to make a version of the Chrome Dino Game which I believe will be useful as a starting point for the Plane Simulator Code.

Final Project Ideas

Idea #1: Music Mimicry

This idea is built on the instrument that we created for last weeks assignment. First, a short section of a famous song be played on the buzzer. Then, the player would have to replicate that short section of song by playing the instrument. If they play accurately, they win, but if they play inaccurately, they lose.

Input:

  • Multiple (at least two) ultrasonic distance sensors
  • Buttons: (1 on/off button, 1 play sound button, 1 accept input button)

Output:

  • 1 buzzer
  • Multiple LEDs (1 LED per note, to give some visual representation of the sounds being played.

Idea #2: Plane Simulator

This idea involves using an Adafruit BNO055 Absolute Orientation Sensor, or an analog sound sensor. Players would use the orientation/sound sensor to control an airplane avatar on screen. For example, yelling loudly would make the plane go faster. The game itself would be a side-scrolling course, with time to complete the course being recorded, and collectible objectives that lower the total time at the finish line. If you completed the course in 48 seconds but collected 5 tokens that each take off 2 seconds (for example), your final score is 38. Top scores would be recorded on a leaderboard so players could see how they stack up to others.

Input:

  • Adafruit BNO055 Absolute Orientation Sensor AND/OR Analog Sound Sensor

Output:

  • Visuals on computer screen

Serial Communication Exercises

Exercise 1:

Processing:
import processing.serial.*;
Serial myPort;
int xPos=0;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[4];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  ellipse(xPos,height/2,30,30);
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values=int(s);
    if (values >= 0){
      xPos=(int)map(values,0,1023,0, width);
    }
  }
  myPort.write("\n");
}
Arduino:
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
}

void loop() {
  while (Serial.available()) {
    if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor);
    }
  }
}

Exercise 2:

Processing:
import processing.serial.*;
Serial myPort;
//int xPos=0;
//int yPos=0;
boolean onOff=false;
boolean onOff2=false;

void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[4];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  //ellipse(xPos,yPos,30,30);
  if (mousePressed){
    if(mouseX<=width/2)
      onOff2=true;
    else
      onOff=true;
  }else{
    onOff=onOff2=false;
  }
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==1){
      //xPos=(int)map(values[0],0,1023,0, width);
      //yPos=(int)map(values[1],0,1023,0, height);
    }
  }
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
}
Arduino:
int left = 0;
int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(2, right);
      digitalWrite(5, left);
      Serial.println();
    }
  }
}

Exercise 3:

Processing:
import processing.serial.*;
Serial myPort;
boolean onOff=false;
boolean onOff2=false;

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;


void setup(){
  delay(1000);
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[4];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  
  position = new PVector(width/2, 0);
  velocity = new PVector(0,0);
  acceleration = new PVector(0,0);
  gravity = new PVector(0, 0.5*mass);
  wind = new PVector(0,0);
  hDampening=map(mass,15,80,.98,.96);
  delay(1000);
}

void draw(){
  background(255);
  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x,position.y,mass,mass);
  if (position.y > height-mass/2) {
      velocity.y *= -0.9;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
      onOff = true;
      println("BOUNCE!!!!");
    }else{
      onOff = false;
 }
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
  int values[]=int(split(s,','));
  if (values.length==2){
      int mappedValues=(int)map(values[0],0,1023,0, width);
  if (mappedValues <= width/2){
    wind.x=-1;
  }
  if (mappedValues > width/2){
    wind.x=1;
  }
  }
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
  println("OnOFF"+onOff);
}
}


void applyForce(PVector force){
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed(){
  if (key==' '){
    mass=random(15,80);
    position.y=-mass;
    velocity.mult(0);
  }
}
Arduino:
int left = 0;
int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(2, right);
      digitalWrite(5, left);
      delay(2);
      int sensor = analogRead(A0);
      delay(1);
      int sensor2 = analogRead(A1);
      delay(1);
      Serial.print(sensor);
      Serial.print(',');
      Serial.println(sensor2);
    }
  }
}

Video:

 

Leo and Gopika Make Bagpipes

Inspiration:

Gopika was inspired by this  video of a Coldplay performance, in which it appears like the band is playing music on lasers in the air. We decided we would try to make an “air guitar” in a similar style. We would use six ultrasonic distance sensors that acted like “strings” and buttons that acted like the frets on a guitar. However because ultrasonic distance sensors need a clock, and each Arduino board only has three, we were limited to how many distance sensors we could use. We ended up with two distance sensors, each controlled by two buttons.

Process:

We first built a circuit with the ultrasonic distance sensor to see what kind of ranges were measurable. Next, we added a buzzer to the circuit, and wrote code to play a note whenever a given distance was triggered. For example, between 0 and 60 cm, every 10 cm corresponds to one note.

We added control buttons to the circuit next, one of which turns the system on (starts detecting distance) and the other changes the octave. Each code had two arrays of notes, each a full octave, giving the buzzer access to two full octaves of notes. If the button is pressed, the higher octave is played, if the button is not pressed, the lower octave is played.

Challenges:

We had a lot of trouble wiring the buttons into the circuit. At first, they didn’t work at all, and pressing the button wasn’t even being detected. Next, the press would be detected, but it would lag the system so that no tone was played for a few seconds. Finally Gopika realized that we had wired the circuit incorrectly and the buttons finally worked.

Final Work:

See if you recognize the song:

And with the octave change:

Main Code:

#define echoPinC 12 
#define trigPinC 11 
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int BUTTON_PIN2 = 8;
# include "pitches.h"

String notes6[7] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4"};

int notes[7] = {NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6};
int notesHigh[7] = {NOTE_C7, NOTE_D7, NOTE_E7, NOTE_F7, NOTE_G7, NOTE_A7, NOTE_B7};

long durationC;
int distanceC;

int playNote;

bool start = false;

void setup() {
  // put your setup code here, to run once:

  pinMode(trigPinC, OUTPUT);
  pinMode(echoPinC, INPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
   int buttonState = digitalRead(BUTTON_PIN); // read new state
int offState = digitalRead(BUTTON_PIN2); 

 if (offState == LOW) {
  start=true;
 }

if(start)
{

  // put your main code here, to run repeatedly:
digitalWrite(trigPinC, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinC, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinC, LOW);
  durationC = pulseIn(echoPinC, HIGH);
  distanceC = durationC * 0.034 / 2; 

  Serial.print("DistanceC: ");
  Serial.print(distanceC);
  Serial.print(" cm");
  Serial.print("mapped: ");

  int constrainedInput=constrain(distanceC, 1, 70);
  
  int mappedNoteC = map(constrainedInput, 1, 70, 0, 6);

Serial.println(notes6[mappedNoteC]);

  if (buttonState == LOW) {
   Serial.print("button unpressed: "); 
  // noTone(4);
  playNote = notesHigh[mappedNoteC];
  
  if(distanceC > 70){
   noTone(4);
  } 
  
  }
  else
  if (buttonState == HIGH) {
   Serial.print("button pressed: ");
      playNote = notes[mappedNoteC];
  }
if (offState == LOW) {
   Serial.print("button unpressed: ");
  noTone(4);
}
 else
  if (offState == HIGH) {
tone(4, playNote);
  }
  if(distanceC > 70){
    noTone(4);
  }
}
}
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

 

Analog and Digital LEDs

Inspiration:

I struggled to think of something creative to make, but I eventually settled on a kind of game where a red, a green, and a blue LED would light up, each with a random brightness, and then the player would have to use a potentiometer to control the brightnesses of each of the three LEDs in order to match the random brightness that they had. If the player was within a certain range, a green LED would light up, signifying that the player was correct, otherwise a red LED would light up signifying that the player was too far off.

Process:

I used some of the code from the Maintain State circuit we built, along with some from the Analog LED circuit from class as well.

Challenges:

My idea was quite complicated, especially for someone new at coding like me, and I spent too long trying to work it out. I eventually realized that I could have the same process with a blue LED that the player would control, which would make the circuit much simpler. I simplified my code and rebuilt my circuit, although for some reason, I encountered the same problem I had before, which was that all three LEDs would light up, and neither the button nor the potentiometer would change that.

I tinkered a little more, and the video shows as far as I got, however because I spent so much time on the more complicated version of this idea, I did not have enough time to completely troubleshoot either the code or the circuit. I had initially thought for a long time that the problem must have been somewhere in the code, but I think now that it might be in the circuit.

Final Work:

Diagram of my circuit on TinkerCAD

Code:

//potentiometer
const int knob = A0;
//target blue LED
const int bled = 5;
//incorrect and correct LEDs
const int iled = 9;
const int cled = 10;
//blue button
const int bbutton = 2;
//on/off button
const int obutton = 12;

bool bledOn = false;
bool iledOn = false;
bool cledOn = false;

//on off button
bool oButtonOnOff = false;
bool oButtonPrevOnOff = LOW;

//blue button
bool bButtonOnOff = false;
bool bButtonPrevOnOff = LOW;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  pinMode(bled, OUTPUT);
  pinMode(iled, OUTPUT);
  pinMode(cled, OUTPUT);
  pinMode(bbutton, INPUT);
  pinMode(obutton, INPUT);

}

void loop() {
//turning the LED on and off with the BLACK button
  bool oButtonCurrentState = digitalRead(obutton);
  int randomLED = random(255);
  if(oButtonCurrentState == HIGH && oButtonCurrentState != oButtonPrevOnOff){
    digitalWrite(bled, randomLED);
  }
  oButtonPrevOnOff = oButtonCurrentState;
  bledOn = !bledOn;

//turning the LED on and off with the BLUE button
  bool bButtonCurrentState = digitalRead(bbutton);
  if(bButtonCurrentState == HIGH && bButtonCurrentState != bButtonPrevOnOff){
    digitalWrite(bled, 255);
  }
  bButtonPrevOnOff = bButtonCurrentState;
  bledOn = true;
  
  int knobValue = analogRead(knob);
  //fix high and low based on potentiometer
  int mappedValue = map(knobValue, 850, 350, 0, 255);
  int constrainedValue = constrain(mappedValue, 0, 255);

  digitalRead(bled);
  if(bled == HIGH && bButtonPrevOnOff == HIGH){
    analogRead(knob);
    analogWrite(bled, constrainedValue);
  }

  int bledValue = analogRead(bled);
  if(bButtonCurrentState == HIGH &&  (randomLED - 5) < (bledValue) < (randomLED + 5)){
    bool cledOn = !cledOn;
  }
  if(bButtonCurrentState == LOW &&  (bledValue) < (randomLED - 5) || (randomLED + 5) < (bledValue)){
    iledOn = !iledOn;
  }
}

 

Week 8: Hands Free Switch

Introduction:

I am obsessed with privacy. My dorm is my safe haven from the pressure of school work and socializing, so I am obviously annoyed when that peace is disturbed. It is from this that I had the idea of a Do-Not-Disturb button. An automatic version of the signs you hang on hotel room doors, to signal to roommates and visitors that I am not to be disturbed.

Procedure:

My initial idea was to have a simple circuit like we built last week. When I close my door, the circuit is closed, and if I pushed a button, a red LED would light up, and maintain state after the button was pushed. If I pushed it again, the LED would turn off, signaling that I am available. If my door was open, then the light would be off, as the circuit was broken.

However, in order to make this more hands-free, the Professor suggested that I use some other signal besides a push button to indicate that I was not to be disturbed. He suggested that I put conductive material on my pillow and the bed below the pillow, so when I was asleep the circuit would close and the light would come on. The circuit itself is very simple. I made a diagram of it in tinkerCAD, and attached a screenshot here.

The small breadboard off to the side represents the conductive material attached to the pillow. (I don’t know how to put conductive material in tinkerCAD). In this diagram, the circuit is closed. The code for this is also quite simple, with the Arduino reading the state of the “pillowPin” and writing that to the LED pin.

The procedure is relatively simple. When the conductive material on the bed and the pillow comes into contact, the circuit is closed.

When the circuit is closed, the Arduino will read the current state of the LED pin and change it. (The LED is always off to start). When the circuit is broken, the LED will turn off.

Demonstration:

https://youtu.be/ujdsTZ9_EqY

const int ledPin = 3;
const int pillowPin = 2;

bool sleepWake = LOW;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(pillowPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  bool currentState = digitalRead(pillowPin);
  if(currentState == HIGH){
    sleepWake = !sleepWake;
  }

}

Week 7: Midterm

Inspiration:

I really like trains, so I wanted my game to represent that aspect of my interests. I was inspired by the Dino game on Google Chrome, as that had similar characteristics.

Process:

I basically tried to brute force this entire code. I found a video of someone who made their own version of the Dino game from Google, from which I drew some inspiration.

I have five classes: Ground, Tree, Obstacle, Player, and Station.

The ground is simply a line that acts as the rail, and has regularly appearing lines underneath which represent the ties of the track.

The obstacles are now only the red signals, but it used to have the green signals and stations. There is a boolean for SPAD (Signal Passed at Danger) which triggers a fail if you pass a red signal too quickly.

The trees now include the green signals, as the trees and green signals are all passive background objects.

The player always stays in the same spot and “controls” the train. They are really controlling the speed at which the objects move across the background. The W and S keys control the speed.

The station has a green rectangle underneath the image that is suppose to denote the stopping zone, however the station no longer loads and I don’t know why.

Final work:

Challenges:

I had lots of challenges. If you had seen my midterm progress post, you can tell how different the two versions are. The biggest two issues are that the game will fail seemingly for no reason, and the station apparently never shows up. I think that is because the green signals and station both used to be a part of obstacles, there is some vestige that no longer appears on screen, but still fails. I also tried to get the station to load only after a certain amount of time, but once it was moved to its own class, it stopped working.

If you keep your speed under 3 km/h, you won’t fail, but also the station will never appear. Because I was attempting to fix this issue, I didn’t have time to add sound.

Code:

PImage myTrain;
PImage greenSignal;
PImage redSignal;
PImage trainStation;
PImage tree;
PImage tree1;

ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Station> stations = new ArrayList<Station>();
ArrayList<Tree> trees = new ArrayList<Tree>();
ArrayList<Ground> grounds = new ArrayList<Ground>();

int obstacleTimer = 0;
int minTimeBetObs = 100;
int randomAddition = 0;
int groundCounter = 0;
float speed = 0;

int groundHeight = 100;
int playerXpos = 100;
int highScore = 0;

Player driver;

void setup(){
  size(1024,400);
  frameRate(60);
   
  
  myTrain = loadImage("myTrain0000.png");
  myTrain.resize(0, 100);
  greenSignal = loadImage("signalGreen0000.png");
  greenSignal.resize(50, 0);
  redSignal = loadImage("signalRed0000.png");
  redSignal.resize(50, 0);
  trainStation = loadImage("stationTrain0000.png");
  trainStation.resize(500, 0);
  tree = loadImage("chree0000.png");
  tree.resize(0, 100);
  tree1 = loadImage("chree0001.png");
  tree1.resize(0, 100);
  
  driver = new Player();
}

void draw(){
  background(250);
  stroke(0);
  strokeWeight(2);
  line(0, height - groundHeight, width, height - groundHeight);
  
  updateObstacles();
  addStations();
  
  if(driver.score > highScore){
    highScore = driver.score;
  }
  
  textSize(20);
  fill(0);
  text("score: " + driver.score, 5, 20);
  text("High Score: " + highScore, width - (140 + (str(highScore).length() * 10)), 20);
  text("Speed (km/h): " + speed, width/2, 20);
  text("Press W to Accelerate, S to Deccelerate", width/2, 50);
}

void keyPressed(){
  switch(key){
    case'w': speed++;
              break;
    case's': speed--;
              break;
  }
}

void keyReleased(){
  switch(key){
              
    case'r': if(driver.dead){
               restart();
              }
              break;
  }
}

void updateObstacles(){
  showObstacles();
  driver.show();
  if(!driver.dead){
    obstacleTimer ++;
    if(obstacleTimer > minTimeBetObs + randomAddition){
      addObstacles();
    }
    groundCounter++;
    if(groundCounter > 10){
      groundCounter = 0;
      grounds.add(new Ground());
    }
    moveObstacles();
    driver.update();
  }
  else{
   textSize(32); 
   fill(0);
   text("You Failed and Endangered the Lives of Everyone Onboard", 180, 200);
   textSize(16);
   text("(Press R to Restart)", 330, 230);
  }
}

void showObstacles(){
 for(int i = 0; i < grounds.size(); i++){
   grounds.get(i).show();
 }
 for(int i = 0; i < obstacles.size(); i++){
   obstacles.get(i).show();
 }
 for(int i = 0; i < trees.size(); i++){
   trees.get(i).show();
 }
}

void addObstacles(){
  if(random(1) < 0.15){
    trees.add(new Tree(floor(random(3))));
  }
  else{
    obstacles.add(new Obstacle(floor(random(3))));
  }
  randomAddition = floor(random(50));
  obstacleTimer = 0;
}

void moveObstacles(){
  for(int i = 0; i < grounds.size(); i++){
   grounds.get(i).move(speed);
   if(grounds.get(i).posX < -playerXpos){
     grounds.remove(i);
     i--;
   }
 }
 for(int i = 0; i < obstacles.size(); i++){
   obstacles.get(i).move(speed);
   if(obstacles.get(i).posX < -playerXpos){
     obstacles.remove(i);
     i--;
 }
 }
 for(int i = 0; i < trees.size(); i++){
   trees.get(i).move(speed);
   if(trees.get(i).posX < -playerXpos){
     trees.remove(i);
     i--;
   }
 }
}

void addStations(){
  if(driver.score > 10){
    stations.add(new Station(1));
  }
}

void restart(){
  driver = new Player();
  obstacles = new ArrayList<Obstacle>();
  trees = new ArrayList<Tree>();
  grounds = new ArrayList<Ground>();
  
  obstacleTimer = 0;
  randomAddition = floor(random(50));
  groundCounter = 0;
  speed = 0;
}

class Ground{
  float posX = width;
  float posY = height - groundHeight + 1;
  int w = 1;
  
  Ground(){
  }
  
  void show(){
   stroke(0);
   strokeWeight(3);
   line(posX, posY, posX + 10, posY);
  }
  
  void move(float speed){
   posX -= speed; 
  }
}

class Obstacle{
  float posX;
  int w, h;
  int type;
  
  Obstacle(int t){
    posX = width;
    type = t;
    switch(type){
      case 0: w = 30;
              h = 60;
              break;
    }
  }
  
  void show(){
    switch(type){
      case 0: image(redSignal, posX - redSignal.width/2, height - groundHeight - redSignal.height);
              break;
    }
  }
  
  void move(float speed){
    posX -= speed;
  }
  
  boolean SPAD(float playerX){
    float playerRight = playerX + myTrain.width/2;
    float thisRight = posX + redSignal.width/2;
    
    if(playerRight > thisRight && speed > 3){
       return true;
     }
     else{
       if(playerRight > thisRight + 1){
         return false;
       }
     }
    return false;  
  }
}

class Player{
 float posY = 0;
 int size = 20;
 boolean dead = false;
 
 int runCount = -5;
 int lifespan;
 int score;
 
 Player(){
 }

 
 void show(){
   image(myTrain, playerXpos - myTrain.width/2, height - groundHeight - (myTrain.height - 15));

   
   if(!dead){
     runCount++;
   }
   if(runCount > 5){
     runCount = -5;
   }
 }
 
 void move(){
     posY = 0; 
   for(int i = 0; i < obstacles.size(); i++){
     if(dead){
       if(obstacles.get(i).SPAD(playerXpos)){
         dead = true; 
       }
     }
     else{
       if(obstacles.get(i).SPAD(playerXpos)){
         dead = true;
       }
     }
     
   }
   
   for(int i = 0; i < stations.size(); i++){
     if(dead){
       if(stations.get(i).overshoot(playerXpos)){
         dead = true; 
       }
     }
     else{
       if(stations.get(i).overshoot(playerXpos)){
         dead = true;
       }
     }
     
   }
 }
 
 void update(){
  incrementCounter();
  move();
 }
 
 void incrementCounter(){
  lifespan++;
  if(lifespan % 3 == 0){
    score += 1;
  }
 }
}


class Station{
  float posX;
  int w, h;
  int type;
  
  Station(int t){
    posX = width;
    type = t;
    switch(type){
      case 0: w = 60;
              h = 40;
              break;
    }
  }
  
  void show(){
    switch(type){
      case 0: image(trainStation, posX - trainStation.width/2, height - groundHeight - trainStation.height);
              noStroke();
              fill(0, 255, 0, 50);
              rect(posX - trainStation.width/2, height - groundHeight, trainStation.width, 50);
              break;
    }
  }
  
  void move(float speed){
    posX -= speed;
  }
  boolean overshoot(float playerX){
    float playerRight = playerX + myTrain.width/2;
    float thisRight = posX + trainStation.width/2;
    
    if(playerRight > thisRight){
       return true;
     }
    return false;  
  }
  
  boolean stopped(float playerX){
    float playerRight = playerX + myTrain.width/2;
    float thisRight = posX + trainStation.width/2;
    
    if(posX - trainStation.width/2 < playerRight && playerRight < thisRight){
       return true;
     }
    return false;
    }
  }

class Tree{
  float w, h;
  float posX, posY;
  int type;
  
  Tree(int t){
    posX = width;
    type = t;
    switch(t){
      case 0: w = 20;
              h = 40;
              break;
      case 1: w = 20;
              h = 40;
              break;
      case 2: w = 20;
              h = 40;
              break;
    }
  }
  
  void show(){
    switch(type){
      case 0: image(tree, posX - tree.width/2, height - groundHeight - (posY + tree.height - 50));
              break;
      case 1: image(tree1, posX - tree1.width/2, height - groundHeight - (posY + tree1.height - 50));
              break;
      case 2: image(greenSignal, posX - greenSignal.width/2, height - groundHeight - greenSignal.height);
              break;
    }
  }
  
  void move(float speed){
    posX -= speed;
  }
}

 

Week 6 – Midterm Progress

Inspiration:

I really like trains, and so I thought I could try to make a basic train simulator in Processing. I wanted the user to control a subway train between two stations, have to navigate a red signal, and stop and the next station without overshooting the platform. I also wanted the background to be populated with other NPC trains.

Process:

I started by trying to build the background for the simulator, which would simply be a top-down view of the tracks and tunnel. I made a platform, two rails for the tracks, and made a class for trains. I tried to add a .png image for the tracks, but for some reason, it wouldn’t load in. I never got an error message, I simply couldn’t see the image. As a place-holder, I added some rectangles for the rails. You can’t control the train yet, but the goal is to be able to.

Final Work:

Challenges:

I had a lot of challenges so far. For some reason, the rectangle of the subway train stays on the screen without disappearing. As mentioned above, I can’t load the tracks .png file, and finally I haven’t yet added user control over the train.

Code:

Train[] trains;

int platformDepth;
int platformLength;
int railWidth;
int railSpacing;
int sleeperWidth;
int sleeperLength;
int sleeperSpacing;

PFont stationFont;
PImage img;

void setup(){
  size(1280,640);
  background(255);
  stationFont = createFont("Courier New", 30);
  
  
  img = loadImage("subwayTracks.png");


  
  platformDepth = 100;
  platformLength = 800;
  railWidth = 2;
  railSpacing = 28;
  sleeperWidth = 5;
  sleeperLength = 30;
  sleeperSpacing = 15;
  
  trains = new Train[3];
  for (int i=0; i<trains.length; i++) {
    trains[i] = new Train(0, height-platformDepth-10-railSpacing, (10));
  }
  
  
}

void draw(){


  
//platform
  stroke(0);
  fill(255, 200, 100);
  rect(width/2-platformLength/2, height-platformDepth, platformLength, platformDepth);
//rails
  noStroke();
  fill(0);
  rect(0, (height-platformDepth) - 10, width, railWidth);
  rect(0, (height-platformDepth) - 10 - railSpacing, width, railWidth);
  
  noStroke();
  fill(0);
//  for (int i=0; i>0; i++){
//    rect(i, height-platformDepth - sleeperLength, sleeperWidth, sleeperLength);
//  }

 for (int i=0; i<trains.length; i++) {
    trains[i].runTrain();
    
  textFont(stationFont);
  textAlign(CENTER);
  text("23rd St. Station", width/2, height-platformDepth+50);
  
  image(img, (height-platformDepth) - 10, width/2);

  
}
}
class Train{
  float posX, posY;
  float trainWidth, trainLength;
  color trainColor;
  float wheelWidth, wheelHeight;
  float speed;
  
  Train(float _posX_, float _posY_, float _speed){    
    posX = _posX_;
    posY = _posY_;
    trainWidth = 40;
    trainLength = trainWidth*1;
    trainColor = color(126,127,128);
    speed = _speed;

  }
 
  void runTrain(){
    trainBody();
    driveTrain();
  }
  
  void constructTrain(float _posX_, float _posY_){
 
}

void trainBody(){

  fill(trainColor);
  noStroke();
  rect(posX, posY, trainLength, trainWidth, 7);
  
  //trailing carriages
  //fill(trainColor);

  rect(posX-trainLength-10, posY, trainLength, trainWidth, 7);
  rect(posX-(trainLength*2)-10, posY, trainLength, trainWidth, 7);
}


void driveTrain(){
  posX += speed;
  
  if (posX > width+trainWidth/2) {
    posX = -trainWidth/2;
  }
  
 }
}