Pages: [1]   Go Down
Author Topic: midi error syntax issues  (Read 656 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hello
I am struggling with trying to finalise some code I am working on. I am using a PING sensor to trigger different midi notes depending on proximity (3 notes in total) and although the method I am using seems to work in other MIDI enabled arduino code i have used for some reason with this one I keep getting the following error message:


 In function 'void loop()':
error: 'midiOUT' was not declared in this scope

I have always used this method of midiOUT and I am stuck as to what I am doing wrong this time around. Any suggestions?


Code:
/* New Variables */
int delay_time = 30; // delay for this amount each write cycle.
byte channel = 1;

byte printing_byte = 0;

/* Ultrasound Sensor
 *------------------
 *
 * Reads values (00014-01199) from an ultrasound sensor (3m sensor)
 * and writes the values to the serialport.
 *
 * http://www.xlab.se | http://www.0j0.org
 * copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
 *
 */

 // CODE EDITED BY SEBASTIAN TOMCZAK 23 APRIL 2008
const int pingPin = 12;
int val = 0;
int ultrasoundValue = 0;
int timecount = 0;
int brightness = 0;// Echo counter
int ledPin1 =  8;
int ledPin2 =  9;
int ledPin3 =  10;
int ledPin4 =  11;
int ledPin5 = 13;
int whitePin = 2;
const int fan =  7;
int light1 = 6;
int light2 = 5;
int light3 = 4;
int light4 = 3;

int fanState = LOW;             // ledState used to set the LED
long previousMillis = 0;  

long interval = 5000;

int ledPin1State = LOW;             // ledState used to set the LED
long previousMillis1 = 10;  

long interval1 = 60;

int ledPin2State = LOW;             // ledState used to set the LED
long previousMillis2 = 20;  

long interval2 = 60;

int ledPin3State = LOW;             // ledState used to set the LED
long previousMillis3 = 30;  

long interval3 = 80;

int ledPin4State = LOW;             // ledState used to set the LED
long previousMillis4 = 10;  

long interval4 = 80;// LED connected to digital pin 13

void setup() {
  Serial.begin(31250);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3, OUTPUT);  
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(light4, OUTPUT);
  pinMode(light3, OUTPUT);
  pinMode(whitePin, OUTPUT);            // Sets the digital pin as output

}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  /* Writing out values to the serial port
   * -------------------------------------------------------------------
   */

  
 // Append echo pulse time to ultrasoundValue
  if((inches >= 15) && (inches < 20)){
     midiOUT(0x90, 85, 127);
    
    digitalWrite(light3, HIGH);
    delay(1000);
    digitalWrite(light3, LOW);
    digitalWrite(light4, HIGH);
    delay(3000);
    digitalWrite(light4, LOW);
 }
 
 else{
  digitalWrite(light3, LOW);
  digitalWrite(light4, LOW);
 }
 
 
 if((inches >= 60) && (inches < 66)){
    midiOUT(0x90, 85, 127);
    digitalWrite(light2, HIGH);
    delay(1000);
    digitalWrite(light2, LOW);
 }
 
 else{
  digitalWrite(light2, LOW);
 }
 
  if((inches >= 93) && (inches < 128)){
    midiOUT(0x90, 85, 127);
    digitalWrite(light1, HIGH);
    delay(1000);
    digitalWrite(light1, LOW);
 }
 
 else{
  digitalWrite(light1, LOW);
 }
 
               // wait for a second;

if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = millis();  

    // if the LED is off turn it on and vice-versa:
    if (fanState == LOW)
      fanState = HIGH;
    else
      fanState = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(fan, fanState);
  }
  
  if (millis() - previousMillis1 > interval1) {
    // save the last time you blinked the LED
    previousMillis1 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin1State == LOW)
      ledPin1State = HIGH;
    else
      ledPin1State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin1, ledPin1State);
  }
  
  if (millis() - previousMillis2 - 20 > interval2) {
    // save the last time you blinked the LED
    previousMillis2 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin2State == LOW)
      ledPin2State = HIGH;
    else
      ledPin2State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledPin2State);
  }
  
  if (millis() - previousMillis3 - 30 > interval3) {
    // save the last time you blinked the LED
    previousMillis3 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin3State == LOW)
      ledPin3State = HIGH;
    else
      ledPin3State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledPin3State);
  }

 if (millis() - previousMillis4 - 10 > interval4) {
    // save the last time you blinked the LED
    previousMillis4 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin4State == LOW)
      ledPin4State = HIGH;
    else
      ledPin4State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin4, ledPin4State);
  }

  /* BEGIN EDITED CODE */

void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);}
 

  /* END EDITED CODE */

  /* Lite up LED if any value is passed by the echo pulse
   * -------------------------------------------------------------------
   */

  /* Delay of program
   * -------------------------------------------------------------------
   */

  delay(delay_time);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

« Last Edit: September 21, 2009, 05:56:31 pm by kazimier » Logged

UK
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

and here is an example of the same midiOUT method working on another code:
Code:
 /*
 * Optical Tachometer
 *
 * Uses an IR LED and IR phototransistor to implement an optical tachometer.
 * The IR LED is connected to pin 13 and ran continually. A status LED is connected
 * to pin 12. Pin 2 (interrupt 0) is connected across the IR detector.
 *
 *
 */
//#define DEBUG      

int ledPin = 13;                // IR LED connected to digital pin 13
int statusPin = 12;  // LED connected to digital pin 12
int lightPin1 = 6;
int lightPin2 = 9;
int lightPin3 = 10;
int lightPin4 = 11;
int brightness = 0;
int delay_time = 40; // delay for this amount each write cycle.
int noteDown = LOW;
byte MIDI_channel = 0;
byte cc_number = 0;
byte incomingByte;
byte button;
byte note;
byte printing_byte = 0;
int Value = 0;
int midi_pitch = 0;
int mappedrpm = 0;
int state=0;
#ifdef DEBUG
const int DEBUG_RATE = 9600;        // Serial debugging communicates at 9600 baud
const int SERIAL_PORT_RATE = DEBUG_RATE;
#else
const int MIDI_BAUD_RATE = 31250;   // MIDI communicates at 31250 baud
const int SERIAL_PORT_RATE = MIDI_BAUD_RATE;
#endif



volatile byte rpmcount;
volatile int status;

unsigned int rpm;

unsigned long timeold;

// flag switching stuff
boolean top_speed_reached = false;
int on_threshold = 120;
int off_threshold = 90;

void rpm_fun()
{
  //Each rotation, this interrupt function is run twice, so take that into consideration for
  //calculating RPM
  //Update count
  rpmcount++;

  //Toggle status LED  
  if (status == LOW) {
    status = HIGH;
  }
  else {
    status = LOW;
  }
  digitalWrite(statusPin, status);
}

void setup()
{
  state = 0;

  Serial.begin(SERIAL_PORT_RATE);
  //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
  //Triggers on FALLING (change from HIGH to LOW)
  attachInterrupt(0, rpm_fun, FALLING);

  //Turn on IR LED
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  //Use statusPin to flash along with interrupts
  pinMode(statusPin, OUTPUT);

  rpmcount = 0;
  rpm = 0;
  timeold = 0;
  status = LOW;


}

void loop()
{
  //Update RPM every second
  delay(800);
  //Don't process interrupts during calculations
  detachInterrupt(0);
  //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
  //happened once per revolution instead of twice. Other multiples could be used
  //for multi-bladed propellers or fans
  rpm = 3*1000/(millis() - timeold)*rpmcount;
  timeold = millis();
  rpmcount = 0;
  attachInterrupt(0, rpm_fun, FALLING);
  brightness = map(rpm, 0, 50, 0, 255);
  analogWrite(lightPin1, brightness);
  printing_byte = map(rpm, 0, 50, 0, 127);{
  midiOUT(0xB0, 7, printing_byte);
  }

  if (!top_speed_reached) {
    if (printing_byte >= on_threshold) {
      top_speed_reached = true;
      midiOUT(0x90, 65, 127);
    }
  } else {
    if (printing_byte <= off_threshold) {
      top_speed_reached = false;
      midiOUT(0x90, 45, 127);
    }
  }
  
}




  //Write it out to serial port
  //Restart the interrupt processing
  


void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);

}




Logged

Gothenburg, Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems there is an end } missing at the end of the loop(), before the definition of midiOUT.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34239
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Replace this:-
Code:
digitalWrite(ledPin4, ledPin4State);
  }

  /* BEGIN EDITED CODE */

void midiOUT(char command, char value1, char value2) {

with this:-
Code:
digitalWrite(ledPin4, ledPin4State);
  } }

  /* BEGIN EDITED CODE */

void midiOUT(char command, char value1, char value2) {

You missed a } in the loop() function and it swallowed the definition of midiOUT
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34239
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Beaten by 1 second. I know how Manchester City feel now.  ;D
Logged

UK
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

that is some speedy responses and of course solved the problem.
It has been some long days and spotting a rogue } here and there is proving to be difficult.
thanks
Logged

UK
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hello -
tried the code out in situ today and there is an issue with the delay - it seems the way I have had to place the delay back into the code rather than being after serialPrint the PING sensor is not working correctly and is not getting a read out fast enough to work.

Below is the way code I have at the moment:


Code:
/* New Variables */
int delay_time = 10; // delay for this amount each write cycle.
byte channel = 1;

byte printing_byte = 0;

