#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include "pitches.h"
// GUItool: begin automatically generated code
AudioInputUSB usb1; //xy=180,398
AudioInputAnalogStereo adcs1; //xy=194,194
AudioMixer4 mixer1; //xy=627,184
AudioMixer4 mixer3; //xy=631,401
AudioMixer4 mixer2; //xy=636,291
AudioMixer4 mixer4; //xy=642,534
AudioOutputAnalogStereo dacs1; //xy=1209,218
AudioOutputUSB usb2; //xy=1221,431
AudioConnection patchCord1(usb1, 0, mixer3, 0);
AudioConnection patchCord2(usb1, 1, mixer4, 0);
AudioConnection patchCord3(adcs1, 0, mixer1, 0);
AudioConnection patchCord4(adcs1, 0, mixer3, 1);
AudioConnection patchCord5(adcs1, 1, mixer2, 0);
AudioConnection patchCord6(adcs1, 1, mixer4, 1);
AudioConnection patchCord7(mixer1, 0, usb2, 0);
AudioConnection patchCord8(mixer3, 0, dacs1, 0);
AudioConnection patchCord9(mixer2, 0, usb2, 1);
AudioConnection patchCord10(mixer4, 0, dacs1, 1);
// GUItool: end automatically generated code
// Stuff for running keyboard
char keystate[61] = {0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0};
char keystate_prev[61] = {0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0};
char pins_rows[6] = {5,4,3,2,1,0};
char pins_cols[11] = {11,14,16,6,7,10,12,15,9};
void init_keys() {
for (int i = 0; i < 6; i++) {
pinMode(pins_rows[i], OUTPUT);
digitalWrite(pins_rows[i], LOW);
}
for (int i = 0; i < 9; i++) {
pinMode(pins_cols[i], OUTPUT);
digitalWrite(pins_cols[i], HIGH);
}
}
// read_keys - Implements switch matrix
void read_keys() {
int k = 0;
// Read bottom C
digitalWrite(pins_cols[0], LOW);
pinMode(pins_rows[5], INPUT_PULLUP);
delayMicroseconds(10);
if (digitalRead(pins_rows[5])) {
keystate[k] = 0;
}
else {
keystate[k] = 1;
}
pinMode(pins_rows[5], OUTPUT);
digitalWrite(pins_rows[5], LOW);
k++;
digitalWrite(pins_cols[0], HIGH);
// Read remaining keys
for (int i = 1; i < 11; i++) {
digitalWrite(pins_cols[i], LOW);
for (int j = 0; j < 6; j++) {
pinMode(pins_rows[j], INPUT_PULLUP);
delayMicroseconds(10);
if (digitalRead(pins_rows[j])) {
keystate[k] = 0;
}
else {
keystate[k] = 1;
}
pinMode(pins_rows[j], OUTPUT);
digitalWrite(pins_rows[j], LOW);
k++;
delayMicroseconds(500);
}
digitalWrite(pins_cols[i], HIGH);
}
}
void setup() {
AudioMemory(20);
// setup pin 13 LED for when keys pressed
pinMode(13, OUTPUT);
mixer3.gain(0, 0.5);
mixer3.gain(1, 0.5);
mixer4.gain(0, 0.5);
mixer4.gain(1, 0.5);
delay(1000);
// Initialise all pins ready for cycling through reading
init_keys();
}
void loop() {
/*
// read keys
read_keys();
// Output midi messages
int played_note = 0;
for (int i = 0; i < 61; i++) {
if (keystate[i] == 1) {
digitalWrite(13, HIGH);
played_note = 1;
if (keystate_prev[i] == 0) { // note on
usbMIDI.sendNoteOn(i+60, 99, 1); // 60 = C4
}
}
else if (keystate_prev[i] == 1) { // note off
usbMIDI.sendNoteOff(i+60, 0, 1); // 60 = C4
}
}
// Update previous states
for (int i = 0; i < 61; i++) {
keystate_prev[i] = keystate[i];
}
if (played_note == 0) { // light up keyboard
digitalWrite(13, LOW);
}
*/
// read the PC's volume setting
float vol = usb1.volume();
// scale to a nice range (not too loud)
// and adjust the audio shield output volume
if (vol > 0) {
// scale 0 = 1.0 range to:
// 0.3 = almost silent
// 0.8 = really loud
vol = 0.3 + vol * 0.5;
}
delay(100);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(10000); // wait for a second
// uncomment this code to allow Knob A3 to pan between songs
/*
int knob = analogRead(A3); // knob = 0 to 1023
float gain1 = (float)knob / 1023.0;
float gain2 = 1.0 - gain1;
mixer1.gain(0, gain1);
mixer1.gain(1, gain2);
mixer2.gain(0, gain1);
mixer2.gain(1, gain2);
*/
}