'PanLeft' was not declared in this scope

I am trying to add functions in my code but get the error:

‘PanLeft’ was not declared in this scope

Would appreciate being told what I am doing wrong, thanks

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int MyArray[3] = {1001, 1002, 1003};
int ledPin = 13;

void setup() {
  mySerial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {

  if (mySerial.available() > 1) {
    int input = mySerial.parseInt();

    // Button 1 pressed
    if (input == 1001) {
      // Function Call
      PanLeft();
    }

    // Buttons depressed
    if (input == 1002) {
      // Function Call
    }

    // Button 2 pressed
    if (input == 1004) {
      // Function Call
    }

    
PanLeft() {
  
}
  
  }
  mySerial.flush();//clear the serial buffer for unwanted inputs

  delay(20);//delay little for better serial communication

}

It looks like you're trying to define PanLeft() inside the loop() function. You can't do that because function definitions have to be made outside other functions.

Try putting your PanLeft definition after the final } of loop().

See comments in code

PanLeft() {  // There is no return type.  Must be void if not another type (void PanLeft())
 
} // extra bracket
 
  }  // extra bracket  These brackets belong at the end of loop().
  mySerial.flush();//clear the serial buffer for unwanted inputs

  delay(20);//delay little for better serial communication

}

If you put your curly brackets each on their own line bracket mismatches a misplaces will be easier to spot.

void PanLeft() 
{
  mySerial.flush();//clear the serial buffer for unwanted inputs

  delay(20);//delay little for better serial communication

}

If you use autoformat (crtl-t or Tools. Autoformat) on your code things like bracket mismatches will show up.

This compiles:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int MyArray[3] = { 1001, 1002, 1003 };
int ledPin = 13;

void setup()
{
  mySerial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{

  if (mySerial.available() > 1)
  {
    int input = mySerial.parseInt();

    // Button 1 pressed
    if (input == 1001)
    {
      // Function Call
      PanLeft();
    }

    // Buttons depressed
    if (input == 1002)
    {
      // Function Call
    }

    // Button 2 pressed
    if (input == 1004)
    {
      // Function Call
    }
  }
}

void PanLeft() 
{
  mySerial.flush();//clear the serial buffer for unwanted inputs
  delay(20);//delay little for better serial communication
}

Many thanks both.

Groundfungus,

I have taken out the lines where :

mySerial.flush() … However have I moved it to the correct part to get used?

Also I had used the auto format for my code, however I had to reduce the indent, probably because I had pasted parts in. Thanks

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int MyArray[3] = { 1001, 1002, 1003 };
int ledPin = 13;

void setup()
{
  mySerial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{

  if (mySerial.available() > 1)
  {
    int input = mySerial.parseInt();

    // Button 1 pressed
    if (input == 1001)
    {
      // Function Call
      PanLeft();
    }

    // Buttons depressed
    if (input == 1002)
    {
      // Function Call
    }

    // Button 2 pressed
    if (input == 1004)
    {
      // Function Call
    }
  }
  
  mySerial.flush();//clear the serial buffer for unwanted inputs
  delay(20);//delay little for better serial communication

}

  
void PanLeft()
{
 
}

mySerial.flush();//clear the serial buffer for unwanted inputs

Nope, flush waits (blocks) until the transmit buffer is empty, period. [Serial Flush](http://Serial Flush)Serial Flush Really kind of useless. If you want to empty the receive buffer just read bytes until the buffer is empty (available() == 0).

If you want to know how to do serial comms the right way (no blocking parseInt) serial input basics

Many thanks. What if I put that line/s of code in every function before the code of the function or make it into a function before going to the directed one.? Always a complication somewhere.

I am only using the functions as a clear dedicated space rather than in confined If/Else structure. What happens if I don't clear the buffer, is it all going to get corruped?

What happens if I don't clear the buffer, is it all going to get corruped?

Did you note what was said about the flush() function ? It waits until the transmit buffer is empty. As you are not putting anything in that buffer then using it achieves nothing.

If you don't empty the input buffer then the next time you read from serial you will get whatever is in there.

I am intrigued as to where you are reading the serial input from such that you receive say 1001 by pressing button 1

What if I put that line/s of code in every function

Not sure what you mean there. Do you mean a function to clear the receive buffer? If you use the methods in Robin2's serial basics post there will be no need to clear out the receive buffer as all bytes are read, then parsed (if necessary).

Edit: I don't understand the logic there, either. But the op hasn't asked so....

Thanks.

Ref 1001, I have not tried that yet, I have been sending 1111,2222,3333, etc but will be going passed 9999 so I thought I would use new sequence. Is the use of 1001, 1002 doomed?

The unit receives data to control 2 stepper motors, Pan & Tilt plus other controls.

It is a remote receiver. A button is held down for a period to move a motor. So I could clear the buffer when transmission has stopped or does it not work that way? If it's a problem I will get rid of functions and just go back as I was which worked although is crammed up to read.

I looked at Robins page thanks, but as a new person to Arduino it would be another learning curve for another time.

What exactly do you have attached to the SoftwareSerial input ?

Thanks, it's a HC-12 transeiver. 433mhz

As long as you use

 if (mySerial.available() > 1) {

there could be 1 byte leftover that would contaminate your next read so clearing the receive buffer might be necessary.

I really recommend that you read through Robin2's post. Even if you don't understand the code it will give insight on how robust non-blocking serial communication works.

Do you have control of what the transmitter sends?

If it's a problem I will get rid of functions and just go back as I was which worked although is crammed up to read.

Don't do that. Your sketch should mainly be functions.

Thanks, and yes I have control of what is sent.

My breadboard tests illiminated LED's without showing any errors, so I can fall back on just using my If/Else code.

As far as I thought the code eventually goes throgh buffer cleaning after each code was received. I can't see why it cannot be used at the right place before the void loop returns it back to be read. Or I clear data after every function is run.

Also is there a problem transmitting 1001 instead of 1111?

Regards

    // Button 1 pressedDo these comments referring to Buttons have any relevance to what you are doing ?

int MyArray[3] = { 1001, 1002, 1003 };Are you intending to use this array at some point ?

Thanks, yes the button press positions are just markers for me to put code in. The code I put up was only to ask in what place and how functions would be put in.

Having found that out has just left me trying to get advice where just 1 line could be placed putting my serial buffer back in use. I will try putting ii after every function before it returns. In addition the subject of using control code blocks 1001, 1002 got queried. Will it not work or is it bad practice? I was using 1000, 2000 etc without problems.

Thanks

In addition the subject of using control code blocks 1001, 1002 got queried

You can use any number that fits in an Arduino int

If you want to empty the Arduino receive buffer then create a function that reads from Serial as long as data is available then exits. Call the function any time that you need it.

the button press positions are just markers for me to put code in

I am still struggling to understand the relationship between buttons and reading an int from Serial.

Thanks. There are no buttons, It was used just as though I were pressing buttons to make the motors pan left,right,tilt up,down or orher commands.

I think your idea is best for clearing the buffer, in making it a function that can be called any time. So pleased the functions are there being used to GoSubs.

Thanks again.

So pleased the functions are there being used to GoSubs.

The functions are being used as functions. Unlike a GOSUB in BASIC a function can return a value and be given meaningful names.

Thanks. Maybe I should have described it more in the instance I am using it, go off to a clean space and do something specific for me, come back when you've done it. Regards