Carson Smith
Published

Team Pineapple Project - DIG3602C

Final Unconventional Sumo Bot Controller UCF-Spring 20XX-DIG3602C-Davis TEAM PINEAPPLE. Team Members: Max, Javon, Carson

BeginnerShowcase (no instructions)49
Team Pineapple Project - DIG3602C

Things used in this project

Story

Read more

Schematics

Breadboard

Code

MIDI_InputTest.ino

C/C++
// List of 5 notes, ordered low to high
// iterate through list bottom to top,
// look for major minor augmented diminished
// record last know chord as an enum

// MAJOR : 60, 64, 67 (0, 4, 3)
// MINOR : 60, 63, 67 (0, 3, 4)
// AUGMENTED : 60, 64, 68 (0, 4, 4)
// DIMINISHED: 60, 63, 66 (0, 3, 3)
#include <MIDI.h>  // Add Midi Library
#include"SoftwareSerial.h"
/* #define LEDPWM1 3
#define LEDPWM2 5
#define LEDPWM3 6
#define LEDPWM4 9
#define LEDPWM5 10
#define LEDPWM6 11
#define LED13 13 */
enum EChordType
{
  major,
  minor,
  augmented,
  diminished,
  none
};


const int nonValue = -9;
int recentNotes[5] = {nonValue, nonValue, nonValue, nonValue, nonValue}; // Initialize the array with -1
bool sustaining = false;
bool moving = true;


EChordType lastChord = EChordType::none;
//Create an instance of the library with default name, serial port and settings
//SoftwareSerial midiSerial(6,7);
//MIDI_CREATE_INSTANCE(SoftwareSerial, midiSerial, MIDI);
MIDI_CREATE_DEFAULT_INSTANCE();
//SoftwareSerial midiSerial(6, 7);

SoftwareSerial mySerial(3, 2); // The bluetooth module connects to these pins on your Arduino

void setup() {
  Serial.begin(9600);
  //midiSerial.begin(31250);
  mySerial.begin(38400);
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MIDI.setHandleNoteOn(handleNoteOn);
  MIDI.setHandleNoteOff(handleNoteOff);
  MIDI.setHandlePitchBend(handlePitchBend);
  MIDI.setHandleControlChange(handleControlChange);
  MIDI.setHandleStart(handleStart);
  MIDI.setHandleStop(handleStop);
  //MIDI.setHandleStart(handleStop);
  //MIDI.setHandleSongSelect(handleSongSelect);
  MIDI.turnThruOff();
}

/* void handleSongSelect(byte song)
{
  Serial.print(song);
  Serial.println();
} */
void handleStop()
{
  moving = true;
}

void handleStart()
{
  moving = false;
}
float bendy;

void loop() {
  //mySerial.println("F");
  if(MIDI.read())
  {
    int length = sizeof(recentNotes) / sizeof(recentNotes[0]);
    qsort(recentNotes, length, sizeof(recentNotes[0]), sortLowToHigh);
    lastChord = checkValues();
    
    /* for (int i = 0; i < sizeof(recentNotes) / sizeof(recentNotes[0]); i++) {
      Serial.print(recentNotes[i]);
      Serial.print(" ");
    }  
    
    Serial.print(" | ");
    Serial.print(lastChord);
    Serial.println(); */

    //turnOnLights(lastChord);
  }
  if(moving)
      switch(lastChord)
      {
        case EChordType::major :
          mySerial.println("F");
          Serial.print("F");
          break;
        case EChordType::minor :
          mySerial.println("B");
          Serial.print("B");
          break;
        case EChordType::augmented :
          mySerial.println("R");
          Serial.print("R");
          break;
        case EChordType::diminished :
          mySerial.println("L");
          Serial.print("L");
          break;
        default :
          mySerial.println();
          Serial.print("reeeee");
          break;
      }
      Serial.println();
}
void handleNoteOn(byte channel, byte note, byte velocity) {
  //midiSerial.write(note);
  bool replacedNote = false;
  int length = sizeof(recentNotes) / sizeof(recentNotes[0]);

  for(int i = 0; i < length; i++)
      {
        if(recentNotes[i] == nonValue)
        {
          recentNotes[i] = note;
          replacedNote = true;
          break;
        }
      }
  if(!replacedNote)
  {
    recentNotes[4] = note;
  }
  
}

