Code error

Please can somebody help me, I am doing a project where I am using an arduino Mega 2560 R3. Below is some of the code. I am getting endless errors for the case # line. PLease need urgent help
}
lcd.setCursor
case #: //validate password
checkPassword();
break;
;case '': // is to reset password attempt
password.reset();
// TODO: clear the screen output
break;
default:
password.append(eKey);
lcd.print("*");
}
}
}

Thanks in advance
Regards

case '#':

case without switch? Single quotes around '#'.

Why don't you post the errors and the rest of your code?

Here is full code:

/////////////////////////////////////////////////////////////////
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip


//Password
Password password = Password( "1234" );

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] = { // Define the Keymap
  {
    '1','2','3','A'      }
  ,
  {
    '4','5','6','B'      }
  ,
  {
    '7','8','9','C'      }
  ,
  {
    '*','0','#','D'      }
};

byte rowPins[ROWS] = {
  46, 47, 48, 49};     //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  50, 51, 52, 53};     //connect to the column pinouts of the keypad

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Assignign arduino pins to LCD display module

//Stroke LED Lights
int ledDelay = 50; // delay by 50ms
int redPin = 29;
int bluePin = 31;

//constants for LEDs, inputs and outputs
//int blueLED = 36;
int greenLED = 37;
int redLED = 38;
int pirPin1 = 39;
int pirPin2 = 34;

int reedPin1 = 41;
int reedPin2 = 42;
int speakerPin = 35; 

int alarmStatus = 0;
int zone = 0;
int alarmActive = 0;

void setup(){
  Serial.begin(9600);
  lcd.begin(20, 4);
  //Adding time 
  Wire.begin();
  

  displayCodeEntryScreen();

  //Police LED Lights
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  //setup and turn off both LEDs
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  
 
  pinMode(pirPin1, INPUT);  //Bedroom 2

  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, HIGH);
  digitalWrite(speakerPin, LOW);
 
}

void loop(){
  

  keypad.getKey();
  //Serial.println(digitalRead(reedPin2));
  //Serial.println(digitalRead(pirPin));
  //Serial.println(digitalRead(pirPin2));
  if (alarmActive == 1){ 
    if (digitalRead(pirPin1) == HIGH)
    {
      zone = 0;
      alarmTriggered();
    }
    if (digitalRead(reedPin1) == LOW)
    {
      zone = 1;
      alarmTriggered();
    }
    if (digitalRead(reedPin2) == LOW)
    {
      zone = 2;
      alarmTriggered();
    }
    
    if (digitalRead(pirPin2) == HIGH)
     {
     zone = 3;
     alarmTriggered();
     }
   }
}

/////////////////////////  Functions  /////////////////////////////////
//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
    if (passwd_pos - 15 >= 5) { 
      return ;
    }
    lcd.setCursor((passwd_pos++),0);
    switch (eKey){
    case '#':                 //# is to validate password 
      passwd_pos  = 15;
      checkPassword(); 
      break;
    case '*':                 //* is to reset password attempt
      password.reset(); 
      passwd_pos = 15;
   // TODO: clear the screen output 
      break;
    default: 
      password.append(eKey);
      lcd.print("*");
    }
  }
}

