// initialize switch pin (2) as an input: pinMode(SwitchPin, INPUT);digitalWrite (SwitchPin, HIGH); // enable internal pullup resistor - open switch will now go high
Why is the relay connected to analog pin 12? Analog pins are input only. You can't perform a digitalRead on an analog pin.
// Constants:const int SwitchPin = A2; // the pin that the switch is attached toconst int RelayPin = A12; // the pin that the relay is attached to
Aren't the analog pins something like D54 & up?So he can read them, they just need to be referenced correctly.http://arduino.cc/en/Hacking/PinMapping2560
PreviousSwitchState is never updated.