Go Down

Topic: Needing a little bit of guidance for connecting arduino codes... (Read 1 time) previous topic - next topic

thunderkid

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

UKHeliBob

It would help considerably if you would post the code in code tags which avoids the problem of smileys appearing in the code.  Have you read the stickies at the start of this forum section ?

What errors do you get when you combine the 2 sets of code ?  You will need to post the combined code and explain what you want it to do.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

thunderkid

hi, sorry about that. I originally tried to just paste one code into another. Ive broken down the 2 codes and tried to fit each part of the code into the right place on the other.
Here is what i have come up with, my error right now says 'expected un-qualified-id before '{' token' i have underlined/bold where it appears on my code. If you could help it would be much appreciated. Thanks

Code: [Select]
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)

// 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
//*******************************************************************************************************************
// 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

// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
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);
}
}


[b][u]{[/u][/b]
{

 
  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);

}




thunderkid

sorry the bold/underlined didnt work, it appears as this: {

PaulS

Code: [Select]
char pinAssignments[6] ={
  'A0', 'A1', 'A2', 'A3', 'A4', 'A5'};

Wrong, for several reasons. First, and array of pin names is useless. Second, the Arduino does not do multi-byte characters.

Putting the { on a new line and putting the } on a new line would make that a lot easier to read. The } disappears when jammed up next to the last character.

Explaining what problems you are having (it doesn't compile; it shows these errors; it doesn't do what I want; etc.) is much better than "it doesn't work".


Go Up