void alarmTriggered(){
  int expected_pos;
  int incr;
  digitalWrite(speakerPin, HIGH);
  digitalWrite(redPin, HIGH);
  
//
  password.reset();
  alarmStatus = 1;
  // alarmActive = 0;
  lcd.clear();
  lcd.setCursor(0,2);
  lcd.print("  SYSTEM TRIGGERED  ");
  lcd.setCursor(0,4);
  if (zone == 1)
  { 

  }
   if(zone == 0){
    expected_pos = 40;
    lcd.print("Motion in Passage");
    delay(1000);
  
 
   if (expected_pos > pos) {
     incr = 1;
   } else {
     incr = -1;
   }
   
  
  {
   StrokeLight();
  }
}

void StrokeLight(){                                                      //Stroke LED Lights
    digitalWrite(redPin, HIGH);        // turn the red light on
    delay(ledDelay); // wait 50 ms
    digitalWrite(redPin, LOW);         // turn the red light off
    delay(ledDelay); // wait 50 ms
    digitalWrite(redPin, HIGH);        // turn the red light on
    delay(ledDelay); // wait 50 ms
    digitalWrite(redPin, LOW);         // turn the red light off
    delay(ledDelay); // wait 50 ms
    digitalWrite(redPin, HIGH);        // turn the red light on
    delay(ledDelay); // wait 50 ms
    digitalWrite(redPin, LOW);         // turn the red light off
    delay(ledDelay); // wait 50 ms
    delay(10); // delay midpoint by 100ms
    digitalWrite(bluePin, HIGH);       // turn the blue light on
    delay(ledDelay); // wait 50 ms
    digitalWrite(bluePin, LOW);        // turn the blue light off
    delay(ledDelay); // wait 50 ms
    digitalWrite(bluePin, HIGH);       // turn the blue light on
    delay(ledDelay); // wait 50 ms
    digitalWrite(bluePin, LOW);        // turn the blue light off
    delay(ledDelay); // wait 50 ms
    digitalWrite(bluePin, HIGH);       // turn the blue light on
    delay(ledDelay); // wait 50 ms
    digitalWrite(bluePin, LOW);        // turn the blue light off
    delay(ledDelay); // wait 50 ms
    }                                                          

void checkPassword(){                  // To check if PIN is corrected, if not, retry!
  if (password.evaluate())
  {  
    if(alarmActive == 0 && alarmStatus == 0)
    {
      activate();
    } 
    else if( alarmActive == 1 || alarmStatus == 1) {
      deactivate();
    }
  } 
  else {
    invalidCode();
  }
}  

void invalidCode()    // display meaasge when a invalid is entered
{
  password.reset();
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("INVALID CODE! LOL!");
  lcd.setCursor(5,2);
  lcd.print("TRY AGAIN!");
  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, HIGH);
  delay(2000);
  digitalWrite(redLED, LOW);
  delay(1000);
  displayCodeEntryScreen();
}

void activate()      // Activate the system if correct PIN entered and display message on the screen
{
  if((digitalRead(reedPin1) == HIGH) && (digitalRead(reedPin2) == HIGH)){
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
    digitalWrite(2, HIGH);
    lcd.setCursor(0,0);
    lcd.print("SYSTEM ACTIVE!"); 
    alarmActive = 1;
    password.reset();
    delay(2000);
  }
  else{
    deactivate();   // if PIN not corrected, run "deactivate" loop
  }
}

void deactivate()
{
  //digitalWrite(camera, LOW);
  alarmStatus = 0;
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" SYSTEM DEACTIVATED!");
  digitalWrite(speakerPin, LOW);
  alarmActive = 0;
  password.reset();
  delay(5000);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);


  displayCodeEntryScreen();
}

void displayCodeEntryScreen()    // Dispalying start screen for users to enter PIN
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter PIN:");
  lcd.setCursor(0,2);
  lcd.print("Home Security System");
  lcd.setCursor(0,3);
  lcd.print("By Andrew OBrien. B4");
}

Moderator edit: CODE TAGS.

Here are the error messages
Arduino: 1.6.5 (Windows 7), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch_aug08a.ino: In function 'void keypadEvent(KeypadEvent)':
sketch_aug08a:139: error: expected ';' before 'break'
sketch_aug08a.ino: At global scope:
sketch_aug08a:145: error: expected declaration before '}' token
expected ';' before 'break'

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

Also, it is giving the error
"expected ';' before break/case. The issue is only in that small piece of code that I posted. Everything else works fine...

sketch_aug20a.ino: In function 'void keypadEvent(KeypadEvent)':
sketch_aug20a.ino:124:9: error: 'passwd_pos' was not declared in this scope
sketch_aug20a.ino:127:20: error: 'passwd_pos' was not declared in this scope
sketch_aug20a.ino: In function 'void alarmTriggered()':
sketch_aug20a.ino:169:23: error: 'pos' was not declared in this scope
sketch_aug20a.ino:181:19: error: a function-definition is not allowed here before '{' token
sketch_aug20a.ino:209:21: error: a function-definition is not allowed here before '{' token
sketch_aug20a.ino:287:1: error: expected '}' at end of input

