Here is my code. It will run like it should until the one second delay is done then pin 12 just pulses every one second. any help on resetting the program to start again when the button is pushed?
// Constants:
const int SwitchPin = 4; // the pin that the switch is attached to
const int RelayPin = 13; // the pin that the relay is attached to
const int TxPin = 6;
const int Relay2 = 12;
// Variables:
int SwitchState = 0; // current state of the switch
int PreviousSwitchState = 0; // previous state of the switch
int RelayState = 0; // current state of relay pin
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
void setup() {
// initialise switch pin (32) as an input:
pinMode(SwitchPin, INPUT);
pinMode(Relay2, OUTPUT);
pinMode(TxPin, OUTPUT);
digitalWrite(TxPin, HIGH);
// enable internal pullup resistor - open switch will now go high
//digitalWrite (SwitchPin, HIGH);
// initialise relay pin (42) as an output:
pinMode(RelayPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the switch pin (32):
SwitchState = digitalRead(SwitchPin);
// read the relay pin (42):
RelayState = digitalRead(RelayPin);
// compare the SwitchState to its previous state
if (SwitchState != PreviousSwitchState) {
// if the state has changed, check change direction
if (SwitchState == HIGH){
// if the current state is HIGH then the switch went from off to on
// instigate a relay state change:
if (RelayState == LOW)
digitalWrite(RelayPin, HIGH);
delay(1000);
else
delay(1000);
digitalWrite(RelayPin, LOW);
}