IF digitalRead syntax problems

Hello All,

I am chasing my tail with the digitalRead vs the IF/Else control statements. I am using a Sunfounder Mega 2560 with a Sainsmart LCD2004A. You will notice the error message I pasted at the bottom.

I place the Swtichpin definition in various places and I get the same type of errors. I feel like I am looking right at something simple. There is extraneous code from a Ping program I sourced the LCD code from but I don’t call it so it just remains extraneous.

#include <LiquidCrystal_I2C.h>
#include<LCD.h>
#include<Wire.h>
#include <NewPing.h>

#define I2C_ADDR 0x27
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define ECHO_pin 11
#define TRIGGER_PIN 12
#define MAX_DISTANCE 500

NewPing sonar(TRIGGER_PIN,ECHO_pin, MAX_DISTANCE);
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup() {
int SwitchPin = 22;
pinMode(SwitchPin,INPUT);
lcd.begin (20,4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);

}
void loop() { 

if (digitalRead(SwitchPin) == HIGH)
 { lcd.setBacklight(HIGH);
lcd.home();
lcd.print("Hello Family");
lcd.setCursor (0, 1);
lcd.print("The switch is 33");
 }
 else 
  { lcd.setBacklight(HIGH);
lcd.home();
lcd.print("Hello Family");
lcd.setCursor (0, 1);
lcd.print("The switch is off");}
  // put your main code here, to run repeatedly:
 }

*ERROR MESSAGE

Arduino: 1.6.6 (Windows 7), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

C:\Users\Robot\Documents\Arduino\I2C_ld_1may16\I2C_ld_1may16.ino: In function ‘void loop()’:

I2C_ld_1may16:32: error: ‘SwitchPin’ was not declared in this scope

if (digitalRead(SwitchPin) == HIGH)

^

exit status 1
‘SwitchPin’ was not declared in this scope

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

At the top of this Forum are several posts by Nick Gammon. One, titled Useful Links, has a subtopic on Scope that will answer your question posed here. You should also read the second post by Nick, as that will help you with future posts. Finally, with the cursor in the source code window of the IDE, use Ctrl-T to reformat your code into a more standard (i.e., easier-to-read) style.

(deleted)

Thank you!,

I thought I had done a better read on the precursor posts by Nick. I apologize for what is glaringly obvious resource miss on my part.

Thanks again!

Rob

PS. Now that Switchpin is global…viola it compiles :slight_smile: