Go Down

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

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.

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

}




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".


HazardsMind

#5
Apr 18, 2013, 06:22 pm Last Edit: Apr 18, 2013, 06:29 pm by HazardsMind Reason: 1
Fixed
Added: You had quite a few errors that were hard to describe how to fix. About half way through of writing the progress, I just decided it would be easier just to do it myself.
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);
  }

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

}



Its not tested but it should work.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

sorry about the very vague 'it doesnt work' i was getting quite stressed out with it not working. Had been working on it for a good while. Thanks very much, i really appreciate that. I was just wondering, if i was going to add more LED lights (1 for each key) would i just add in more of the input numbers?

Code: [Select]
int buttonPin = 2; // the number of the push button pin
int ledPin = 13; // the number of the LED pin


e.g

Code: [Select]
int buttonPin = 1, 2, 3, 4, 5, 6; // the number of the push button pin
int ledPin = 8, 9, 10, 11, 12, 13; // the number of the LED pin

HazardsMind

You can't do it like that,
Quote
int buttonPin = 1, 2, 3, 4, 5, 6; // the number of the push button pin
int ledPin = 8, 9, 10, 11, 12, 13; // the number of the LED pin
but you can make them into arrays.
Quote
int buttonPin[] = {1, 2, 3, 4, 5, 6}; // the number of the push button pin
int ledPin[] = {8, 9, 10, 11, 12, 13}; // the number of the LED pin

Now if you do it this way, you will need to tweak your code a bit.

Note: if you plan to use the serial monitor, DONT USE pins 1 or 0 in your code.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

ok cool. ill give it a try. Instead of having 
Code: [Select]
pinMode(ledPin, OUTPUT);, would that change to the number of the LED pin rather than just output because there's more of them?

HazardsMind

You will need to use FOR loops and change it to:

Code: [Select]

for(int ledCount = /*first led pin*/ ; ledCount < /* 1 + last led pin*/ ; ledCount++)
{
   pinMode(ledPin[ ledCount ], OUTPUT);
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

and the same for the button pin INPUT, but with its 1-6 range? sorry if i keep asking obvious questions. i really do appreciate your help, when it comes to errors in the code, i seem to always spiral out of control and every correction leads to a new error message

HazardsMind

Quote
and the same for the button pin INPUT, but with its 1-6 range?
yes
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

after putting in for
Code: [Select]
(int buttonCount = 1 ; buttonCount < 6 ; buttonCount++); 

}
i get an error on:

 
Code: [Select]
buttonState = digitalRead(buttonPin); stating 'invalid conversion from 'int*' to 'unit8_t'

im confused now..  :~

HazardsMind

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

This is the full code..

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 [] = {1, 2, 3, 4, 5, 6}; // the number of the push button pin
int ledPin[] = {8, 9, 10, 11, 12, 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:
for (int ledCount = 8 ; ledCount < 13 ; ledCount++)
{
}
  // initialize the pushbutton pin as an input:
for (int buttonCount = 1 ; buttonCount < 6 ; buttonCount++); 

}
//*******************************************************************************************************************
// 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);
  }

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

}

Go Up