Pages: [1] 2   Go Down
Author Topic: Needing a little bit of guidance for connecting arduino codes...  (Read 1026 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 / smiley-cool -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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3489
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46144
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2934
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: April 18, 2013, 11:29:26 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int buttonPin = 2; // the number of the push button pin
int ledPin = 13; // the number of the LED pin

e.g

Code:
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
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2934
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2934
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
for(int ledCount = /*first led pin*/ ; ledCount < /* 1 + last led pin*/ ; ledCount++)
{
   pinMode(ledPin[ ledCount ], OUTPUT);
}
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2934
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and the same for the button pin INPUT, but with its 1-6 range?
yes
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

after putting in for
Code:
(int buttonCount = 1 ; buttonCount < 6 ; buttonCount++); 
 
}
i get an error on:

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

im confused now..  smiley-confuse
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2934
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post what you have done.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the full code..

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

}
Logged

Pages: [1] 2   Go Up
Jump to: