Help with repairing this sketch

I have two sensors one is inputted at A0 and the other is at A1. When I just use one sensor the code works just fine. when I try using both of them it gives me the Button A was not declared in this scope.
what do I have to put in the sketch do make it work.
Thanks,
Bob

#include <SoftwareSerial.h>
#define rxPin 50 // Serial input (connects to Emic 2 SOUT)
#define txPin 52 // Serial output (connects to Emic 2 SIN)
// set up a new serial port
SoftwareSerial emicSerial = SoftwareSerial(rxPin, txPin);
const int analogPin1 = A0; // pin that the motion sensor is attached to
const int analogPin2 = A1; // pin that the remolt sensor is attached to
const int ledPin = 24; // pin that the LED is attached to
const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input

void setup() {
// initialize the LED pin as an output:
pinMode(24, OUTPUT);
// initialize serial communications:
Serial.begin(9600);

// define pin modes

pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);

emicSerial.begin(9600);

emicSerial.print('\n'); // Send a CR in case the system is already up
while (emicSerial.read() != ':'); // When the Emic 2 has initialized and is ready, it will send a single ':' character, so wait here until we receive it
delay(10); // Short delay
emicSerial.flush(); // Flush the receive buffer
}
void loop() {
// read the value of the potentiometer1:
int analogValue = analogRead(analogPin1);

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
Button A();
delay(10);
read the value of the potentiometer2:
int analogValue = analogRead(analogPin2);
if (analogValue > threshold) {
ButtonA();

} else {
digitalWrite(24, LOW);
}

// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
{
}

}
void ButtonA() {
digitalWrite(24, HIGH);
delay(1900);
digitalWrite(24, LOW);
emicSerial.print('S');
emicSerial.print("[ AY<400,25> RAO<200,28> K<100> IH<100,27> N<50> DHAX<150> TRIY<150,25> TAO<200,27> P<100> AO<100,28> LX<50> DHAX<150> DEY<300,27> LX<75> uh<400,25>[n1]");
emicSerial.print('\n');

while (emicSerial.read() != ':'); // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
//while (1) // Demonstration complete!
{

}

}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

You're missing a closing brace at the end of your loop function. This sort of bug can be detected easily by doing a Tools > Auto Format and checking to see if the indentation is as expected. If you look at your code you'll notice that ButtonA() is indented, which it should not be. This indicates that the ButtonA() function is actually inside of another function, which is not allowed.

usabob01:
I do use auto Format and it moves them to the right just like the are.

I saw that and I'm glad you're already using Tools > Auto Format. I was trying to explain to you that it's useful to help with debugging as well as making your code more readable. You have to actually look at the code after the Auto Format to see if the automatic indentation is as expected, which would have given you a helpful clue about the cause of your error.

usabob01:
When I pasted my code I do not see the same box you are suppose to use. I am new to this and trying to figure all this out.quote]

There's a button on the toolbar that looks like this: </>. Click that button, then paste your code, then write any non-code part of your post outside of the code tags that were added. If you did it correctly your post would look like this:

I have two sensors one is inputted at A0 and the other is at A1. When I just use one sensor the code works just fine. when I try using both of them it gives me the Button A was not declared in this scope.
what do I have to put in the sketch do make it work.
Thanks,
Bob