Everything else works fine..

How can it "work"?
It doesn't even compile.

  switch (keypad.getState()){
  case PRESSED:

You've only got one case - you may have well just written if (keypad.getState() == PRESSED)

Hi
When I comment out those errors the rest of the code compiles fine. This is my first attempt at a more "complex" code as I have done all the examples and am trying to better my understanding of the coding.
If you could please post all your input or fixes I would really appreciate it as I need this to work urgently for a project.
Regards

If you indented your code properly, you'd probably see your problems.

Following AWOL's suggestion, note that before you post your code from the IDE, use Ctrl-T to reformat your code to a style that we find easier to read.

I've run auto format:
Here is the formatted code

/////////////////////////////////////////////////////////////////
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //Arduino Playground - HomePage

//Password
Password password = Password( "1234" );

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] = { // Define the Keymap
{
'1', '2', '3', 'A'
}
,
{
'4', '5', '6', 'B'
}
,
{
'7', '8', '9', 'C'
}
,
{
'*', '0', '#', 'D'
}
};

byte rowPins[ROWS] = {
46, 47, 48, 49
}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
50, 51, 52, 53
}; //connect to the column pinouts of the keypad

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Assignign arduino pins to LCD display module

//Stroke LED Lights
int ledDelay = 50; // delay by 50ms
int redPin = 29;
int bluePin = 31;

//constants for LEDs, inputs and outputs
//int blueLED = 36;
int greenLED = 37;
int redLED = 38;
int pirPin1 = 39;
int pirPin2 = 34;

int reedPin1 = 41;
int reedPin2 = 42;
int speakerPin = 35;

int alarmStatus = 0;
int zone = 0;
int alarmActive = 0;

void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
//Adding time
Wire.begin();

displayCodeEntryScreen();

//Police LED Lights
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);

//setup and turn off both LEDs
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(speakerPin, OUTPUT);

pinMode(pirPin1, INPUT); //Bedroom 2

digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(speakerPin, LOW);

}

void loop() {

keypad.getKey();
//Serial.println(digitalRead(reedPin2));
//Serial.println(digitalRead(pirPin));
//Serial.println(digitalRead(pirPin2));
if (alarmActive == 1) {
if (digitalRead(pirPin1) == HIGH)
{
zone = 0;
alarmTriggered();
}
if (digitalRead(reedPin1) == LOW)
{
zone = 1;
alarmTriggered();
}
if (digitalRead(reedPin2) == LOW)
{
zone = 2;
alarmTriggered();
}

if (digitalRead(pirPin2) == HIGH)
{
zone = 3;
alarmTriggered();
}
}
}

///////////////////////// Functions /////////////////////////////////
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
return ;
}
lcd.setCursor
void checkPassword() {
checkPassword();
break;
; case '': // is to reset password attempt
password.reset();
// TODO: clear the screen output
break;
default:
password.append(eKey);
lcd.print("*");
}
}
}

void alarmTriggered() {
int expected_pos;
int incr;
digitalWrite(speakerPin, HIGH);
digitalWrite(redPin, HIGH);

//
password.reset();
alarmStatus = 1;
// alarmActive = 0;
lcd.clear();
lcd.setCursor(0, 2);
lcd.print(" SYSTEM TRIGGERED ");
lcd.setCursor(0, 4);
if (zone == 1)
{

}
if (zone == 0) {
expected_pos = 40;
lcd.print("Motion in Passage");
delay(1000);

if (expected_pos > pos) {
incr = 1;
} else {
incr = -1;
}

{
StrokeLight();
}
}

void StrokeLight() { //Stroke LED Lights
digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, LOW); // turn the red light off

}

void checkPassword() { // To check if PIN is corrected, if not, retry!
if (password.evaluate())
{
if (alarmActive == 0 && alarmStatus == 0)
{
activate();
}
else if ( alarmActive == 1 || alarmStatus == 1) {
deactivate();
}
}
else {
invalidCode();
}
}

