"was not declared" but works fine elsewhere

Hello,
I previously wrote some code which when a button is pressed a Midi command is sent from a Linksprite Midi Shield; this worked fine.

I am now trying to modify it to send a Midi command when the voltage on A0 changes, but am now getting an error: "exit status 1 'noteOn' was not declared in this scope" on the line noteOn(0x90, noteWand, 0x45); in the below code:

#include <NewPing.h>

#define SONAR_NUM 2      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(6, 7, MAX_DISTANCE)
};

//Declare Notes
const int noteWand = 0x1E;
const int noteHallway = 0x1F;
const int noteCircus = 0x20;

int stateWand;
int prevWand = analogRead(A0);

void setup() {
  Serial.begin(31250);

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

  stateWand =   analogRead(A0);
  if (stateWand != prevWand) {
    noteOn(0x90, noteWand, 0x45);
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);
    noteOn(0x90, noteWand, 0x00);
    prevWand = stateWand;
                delay(1000);
  } else {
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  }

  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    if sonar[1].ping_cm() <> 0 {
      noteOn(0x90, noteHallway, 0x45);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);
      noteOn(0x90, noteHallway, 0x00);
      delay(1000);
    } else if sonar[2].ping_cm() <> 0 {
      noteOn(0x90, noteCircus, 0x45);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);
      noteOn(0x90, noteCircus, 0x00);
      delay(1000);
    } else {
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    }
  }
  void noteOn(int cmd, int pitch, int velocity) {
    Serial.write(cmd);
    Serial.write(pitch);
    Serial.write(velocity);
  }

Can anyone see where I've gone wrong?
Thanks,
Ryan

  • Where did you find the above syntax ? :thinking:


  • Google Arduino if
if (condition) {
  //statement(s)
}

Try moving the definition of noteOn() to before loop().

You're also missing a closing } for the loop() function.

Is 'noteOn' declared in a Midi library, which you have forgotten to include?

Edit - Please ignore this post, as I have now seen where your attempted
definition is.

As written, that sketch never "worked fine". It could never have compiled, let alone work. The syntax in the two if statements is incorrect, and you're missing the closing bracket for the loop() function, which puts the declaration of noteOn inside the loop() function, which is also incorrect.

The revised sketch below at least compiles. I won't guarantee that it works.

#include <NewPing.h>

#define SONAR_NUM 2       // Number of sensors.
#define MAX_DISTANCE 200  // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
   NewPing(4, 5, MAX_DISTANCE),  // Each sensor's trigger pin, echo pin, and max distance to ping.
   NewPing(6, 7, MAX_DISTANCE)
};

//Declare Notes
const int noteWand = 0x1E;
const int noteHallway = 0x1F;
const int noteCircus = 0x20;

int stateWand;
int prevWand = analogRead(A0);

void setup() {
   Serial.begin(31250);

   // initialize digital pin LED_BUILTIN as an output.
   pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

   stateWand = analogRead(A0);
   if( stateWand != prevWand ) {
      noteOn(0x90, noteWand, 0x45);
      digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
      delay(1000);
      noteOn(0x90, noteWand, 0x00);
      prevWand = stateWand;
      delay(1000);
   } else {
      digitalWrite(LED_BUILTIN, LOW);  // turn the LED off by making the voltage LOW
   }

   for( uint8_t i = 0; i < SONAR_NUM; i++ ) {  // Loop through each sensor and display results.
      delay(50);                              // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
      if( sonar[1].ping_cm() != 0 ) {
         noteOn(0x90, noteHallway, 0x45);
         digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
         delay(1000);
         noteOn(0x90, noteHallway, 0x00);
         delay(1000);
      } else if( sonar[2].ping_cm() != 0 ) {
         noteOn(0x90, noteCircus, 0x45);
         digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
         delay(1000);
         noteOn(0x90, noteCircus, 0x00);
         delay(1000);
      } else {
         digitalWrite(LED_BUILTIN, LOW);  // turn the LED off by making the voltage LOW
      }
   }
}
void noteOn(int cmd, int pitch, int velocity) {
   Serial.write(cmd);
   Serial.write(pitch);
   Serial.write(velocity);
}

Thank you everyone for your replies :slight_smile:

@LarryD the syntax is from the "Ping 3 Sensors Sketch" at https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home; does this not look correct?
Thanks, I forgot the proper syntax of an if statement (I don't use Arduino very often) but after refreshing it I see that I was missing brackets and the != operator.

@PaulRB thanks for that tip; the previously working sketch had it within the loop but I'll try it in the setup section instead.

@sterretje thanks, good eye! Not sure how that happened.

@JohnLincoln Thanks for your input anyway :slight_smile:

@van_der_decken sorry, what I meant was a previous sketch which worked I then copied and modified, and in modifying I broke it.
Thanks for your notes - I implemented those fixes and it compiles... now to see if it actually works!

Thanks again, everybody!

Ah, I think I see what you mean now;
I have modified the code as such:

#include <NewPing.h>

#define SONAR_NUM 2      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(6, 7, MAX_DISTANCE)
};

//Declare Notes
const int noteWand = 0x1E;
const int noteHallway = 0x1F;
const int noteCircus = 0x20;

int stateWand;
int prevWand = analogRead(A0);

void setup() {
  Serial.begin(31250);

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

  void noteOn(int cmd, int pitch, int velocity) {
    Serial.write(cmd);
    Serial.write(pitch);
    Serial.write(velocity);
  }

void loop() {

  stateWand =   analogRead(A0);
  if (stateWand != prevWand) {
    noteOn(0x90, noteWand, 0x45);
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);
    noteOn(0x90, noteWand, 0x00);
    prevWand = stateWand;
                delay(1000);
  } else {
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  }

  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
if (sonar[i].ping_cm() != 0) {
  if (i == 1) {
      noteOn(0x90, noteHallway, 0x45);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);
      noteOn(0x90, noteHallway, 0x00);
      delay(1000);
  } else if (i == 2) {
      noteOn(0x90, noteCircus, 0x45);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);
      noteOn(0x90, noteCircus, 0x00);
      delay(1000);
  }
    } else {
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    }
  }
}

Not sure you have understood correctly, although you seem to have moved the onNote() function correctly.

In C/C++ you can't declare a function inside another function.

Normally in C/C++, and most other languages you must declare a function before you call/use it.