#include <SoftwareSerial.h>
#define rxPin 50    // Serial input (connects to Emic 2 SOUT)
#define txPin 52     // Serial output (connects to Emic 2 SIN)
// set up a new serial port
SoftwareSerial emicSerial =  SoftwareSerial(rxPin, txPin);
const int analogPin1 = A0;    // pin that the motion sensor is attached to
const int analogPin2 = A1;    // pin that the remolt sensor is attached to
const int ledPin = 24;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the LED pin as an output:
  pinMode(24, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);


  // define pin modes

  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  emicSerial.begin(9600);


  emicSerial.print('\n');             // Send a CR in case the system is already up
  while (emicSerial.read() != ':');   // When the Emic 2 has initialized and is ready, it will send a single ':' character, so wait here until we receive it
  delay(10);                          // Short delay
  emicSerial.flush();                 // Flush the receive buffer
}
void loop() {
  // read the value of the potentiometer1:
  int analogValue = analogRead(analogPin1);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    Button A();
    delay(10);
read the value of the potentiometer2:
    int analogValue = analogRead(analogPin2);
    if (analogValue > threshold) {
      ButtonA();

    } else {
      digitalWrite(24, LOW);
    }

    // print the analog value:
    Serial.println(analogValue);
    delay(1);        // delay in between reads for stability
    {
    }

  }
  void ButtonA() {
    digitalWrite(24, HIGH);
    delay(1900);
    digitalWrite(24, LOW);
    emicSerial.print('S');
    emicSerial.print("[ AY<400,25> RAO<200,28> K<100> IH<100,27> N<50> DHAX<150> TRIY<150,25> TAO<200,27> P<100> AO<100,28> LX<50> DHAX<150> DEY<300,27> LX<75> uh<400,25>[n1]");
    emicSerial.print('\n');

    while (emicSerial.read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
    //while (1)     // Demonstration complete!
    {

    }

  }

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What Arduino are you using?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What Arduino are you using?

Thanks.. Tom.. :slight_smile:

[/I am using the mega 2560. No matter where I move things around I am still getting the button A was not declared in this scopequote]

Hi,
Button A is not the same as ButtonA

Tom.. :slight_smile:

You have:

    Button A();

and

    ButtonA();

Hi,
Also a } missing as well as // thats missing on a comment line.

Tom... :slight_smile:

aarg:
You have:

    Button A();

and

    ButtonA();

[I did not even see that! Just like it said I did not declare it. I don't know what I would do if it wasn't for smart guys like you all. These {{ are giving me fits. What do you mean } on comment line? Thanks Bob /quote]

usabob01:

aarg:
You have:

    Button A();

and

    ButtonA();

[I did not even see that! Just like it said I did not declare it. I don't know what I would do if it wasn't for smart guys like you all. These {{ are giving me fits. What do you mean } on comment line? Thanks Bob /qI still get the not declared even after repairing the ButtonA and Button A uote]

still get the not declared even after repairing the ButtonA and Button A

Can you please not put your reply inside quotes tags.

It is still not working because you have still not repaired it correctly.

Please post the code ( using code tags the </> icon ) as you have it now, and we can tell you where you are going wrong.

Note https://www.arduino.cc/en/serial/flush will tell you what this command does, it is not doing what you think.

Hi,
Read post #7, you have a } missing and // missing.

Look at your original two codes and check the } and //.

Tom... :slight_smile:

I'm new at this and am having a problem With this sketch. It gives me (Button A was not declared in this scope. I'm having a struggle with learning how to use the brackets and think that is part of  the problem.
Maybe someone can show me exactly where the problem is.
                                      New at this,              
                                                        Bob  

include <SoftwareSerial.h>
#define rxPin 50    // Serial input (connects to Emic 2 SOUT)
#define txPin 52     // Serial output (connects to Emic 2 SIN)
// set up a new serial port
SoftwareSerial emicSerial =  SoftwareSerial(rxPin, txPin);
const int analogPin1 = A0;    // pin that the motion sensor is attached to
const int analogPin2 = A1;    // pin that the remolt sensor is attached to
const int ledPin = 24;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {


  pinMode(24, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);


  // define pin modes

  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  emicSerial.begin(9600);


  emicSerial.print('\n');             // Send a CR in case the system is already up
  while (emicSerial.read() != ':');   // When the Emic 2 has initialized and is ready, it will send a single ':' character, so wait here until we receive it
  delay(10);                          // Short delay
  // emicSerial.flush();                 // Flush the receive buffer
}
void loop() {
  //     read the value of the potentiometer1:
  int analogValue = analogRead(analogPin1);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold)
    Button A();
  delay(10);
  //read the value of the potentiometer2:
  int analogValue = analogRead(analogPin2);
  if (analogValue > threshold)
    Button A();
}
} else {
  digitalWrite(24, LOW);
}

// print the analog value:
Serial.println(analogValue);
delay(1);        // delay in between reads for stability
{
}

}
void Button A() {
  digitalWrite(24, HIGH);
  delay(1900);
  digitalWrite(24, LOW);
  emicSerial.print('S');
  emicSerial.print("[ AY<400,25> RAO<200,28> K<100> IH<100,27> N<50> DHAX<150> TRIY<150,25> TAO<200,27> P<100> AO<100,28> LX<50> DHAX<150> DEY<300,27> LX<75> uh<400,25>[n1]");
  emicSerial.print('\n');

  while (emicSerial.read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
  //while (1)     // Demonstration complete!
  {

  }

}]

I haven't checked everything about your sketch but a couple of things stand out.

  1. You can't have space in a function or variable name. So change Button A() to buttonA() or something similar.
  2. I see some high pin numbers- are you using a Mega? If so, it's got lots of hardware serial ports so you shouldn't need to use the far-inferior SoftwareSerial library.

Check the curly brackets of you loop() function.

In fact, check all the curly bracket pairs in your program. Many of them are wrong, and the square bracket on the end of the code should not be there, nor the one a few lines above inside the print statement.

Repaired all your suggestions and it cleared my faults. Thanks for all your help from the beginner. 



#include <SoftwareSerial.h>
#define rxPin 50    // Serial input (connects to Emic 2 SOUT)
#define txPin 52     // Serial output (connects to Emic 2 SIN)
// set up a new serial port
SoftwareSerial emicSerial =  SoftwareSerial(rxPin, txPin);
const int analogPin1 = A0;    // pin that the motion sensor is attached to
const int analogPin2 = A1;    // pin that the remolt sensor is attached to
const int ledPin = 24;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {


  pinMode(24, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);


  // define pin modes

  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  emicSerial.begin(9600);


  emicSerial.print('\n');             // Send a CR in case the system is already up
  while (emicSerial.read() != ':');   // When the Emic 2 has initialized and is ready, it will send a single ':' character, so wait here until we receive it
  delay(10);                          // Short delay
  // emicSerial.flush();                 // Flush the receive buffer
}
void loop() {
  //     read the value of the potentiometer1:
  int analogValue = analogRead(analogPin1);
  if (analogValue > threshold) {
    buttonA();
  } else {
    //read the value of the potentiometer2:
    int analogValue = analogRead(analogPin2);
    if (analogValue > threshold)
      buttonA();
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability

}

void buttonA() {
  digitalWrite(24, HIGH);
  delay(1900);
  digitalWrite(24, LOW);
  emicSerial.print('S');
 // emicSerial.print("[ AY<400,25> RAO<200,28> K<100> IH<100,27> N<50> DHAX<150> TRIY<150,25> TAO<200,27> P<100> AO<100,28> LX<50> DHAX<150> DEY<300,27> LX<75> uh<400,25> NX]");
  emicSerial.print("   [ HXIY<150,25> RAO<200,28> KS<100> IH<100,27> N<50> DHAX<150> TRIY<150,25> TAO<200,27> P<100> AO<100,28> LX<50> DHAX<150> DEY<300,27> LX<75> uh<400,25> NX]");
  emicSerial.print("    [ HXAO<150,28> PIH<100,26> N<50> AE<75,27> N<50> D<25> AX<150> BAO<150,25> PIH<100> N<50> AE<75,22> N<50> D<25> AX<150> SIH<100,20> NX<50> IH<100,18> N<50> HXIH<200> Z<100> SAO<400> NX]");
  emicSerial.print("   [  AO<100,28> LX DHAX<150,175> Lx<150,27>  TEL<150> BRR<300,25> D Z<150> AO<100,27> N<50> JHEY<300,28> BRR<350,27> D<50> STRIY<500,25>T RAO<150,25> BIH<100> N<50> GOW<300,22> TWIY<200,20> T<100> TWIY<200,18> T<100> TWIY<200,18> TWIY<400,18>]");
  emicSerial.print(" [   AO<1150,200> GUH<150,23> NAX<150,25> RAO<200,21> K<100> TAH<150,18> NAY<650> T<700> GUH<150,23> NAX<150,25> RAO<200,21> K<100> TAH<150,18> NAY<650,30> T<100>[n1]");

  emicSerial.print('\n');

  while (emicSerial.read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
  //while (1)     // Demonstration complete!


}

You're really failing terribly at using forum markup. I can understand screwing up on the first post but when you do it again and again every single time it's unacceptable. After writing your post click the "Preview" button, take some time to read it over to make sure the formatting is correct. If you need to make any changes then preview it again. Only after it is perfect should you click the "Post" button.

To succeed with programming and electronics you need to be very precise. It's best to take the same approach when asking for help here on the forum.