void handleNoteOff(byte channel, byte note, byte velocity) {
    int length = sizeof(recentNotes) / sizeof(recentNotes[0]);
    for(int i = 0; i < length; i++)
    {
      if(recentNotes[i] == note)
      {
        recentNotes[i] = nonValue;
        break;
      }
    }
}

void handlePitchBend(byte channel, int bend) { 
  //mySerial.println("F");
  /* bendy = bend;
  if(bend > 2000)
  {
    eTurnRad = ETurningRadiusMode::wide;
  }
  else if(bend < -2000)
  {
    eTurnRad = ETurningRadiusMode::narrow;
  }
  Serial.print(bendy);
  Serial.println(); */
}

void handleControlChange(byte channel, byte number, byte value) {
  //Serial.print(number);
  //Serial.println();
  if (number == 64) { // Check if the message is for the sustain pedal
    if (value == 0) {
      // Sustain pedal is released
      mySerial.println("C");
      sustaining = false;
    } else if (value == 127) {
      // Sustain pedal is fully pressed
      sustaining = true;
    }
  }
}

bool checkArray(int arr[], int len, int val) {
    for (int i = 0; i < len; i++) {
        if (arr[i] == val) {
            return true;
        }
    }
    return false;
}
EChordType checkValues()
{
  int length = sizeof(recentNotes) / sizeof(recentNotes[0]);
  for(int i = 0; i < length-2; i++)
  {
    int diff1 = abs(recentNotes[i+1] - recentNotes[i]);
    if(diff1 == 3 || diff1 == 4)
    {
      int diff2 = abs(recentNotes[i+2] - recentNotes[i+1]);
      if(diff2 == 3 || diff2 == 4)
      {
          if (diff1 == 4 && diff2 == 3) {
            return EChordType::major;
        } else if (diff1 == 3 && diff2 == 4) {
            return EChordType::minor;
        } else if (diff1 == 4 && diff2 == 4) {
            return EChordType::augmented;
        } else if (diff1 == 3 && diff2 == 3) {
            return EChordType::diminished;
        }
      }
    }
  }
  return lastChord;
}

/* void turnOffLights() {
  analogWrite(LEDPWM1, 0);
  analogWrite(LEDPWM2, 0);
  analogWrite(LEDPWM3, 0);
  analogWrite(LEDPWM4, 0);
  analogWrite(LEDPWM5, 0);
  analogWrite(LEDPWM6, 0);
} */

/* void turnOnLights(EChordType chordType) {
 switch (chordType) {
    case EChordType::major:
      analogWrite(LEDPWM1, 255);
      analogWrite(LEDPWM2, 0);
      analogWrite(LEDPWM3, 0);
      analogWrite(LEDPWM4, 0);
      break;
    case EChordType::minor:
      analogWrite(LEDPWM1, 0);
      analogWrite(LEDPWM2, 255);
      analogWrite(LEDPWM3, 0);
      analogWrite(LEDPWM4, 0); 
      break;
    case EChordType::augmented:
      analogWrite(LEDPWM1, 0);
      analogWrite(LEDPWM2, 0);
      analogWrite(LEDPWM3, 255);
      analogWrite(LEDPWM4, 0);
      break;
    case EChordType::diminished:
      analogWrite(LEDPWM1, 0);
      analogWrite(LEDPWM2, 0);
      analogWrite(LEDPWM3, 0);
      analogWrite(LEDPWM4, 255);
      break;
    case EChordType::none:
    turnOffLights();
      break;
 }
} */


int sortLowToHigh(const void *cmp1, const void *cmp2)
{
  // Need to cast the void * to int *
  int a = *((int *)cmp1);
  int b = *((int *)cmp2);
  // The comparison
  return a-b;
}
int sortHighToLow(const void *cmp1, const void *cmp2)
{
  // Need to cast the void * to int *
  int a = *((int *)cmp1);
  int b = *((int *)cmp2);
  // The comparison
  return b-a;
}

Credits

Carson Smith

Carson Smith

1 project • 0 followers

Comments