hi, im fairly new to arduino coding and working on a project for university. I have a code that works for the keys to a MIDI xylophone:
int pinRead;
char pinAssignments[6] ={
'A0', 'A1', 'A2', 'A3', 'A4', 'A5'};
byte PadNote[6] = {
60, 62, 64, 65, 67, 69,}; // MIDI notes from 0 to 127 (Mid C = 60)
int PadCutOff[6] =
{
400,400,400,400,400,400}; // Minimum Analog value to cause a drum hit
int MaxPlayTime[6] = {
90,90,90,90,90,90}; // Cycles before a 2nd hit is allowed
#define midichannel 1; // MIDI channel from 0 to 15 (+1 in "real world")
boolean VelocityFlag = true; // Velocity ON (true) or OFF (false)
//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************
boolean activePad[6] = {
0,0,0,0,0,0}; // Array of flags of pad currently playing
int PinPlayTime[6] = {
0,0,0,0,0,0}; // Counter since pad started to play
byte status1;
int pin = 0;
int hitavg = 0;
//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************
void setup()
{
Serial.begin(57600); // connect to the serial port 115200
}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
void loop()
{
for(int pin=0; pin < 16; pin++) //
{
//int pin = 3;
// for (pinRead=0; pinRead < 16, pin++){
hitavg = analogRead(pinAssignments[pin]);
//Serial.println(hitavg);
// read the input pin
if((hitavg > PadCutOff[pin]))
{
if((activePad[pin] == false))
{
if(VelocityFlag == true)
{
// hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin])); // With full range (Too sensitive ?)
hitavg = (hitavg / 8) -1 ; // Upper range
}
else
{
hitavg = 127;
}
MIDI_TX(144,PadNote[pin],hitavg); //note on
PinPlayTime[pin] = 0;
activePad[pin] = true;
}
else
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;
}
}
else if((activePad[pin] == true))
{
PinPlayTime[pin] = PinPlayTime[pin] + 1;
if(PinPlayTime[pin] > MaxPlayTime[pin])
{
activePad[pin] = false;
MIDI_TX(144,PadNote[pin],0);
}
}
}
}
//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
status1 = MESSAGE + midichannel;
Serial.write(status1);
Serial.write(PITCH);
Serial.write(VELOCITY);
}
and a code that works to light up the keys while hit:
// set pin numbers:
int buttonPin = 2; // the number of the push button pin
int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
How can i connect these together into the same code? i tried copying and pasting, but everytime i do this i recieve error messages. Any help would be much appreciated?
cheers in advance