void invalidCode() // display meaasge when a invalid is entered
{
password.reset();
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("INVALID CODE! LOL!");
lcd.setCursor(5, 2);
lcd.print("TRY AGAIN!");
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
delay(2000);
digitalWrite(redLED, LOW);
delay(1000);
displayCodeEntryScreen();
}

void activate() // Activate the system if correct PIN entered and display message on the screen
{
if ((digitalRead(reedPin1) == HIGH) && (digitalRead(reedPin2) == HIGH)) {
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
digitalWrite(2, HIGH);
lcd.setCursor(0, 0);
lcd.print("SYSTEM ACTIVE!");
alarmActive = 1;
password.reset();
delay(2000);
}
else {
deactivate(); // if PIN not corrected, run "deactivate" loop
}
}

void deactivate()
{
//digitalWrite(camera, LOW);
alarmStatus = 0;
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" SYSTEM DEACTIVATED!");
digitalWrite(speakerPin, LOW);
alarmActive = 0;
password.reset();
delay(5000);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);

displayCodeEntryScreen();
}

void displayCodeEntryScreen() // Dispalying start screen for users to enter PIN
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter PIN:");
lcd.setCursor(0, 2);
lcd.print("Home Security System");
lcd.setCursor(0, 3);
lcd.print("By Andrew O'Brien, B4");
}

Please can someone rewrite this piece of code or tell me what to do as I don't totally understand Delta's answer. I keep getting endless error messages for this one piece of code. When it is commented out everything else compiles fine.

void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
return ;
}
lcd.setCursor
void checkPassword() {
checkPassword();
break;
; case '': // is to reset password attempt
password.reset();
// TODO: clear the screen output
break;
default:
password.append(eKey);
lcd.print("*");
}
}
}

Ive rewritten it like this as this is how how understood Delta's answer:
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState())
case PRESSED:
return ;

lcd.setCursor
checkPassword()
break;
; case '': // is to reset password attempt
password.reset();
// TODO: clear the screen output
break;
default:
password.append(eKey);
lcd.print("*");

}

That fixes the one error but gives me an error message for the line "lcd.setCursor" to do with a ';'.
Please help this is really urgent

I also get the error message
sketch_aug14a:143: error: expected declaration before '}' token
for the this section of code

void alarmTriggered() {
int expected_pos;
int incr;
digitalWrite(speakerPin, HIGH);
digitalWrite(redPin, HIGH);
}

Thanks in advance

  lcd.setCursor

What's that ?
Why is it on its own?

The code I partially copied and partially wrote myself. The 20 or so lines giving me problems is the bit I borrowed from another project. This is the bit that doesn't agree with me:

void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState())
case PRESSED:
return ;

checkPassword();
break;
; case '': // is to reset password attempt
password.reset();
// TODO: clear the screen output
break
default:
password.append(eKey);
lcd.print("*");

}