/* Ultrasound Sensor
 *------------------
 *
 * Reads values (00014-01199) from an ultrasound sensor (3m sensor)
 * and writes the values to the serialport.
 *
 * http://www.xlab.se | http://www.0j0.org
 * copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
 *
 */

 // CODE EDITED BY SEBASTIAN TOMCZAK 23 APRIL 2008
const int pingPin = 12; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0;
int brightness = 0;// Echo counter
int ledPin1 =  8;
int ledPin2 =  9;
int ledPin3 =  10;
int ledPin4 =  11;
int ledPin5 = 13;
int whitePin = 2;
const int fan =  7;
int light1 = 6;
int light2 = 5;
int light3 = 4;
int light4 = 3;

int fanState = LOW;             // ledState used to set the LED
long previousMillis = 0;  

long interval = 5000;

int ledPin1State = LOW;             // ledState used to set the LED
long previousMillis1 = 10;  

long interval1 = 60;

int ledPin2State = LOW;             // ledState used to set the LED
long previousMillis2 = 20;  

long interval2 = 60;

int ledPin3State = LOW;             // ledState used to set the LED
long previousMillis3 = 30;  

long interval3 = 80;

int ledPin4State = LOW;             // ledState used to set the LED
long previousMillis4 = 10;  

long interval4 = 80;// LED connected to digital pin 13

void setup() {
  Serial.begin(31250);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3, OUTPUT);  
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(light4, OUTPUT);
  pinMode(light3, OUTPUT);
  pinMode(whitePin, OUTPUT);            // Sets the digital pin as output

}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  /* Writing out values to the serial port
   * -------------------------------------------------------------------
   */

  
 // Append echo pulse time to ultrasoundValue
  if((inches >= 15) && (inches < 20)){
     midiOUT(0x90, 38, 127);
    
    digitalWrite(light3, HIGH);
    delay(1000);
    digitalWrite(light3, LOW);
    midiOUT(0x90, 39, 127);
    digitalWrite(light4, HIGH);
    delay(3000);
    digitalWrite(light4, LOW);
 }
 
 else{
  digitalWrite(light3, LOW);
  digitalWrite(light4, LOW);
 }
 
 
 if((inches >= 60) && (inches < 66)){
    midiOUT(0x90, 37, 127);
    digitalWrite(light2, HIGH);
    delay(1000);
    digitalWrite(light2, LOW);
 }
 
 else{
  digitalWrite(light2, LOW);
 }
 
  if((inches >= 93) && (inches < 128)){
    midiOUT(0x90, 36, 127);
    digitalWrite(light1, HIGH);
    delay(1000);
    digitalWrite(light1, LOW);
 }
 
 else{
  digitalWrite(light1, LOW);
 }
 
               // wait for a second;

if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = millis();  

    // if the LED is off turn it on and vice-versa:
    if (fanState == LOW)
      fanState = HIGH;
    else
      fanState = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(fan, fanState);
  }
  
  if (millis() - previousMillis1 > interval1) {
    // save the last time you blinked the LED
    previousMillis1 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin1State == LOW)
      ledPin1State = HIGH;
    else
      ledPin1State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin1, ledPin1State);
  }
  
  if (millis() - previousMillis2 - 20 > interval2) {
    // save the last time you blinked the LED
    previousMillis2 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin2State == LOW)
      ledPin2State = HIGH;
    else
      ledPin2State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledPin2State);
  }
  
  if (millis() - previousMillis3 - 30 > interval3) {
    // save the last time you blinked the LED
    previousMillis3 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin3State == LOW)
      ledPin3State = HIGH;
    else
      ledPin3State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledPin3State);
  }

 if (millis() - previousMillis4 - 10 > interval4) {
    // save the last time you blinked the LED
    previousMillis4 = millis();  

    // if the LED is off turn it on and vice-versa:
    if (ledPin4State == LOW)
      ledPin4State = HIGH;
    else
      ledPin4State = LOW;
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin4, ledPin4State);
  }
delay(40);
}

  /* BEGIN EDITED CODE */

void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);}
 

  /* END EDITED CODE */

  /* Lite up LED if any value is passed by the echo pulse
   * -------------------------------------------------------------------
   */

  /* Delay of program
   * -------------------------------------------------------------------
   */





long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

as you can see the delay is now inside the code before the Serial.print, where as before I had it after the Serial.print which gave an accurate read out but meant that the midiOUT did not work.
any suggestions or help would be greatly appreciated.
Logged

UK
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

where as before I had it looking like this:


Code:
oid midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);}


  /* END EDITED CODE */

  /* Lite up LED if any value is passed by the echo pulse
   * -------------------------------------------------------------------
   */

  /* Delay of program
   * -------------------------------------------------------------------
   */

  delay(delay_time);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
Logged

Pages: [1]   Go Up
Jump to: