This Project uses 1 potentiometer and 2 buttons. The potentiometer is turned for the specific note you'd like to play. The first button is pressed when you'd like to play that note. The final button is pressed when you'd like to record a max of 4 notes in a row to be played at regular intervals. The recording button is a toggle, meaning it can be turned on, and you can move your hand to the other button to play, and then turned back off. The wires used were green, yellow, and orange for variables, red for power, and black for ground. The code was designed to utilize Millis for background playing.
#include "MIDIUSB.h"
int inputX = A0; //potentiometer
int playNote = 13; //note player
int trackOne = 12; //record
int clampNoteMin = 40;
int clampNoteMax = 80;
int savedNote = 0; //to stop note
int currentNote = 0;
int recordOne[4] = {0,0,0,0};
//record toggle
int previousState;
int currentState;
bool status;
int lastLoopNote = 0; //to stop it next loop
//edge detetcion for the recording button
bool lastPlayState = 1;
//write index for going through the array
int writeIndex = 0;
//millis in bg
unsigned long previousMillis = 0;
int interval= 500;
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void setup() {
pinMode(inputX, INPUT_PULLUP);
pinMode(playNote, INPUT_PULLUP);
pinMode(trackOne, INPUT_PULLUP);
previousState = digitalRead(trackOne); //toggle
Serial.begin(115200);
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
MidiUSB.flush(); // cord stuff
}
void turnOff(){
for (int note = 0; note < 128; note++) {
midiEventPacket_t off = {0x08, 0x80 | 0, note, 0};
MidiUSB.sendMIDI(off);
}
MidiUSB.flush();
}
//toggle from led toggle
void toggleRecord(){
currentState = digitalRead(trackOne);
if(currentState != previousState){
if(currentState == 1)
{
status = !status;
}
delay(50);
}
previousState = currentState;
}
void loop() {
unsigned long currentMillis = millis();
toggleRecord();
Serial.print("REC STATUS: ");
Serial.println(digitalRead(tra ckOne));
bool playState = digitalRead(playNote);
if (lastPlayState == 1 && playState == 0) {
Serial.println("PLAY PRESSED");
Serial.print("Saved note: ");
Serial.println(savedNote);
//save the note for turning off and play
savedNote = map(analogRead(inputX),0,1023, clampNoteMin,clampNoteMax);
noteOn(0, savedNote, 127);
//record played note to the array
if (status) {
recordOne[writeIndex] = savedNote;
writeIndex = writeIndex + 1;
if (writeIndex >= 4) {
writeIndex = 0;
}
}
}
// button release stop playing note
if (lastPlayState == 0 && playState == 1) {
noteOff(0, savedNote, 0);
}
lastPlayState = playState;
//saving part 1
//read track 1
//play saved song in the background
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (lastLoopNote != 0) {
noteOff(0, lastLoopNote, 0);
}
int currentLoopNote = recordOne[currentNote];
if (currentLoopNote != 0) {
noteOn(0, currentLoopNote, 127);
lastLoopNote = currentLoopNote;
}
else {
lastLoopNote = 0;
}
currentNote = currentNote + 1;
if (currentNote >= 4) {
currentNote = 0;
}
}
MidiUSB.flush();
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.