void alarmTriggered() {
int expected_pos;
int incr;
digitalWrite(speakerPin, HIGH);
digitalWrite(redPin, HIGH);
}
//
password.reset();
alarmStatus = 1;
// alarmActive = 0;
lcd.clear();
lcd.setCursor(0, 2);
lcd.print(" SYSTEM TRIGGERED ");
lcd.setCursor(0, 4);
if (zone == 1)
{

}
if (zone == 0) {
expected_pos = 40;
lcd.print("Motion in Passage");
delay(1000);

if (expected_pos > pos) {
incr = 1;
} else {
incr = -1;

That is is the part that I didn't know how to code so copied from another project because in theory it should work. I know there are errors in the code but I do not know how to fix them and even some of their functions as I didn't write this piece of code. If you had time I would really appreciate it if you could point me in the right direction or rewrite it instead of telling me where the problems are as I can't fix those problems.
Regards

Delta, as I have said I copied this small section (around 20 lines) of code from another project. I know Python, Delphi and Java and basic C and C++. I am currently learning the C and C++ language. This is really urgent as I need it for something for next week. I will have time later to go back and become fluent in C++ but at the moment I really just need the fix in the Syntax. I have done the Arduino examples and tutorials but am still relatively new to the Arduino scene. PLease if you could just give me a pointer instead of pointing out the errors that I don't know how to fix as that just doesn't help me. I appreciate all your input...
Regards

I have posted my full code already but have added it as an attachment. The idea is that if somebody breaks a IR sensor then an alarm sounds. The alarm is deactivated by a keypad and all the details i.e. enter password or alarm status are displayed on an LCD screen. I am using an Arduino Mega 2560. You also activate the alarm from the keypad. The alarm status is displayed on the LCD aswell as with a few LEDs.
That is the purpose of the code

sketch_aug14a.ino (5.61 KB)

void alarmTriggered() {
  int expected_pos;
  int incr;
  digitalWrite(speakerPin, HIGH);
  digitalWrite(redPin, HIGH);
} // function ends here
//
password.reset();// so this code isn't in a function. That's not allowed

The syntax is the same as Java and C in this respect.

@AWOL
Thanks that did fix that problem.

theSAdunion:
Delta, as I have said I copied this small section (around 20 lines) of code from another project. I know Python, Delphi and Java and basic C and C++.

And how many times have you just copy-and-pasted a chunk of java into a random spot into a .java file and it compiled and worked?

theSAdunion:
This is really urgent as I need it for something for next week. I will have time later to go back and become fluent in C++ but at the moment I really just need the fix in the Syntax.

"I really need to have my house complete next week, but I am pouring the concrete for the foundation and it's not working! Help!"

Really, the only answer is: "dude, if you need to have your house done by next week, I can tell by the way you are pouring that concrete that you are hosed."

void keypadEvent(KeypadEvent eKey) {
  switch (keypad.getState())
  case PRESSED:
  return ;

  lcd.setCursor;
  checkPassword()
  break;
; case '*':                //* is to reset password attempt
  password.reset();
  // TODO: clear the screen output
  break;
default:
  password.append(eKey);
  lcd.print("*");

}

Jesus, what a mess. Straight away I know that you are not a java programmer because the java syntax for switch is identical to the C syntax, and no java programmer would screw it up quite this badly. There is simply no doubt that when you claim to "know" Java you are bullshitting, and that makes anything else you say suspect. I have no idea why you would be doing this. Go watch some old episodes of "House" to find out why telling fibs to your doctor is dumb.

1 - A switch statement has its body enclosed in braces. This is so that the compiler knows where it stars and ends. We can fix this by putting { after switch (keypad.getState())

2 - the case: PRESSED branch has an unconditional return statement, followed by lcd.setCursor(). This means that that line will never under any circumstanced get executed. Why is it there? What is its purpose? What is it supposed to do? I don't know. And you probably don't know either. You just pasted it in in hopes that it would do something. See VoodooChickenCoding@PrtlandPatternRepository .

3 - You don't have a semicolon after lcd.setCursor() . Again, if you had any skill whatever in Java, you would have at least fixed this before coming here and being all like "the compiler doesn't understand my code and I don't know why!"

4 - 'PRESSED' and '' are different things - one is the key state, one is the particular key that was pressed. Checking the keypad state against '' will accompish nothing: you need to call getKey().

You say that you just need the syntax fixed. You are gravely mistaken - the syntax errors are the least of what is wrong with this sketch. A programmer's task is to bridge "what needs to be done" to "how to go about doing it". I really don'y see that you have much of a handle on this. I suspect you think that if you can get the syntax fixed, you can then make small changes at random until the arduino does the job you want. This is not a development methodology that is going to work well.

And it's not just this little chunk of code. What the hell is this???

void alarmTriggered() {
  int expected_pos;
  int incr;
  digitalWrite(speakerPin, HIGH);
  digitalWrite(redPin, HIGH);
}

What do expected_pos and incr do? Nothing! Why are they there? I don't know.

Also, I loathe seeing "x == 0" to test for a boolean false in an integer variable, although that's a style thing.

I'm sorry to be harshing your trip, man, but you need to start small and grow into the full functionality. You cannot drop chunks of other people's code into your sketch and expect it to work (except as libraries that you know how to use correctly). If it absolutely has to be done by next week, then either prepare to fail, or engage in outright academic fraud and pay someone else to do your assignment for you.