Help with code using relay shield to power on/off my arcade cabinet.

I seem to have it working...although my code isn't to nice.
Was really thinking and trying to incorporate some kind of counting system each time the main momentary switch is pressed.
I noticed sometimes it likes to skip the shut down part of the code and go to the power on section.

Old sketch..NOT using:

// Turning on electrical systems in my Arcade Cabinet with Seeed relay module.
// When powering up, this is the order of the relays:
// AC power remote relay, TV remote relay and finally the computer relay.
// When shutting down, it's reversed:
// Computer relay (10 sec. for complete shutdown). 
// Then AC power remote relay.
// The arduino in this setup will be powered by a 9V battery.
// 12Sep14

int switchState = LOW;
const int relayPin1 = 7;    //Digital pin to control the relay for the AC power (remote AC outlet) 'relay #1'.
const int relayPin2 = 6;    //Digital pin to control the relay for the TV (remote) 'relay #2'.
const int relayPin3 = 5;    //Digital pin to control the relay for the Computer  'relay #3'.
const int mainSwitch = 9;   //Digital pin input to control relays 1-3.
long previousMillis = 0;        // will store last time switch was pressed
long countPresses = 0;           // interval (milliseconds)



void setup()
{
  Serial.begin(9600);
  pinMode(mainSwitch, INPUT);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  digitalWrite(mainSwitch, HIGH);
}



void loop()
{

  if(digitalRead(mainSwitch) == LOW)  // main button is pressed
  {

    delay(3000);   //Wait a few seconds after pushing the main Cabinet switch.
    //First turn on electrical system (Will remain on until the end or no more movement).
    digitalWrite(relayPin1,HIGH);
    delay(300);
    //Turn off relay for AC power (remote cntrl.).
    digitalWrite(relayPin1,LOW);
    delay(5000);
    //Turn on relay for TV (remote cntrl.).
    digitalWrite(relayPin2,HIGH);  //Turns on TV.
    delay(300);
    //Turn off relay for TV.
    digitalWrite(relayPin2,LOW);
    delay(3000);
    //Turn on relay for PC.
    digitalWrite(relayPin3,HIGH);  //Turns on PC.
    delay(300);
    //Turn off relay for PC.
    digitalWrite(relayPin3,LOW);

    delay(10000);
  }



  // Now Shutting down the entire Arcade Cabinet.
  // Relays are reversed and we only need PC relay and then the AC power relay.


  if(digitalRead(mainSwitch) == LOW)  // main button is pressed.
  {
    digitalWrite(relayPin3,HIGH);  
    delay(300);

    //Turn off relay for PC.
    digitalWrite(relayPin3,LOW);
    delay(10000);

    digitalWrite(relayPin1,HIGH);
    delay(300);
    //Turn off relay for Elect Sys.
    digitalWrite(relayPin1,LOW);
    delay(5000);
  }



  countPresses + 2;

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > countPresses) 
  {
    // save the last time you pressed the switch
    previousMillis = currentMillis;  
  }

}

Just a pic of the cab:

  if(digitalRead(mainSwitch) == LOW)  // main button is pressed.You are sensing that the button is currently pressed rather than when it becomes pressed. You need to sense the change from on to off, ie the change of state not the current state.

Look at the StateChangeDetection example in the IDE to see how to do it.

UKHeliBob:
  if(digitalRead(mainSwitch) == LOW)  // main button is pressed.You are sensing that the button is currently pressed rather than when it becomes pressed. You need to sense the change from on to off, ie the change of state not the current state.

Look at the StateChangeDetection example in the IDE to see how to do it.

Thanks for the reply and help,

Looking at the state change detection sketch:

created  27 Sep 2005
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.
   
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

 
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}

Not sure if I follow this part here:

// turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }

I kind of understand about % (Modulo). From the reference:
"Calculates the remainder when one integer is divided by another. It is useful for keeping a variable within a particular range (e.g. the size of an array). "

In my sketch I have one input (my main switch, which controls 3 relays) in two different ways (powering ON and powering OFF).

One thing I thought of is that the powering ON stage, the 'buttonPushCounter' is always going to be an odd number.
The second stage, powering OFF will always be an even number. (unless things get garbled)..

So wondering if the 'buttonPushCounter' is currently an odd number it proceeds (if the switch is pressed) to the 2nd stage of powering OFF.

Or the power ON stage can be defined as having a remainder of 1?
And the Power OFF stage can be defined as having a remainder of 1 as well?

thomas

You could do what you want by counting button presses but to my mind it would be simpler to set a boolean variable to true or false depending on whether the cabinet was powered on or not.

Start with the variable, let's call it poweredOn, set to false. Detect the state change of switch state from off to on then, if poweredOn is false execute the code to turn the power on and set poweredOn to true else if poweredOn is true execute the code to turn the power off and set poweredOn to false.

You have quite rightly turned on the internal pullup resistor to avoid the switch input floating but in my opinion it is more explicit if you usepinMode(mainSwitch, INPUT_PULLUP);

Edit : Changed INPUT to INPUT_PULLUP

perhaps you could try it like this:

Calling a function when the button is pushed.... Remember that your blocking code will affect the response of a button push if for example your function is running. Trying to do ON function without blocking will allow you to power down, even if your program was in the process of powering up (not sure if you would want that, anyway)

Note I added turning on pin13 for a visualization of which function was called last.

not tested...

// Turning on electrical systems in my Arcade Cabinet with Seeed relay module.
// When powering up, this is the order of the relays:
// AC power remote relay, TV remote relay and finally the computer relay.
// When shutting down, it's reversed:
// Computer relay (10 sec. for complete shutdown). 
// Then AC power remote relay.
// The arduino in this setup will be powered by a 9V battery.
// 12Sep14

int switchState = LOW;
const int relayPin1 = 7;    //Digital pin to control the relay for the AC power (remote AC outlet) 'relay #1'.
const int relayPin2 = 6;    //Digital pin to control the relay for the TV (remote) 'relay #2'.
const int relayPin3 = 5;    //Digital pin to control the relay for the Computer  'relay #3'.
const int mainSwitch = 9;   //Digital pin input to control relays 1-3.
long previousMillis = 0;        // will store last time switch was pressed
long countPresses = 0;           // interval (milliseconds)
int lastButtonState;
int state = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(mainSwitch, INPUT);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(mainSwitch, HIGH);
}

void loop()
{
  int buttonState = digitalRead(mainSwitch);
  if(buttonState == LOW && lastButtonState == HIGH)  // main button is pressed
  {
    state = 1 - state;
    if (state == 1)
    {
      functionsOn();
      digitalWrite(13, HIGH);
    }
    else
    {
      functionsOff();
      digitalWrite(13,LOW);
    }
  }
  lastButtonState = buttonState;
}

void functionsOn()
{
  delay(3000);   //Wait a few seconds after pushing the main Cabinet switch.
  //First turn on electrical system (Will remain on until the end or no more movement).
  digitalWrite(relayPin1,HIGH);
  delay(300);
  //Turn off relay for AC power (remote cntrl.).
  digitalWrite(relayPin1,LOW);
  delay(5000);
  //Turn on relay for TV (remote cntrl.).
  digitalWrite(relayPin2,HIGH);  //Turns on TV.
  delay(300);
  //Turn off relay for TV.
  digitalWrite(relayPin2,LOW);
  delay(3000);
  //Turn on relay for PC.
  digitalWrite(relayPin3,HIGH);  //Turns on PC.
  delay(300);
  //Turn off relay for PC.
  digitalWrite(relayPin3,LOW);
  delay(10000);
}


void functionsOff()
{
  // Now Shutting down the entire Arcade Cabinet.
  // Relays are reversed and we only need PC relay and then the AC power relay.
  digitalWrite(relayPin3,HIGH);  
  delay(300);

  //Turn off relay for PC.
  digitalWrite(relayPin3,LOW);
  delay(10000);

  digitalWrite(relayPin1,HIGH);
  delay(300);
  //Turn off relay for Elect Sys.
  digitalWrite(relayPin1,LOW);
  delay(5000);
}

BulldogLowell:
perhaps you could try it like this:

Calling a function when the button is pushed.... Remember that your blocking code will affect the response of a button push if for example your function is running. Trying to do ON function without blocking will allow you to power down, even if your program was in the process of powering up (not sure if you would want that, anyway)

Note I added turning on pin13 for a visualization of which function was called last.

not tested...

// Turning on electrical systems in my Arcade Cabinet with Seeed relay module.

// When powering up, this is the order of the relays:
// AC power remote relay, TV remote relay and finally the computer relay.
// When shutting down, it's reversed:
// Computer relay (10 sec. for complete shutdown).
// Then AC power remote relay.
// The arduino in this setup will be powered by a 9V battery.
// 12Sep14

int switchState = LOW;
const int relayPin1 = 7;    //Digital pin to control the relay for the AC power (remote AC outlet) 'relay #1'.
const int relayPin2 = 6;    //Digital pin to control the relay for the TV (remote) 'relay #2'.
const int relayPin3 = 5;    //Digital pin to control the relay for the Computer  'relay #3'.
const int mainSwitch = 9;   //Digital pin input to control relays 1-3.
long previousMillis = 0;        // will store last time switch was pressed
long countPresses = 0;           // interval (milliseconds)
int lastButtonState;
int state = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(mainSwitch, INPUT);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(mainSwitch, HIGH);
}

void loop()
{
  int buttonState = digitalRead(mainSwitch);
  if(buttonState == LOW && lastButtonState == HIGH)  // main button is pressed
  {
    state = 1 - state;
    if (state == 1)
    {
      functionsOn();
      digitalWrite(13, HIGH);
    }
    else
    {
      functionsOff();
      digitalWrite(13,LOW);
    }
  }
  lastButtonState = buttonState;
}

void functionsOn()
{
  delay(3000);   //Wait a few seconds after pushing the main Cabinet switch.
  //First turn on electrical system (Will remain on until the end or no more movement).
  digitalWrite(relayPin1,HIGH);
  delay(300);
  //Turn off relay for AC power (remote cntrl.).
  digitalWrite(relayPin1,LOW);
  delay(5000);
  //Turn on relay for TV (remote cntrl.).
  digitalWrite(relayPin2,HIGH);  //Turns on TV.
  delay(300);
  //Turn off relay for TV.
  digitalWrite(relayPin2,LOW);
  delay(3000);
  //Turn on relay for PC.
  digitalWrite(relayPin3,HIGH);  //Turns on PC.
  delay(300);
  //Turn off relay for PC.
  digitalWrite(relayPin3,LOW);
  delay(10000);
}

void functionsOff()
{
  // Now Shutting down the entire Arcade Cabinet.
  // Relays are reversed and we only need PC relay and then the AC power relay.
  digitalWrite(relayPin3,HIGH); 
  delay(300);

//Turn off relay for PC.
  digitalWrite(relayPin3,LOW);
  delay(10000);

digitalWrite(relayPin1,HIGH);
  delay(300);
  //Turn off relay for Elect Sys.
  digitalWrite(relayPin1,LOW);
  delay(5000);
}

Thanks!
That works perfect :slight_smile:
I may have to add another relay for the AC remote since it has separate ON and OFF buttons but no big deal since I have 4 relays total to work with.

I'm going to power the Arduino + Relay shield with a 9VDC battery. Would this be OK? Would the battery last more than just a few days/weeks...I guess I'll try this out and see.
If it doesn't do you have any suggestions on powering both the Arduino and Relay shield (has to be battery powered)?

Many thanks again,

Thomas

I'm going to power the Arduino + Relay shield with a 9VDC battery. Would this be OK? Would the battery last more than just a few days/weeks..

If you mean what I know as a PP3 battery then no, it will not last long. Only a matter of hours.

You would be better of using 6 AA batteries, but why not use whatever is powering the arcade cabinet ?

UKHeliBob:

I'm going to power the Arduino + Relay shield with a 9VDC battery. Would this be OK? Would the battery last more than just a few days/weeks..

If you mean what I know as a PP3 battery then no, it will not last long. Only a matter of hours.

You would be better of using 6 AA batteries, but why not use whatever is powering the arcade cabinet ?

When the cabinet is OFF there will be no power.
I could add some type of charging circuit..? (for when the cabinet is ON it can recharge the batteries)

thomas

Here is the final sketch (if anyone is interested)

// Arcade cabinet power ON and OFF sketch using only one switch.
// Turning on electrical systems in my Arcade Cabinet with Seeed relay module.
// When powering up, this is the order of the relays:
// AC power remote relay, TV remote relay and finally the computer relay.
// When shutting down, it's reversed:
// Computer relay (10 sec. for complete shutdown). 
// Then AC power remote relay.
// The arduino in this setup will be powered by a (ytbd) battery (and possible charging circuit).
// 16Sep14

int switchState = LOW;
const int relayPin1 = 7;    // Digital pin to control the relay for the AC remote power ON Button 'relay #1'.
const int relayPin2 = 6;    // Digital pin to control the relay for the AC remote power OFF Button 'relay #2'.
const int relayPin3 = 5;    // Digital pin to control the relay for the TV Remote ON/OFF  'relay #3'.
const int relayPin4 = 4;    // Digital pin to control the relay for the PC ON/OFF switch  'relay #4'.
const int mainSwitch = 9;   // Digital pin input to control relays 1-3.
long previousMillis = 0;    // Will store last time switch was pressed
long countPresses = 0;      // Interval (milliseconds)
int lastButtonState;
int state = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(mainSwitch, INPUT_PULLUP);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(mainSwitch, HIGH);
}

void loop()
{
  int buttonState = digitalRead(mainSwitch);
  if(buttonState == LOW && lastButtonState == HIGH)  // main button is pressed
  {
    state = 1 - state;
    if (state == 1)
    {
      functionsOn();
      digitalWrite(13, HIGH);
    }
    else
    {
      functionsOff();
      digitalWrite(13,LOW);
    }
  }
  lastButtonState = buttonState;
}

void functionsOn()
{
  // Now Powering Up entire Arcade Cabinet.
  // First turn on AC electrical system- remote control, TV & finally the PC. 
  // These will remain on until the mainSwitch is pressed again.
  delay(1000);  // Slight delay before activating relays.
  digitalWrite(relayPin1,HIGH);
  delay(300);
  // Turn off relay for AC power (remote cntrl.).
  digitalWrite(relayPin1,LOW);
  delay(5000);
  // Turn on relay for TV (remote cntrl.).
  digitalWrite(relayPin3,HIGH);  //Turns on TV.
  delay(300);
  // Turn off relay for TV.
  digitalWrite(relayPin3,LOW);
  delay(3000);
  // Turn on relay for PC.
  digitalWrite(relayPin4,HIGH);  //Turns on PC.
  delay(300);
  // Turn off relay for PC.
  digitalWrite(relayPin4,LOW);
  delay(10000);
}


void functionsOff()
{
  // Now Shutting down the entire Arcade Cabinet.
  // Relays are activated in reverse order but 
  // we only need PC relay and the AC power relay.
  // Turn on relay for PC.
  delay(1000);  // Slight delay before activating relays.
  digitalWrite(relayPin4,HIGH);  
  delay(300);
  // Turn off relay for PC.
  digitalWrite(relayPin4,LOW);
  delay(10000);    //Wait 10 seconds to let PC totally shut down.
  //Turn on relay for AC remote.
  digitalWrite(relayPin2,HIGH);
  delay(300);
  // Turn off relay for AC remote.
  digitalWrite(relayPin2,LOW);
  delay(5000);
}

When the cabinet is OFF there will be no power.

Sorry but I don't understand. When the Arduino turns on the relay(s) they will switch some power. Where is that power coming from ?

UKHeliBob:

When the cabinet is OFF there will be no power.

Sorry but I don't understand. When the Arduino turns on the relay(s) they will switch some power. Where is that power coming from ?

When the cabinet is off, there is no AC power going to the cabinet.
The batteries connected to the Arduino will provide power to activate the relays to turn on entire system when there is no AC power.

t

thomas3120:

UKHeliBob:

When the cabinet is OFF there will be no power.

Sorry but I don't understand. When the Arduino turns on the relay(s) they will switch some power. Where is that power coming from ?

When the cabinet is off, there is no AC power going to the cabinet.
The batteries connected to the Arduino will provide power to activate the relays to turn on entire system when there is no AC power.

t

The Arduino is connected to the relays, which must be external to the cabinet so you already have both mains and relay activation wires coming to/from the cabinet. Why not simply add 2 more wires and power the Arduino externally ? You don't even need to add an extra cable if you use multi core cable, such as networking cable to send the power to the relays when activated and low voltage power to the Arduino.

UKHeliBob:

thomas3120:

UKHeliBob:

When the cabinet is OFF there will be no power.

Sorry but I don't understand. When the Arduino turns on the relay(s) they will switch some power. Where is that power coming from ?

When the cabinet is off, there is no AC power going to the cabinet.
The batteries connected to the Arduino will provide power to activate the relays to turn on entire system when there is no AC power.

t

The Arduino is connected to the relays, which must be external to the cabinet so you already have both mains and relay activation wires coming to/from the cabinet. Why not simply add 2 more wires and power the Arduino externally ? You don't even need to add an extra cable if you use multi core cable, such as networking cable to send the power to the relays when activated and low voltage power to the Arduino.

I have to give you thanks for your posts. It made me do an ahh haaa...light bulb lol :wink: + karma for UKHeliBob :smiley:
I installed a double AC outlet(before I just had a single). On one side of the outlet there is always AC and where I have my PSU for the Arduino/relay shield. other side has the AC remote/relay.

Here is where I hid the Arduino/relay shield (under the CP).

t

Have a small bug in my sketch..
When I press the main momentary button to turn OFF the Arcade, I find that it turns itself back on in an hour or so without pressing the switch... really strange :astonished:

However, when the Arcade is ON, it stays ON unless I press the main momentary switch.

Any help/input appreciated.
Here's the sketch:

// Arcade cabinet power ON and OFF sketch using only one main momentary switch.
// Using Arduino Uno & Seeed relay shield.
// When powering up, this is the order of the relays:
// AC power remote relay, TV remote relay and finally the computer relay.
// When shutting down, it's reversed:
// Computer relay (10 sec. for complete shutdown). 
// Then AC power remote relay.
// 16Sep14

int switchState = LOW;
const int relayPin1 = 7;    // Digital pin to control the relay for the AC power ON Button 'relay #1'.
const int relayPin2 = 6;    // Digital pin to control the relay for the AC power OFF Button 'relay #2'.
const int relayPin3 = 5;    // Digital pin to control the relay for the TV Remote ON/OFF  'relay #3'.
const int relayPin4 = 4;    // Digital pin to control the relay for the PC ON/OFF switch  'relay #4'.
const int mainSwitch = 9;   // Digital pin input to control relays 1-4.
long previousMillis = 0;    // Will store last time switch was pressed
long countPresses = 0;      // Interval (milliseconds)
int lastButtonState;
int state = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(mainSwitch, INPUT_PULLUP);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(mainSwitch, HIGH);
}

void loop()
{
  int buttonState = digitalRead(mainSwitch);
  if(buttonState == LOW && lastButtonState == HIGH)  // main button is pressed
  {
    state = 1 - state;
    if (state == 1)
    {
      functionsOn();
      digitalWrite(13, HIGH);
    }
    else
    {
      functionsOff();
      digitalWrite(13,LOW);
    }
  }
  lastButtonState = buttonState;
}

void functionsOn()
{
  // Now Powering Up entire Arcade Cabinet.
  // First turn on AC electrical system- remote control, TV & finally the PC. 
  // These will remain on until the mainSwitch is pressed again.
  delay(1000);  // Slight delay before activating relays.
  digitalWrite(relayPin1,HIGH);
  delay(300);
  // Turn off relay for AC power (remote cntrl.).
  digitalWrite(relayPin1,LOW);
  delay(5000);
  // Turn on relay for TV (remote cntrl.).
  digitalWrite(relayPin3,HIGH);  //Turns on TV.
  delay(300);
  // Turn off relay for TV.
  digitalWrite(relayPin3,LOW);
  delay(3000);
  // Turn on relay for PC.
  digitalWrite(relayPin4,HIGH);  //Turns on PC.
  delay(300);
  // Turn off relay for PC.
  digitalWrite(relayPin4,LOW);
  delay(1000);
}


void functionsOff()
{
  // Now Shutting down the entire Arcade Cabinet.
  // Relays are activated in reverse order but 
  // we only need PC relay and the AC power relay.
  // Turn on relay for PC.
  delay(1000);  // Slight delay before activating relays.
  digitalWrite(relayPin4,HIGH);  
  delay(300);
  // Turn off relay for PC.
  digitalWrite(relayPin4,LOW);
  delay(15000);    //Wait 15 seconds to let PC totally shut down.
  //Turn on relay for AC remote.
  digitalWrite(relayPin2,HIGH);
  delay(300);
  // Turn off relay for AC remote.
  digitalWrite(relayPin2,LOW);
  delay(1000);
}
  digitalWrite(mainSwitch, HIGH);

You've already turned the pullup resistor on, by using INPUT_PULLUP.

How is the main switch actually wired?

Are you sure a mouse isn't pressing the switch?

PaulS:

  digitalWrite(mainSwitch, HIGH);

You've already turned the pullup resistor on, by using INPUT_PULLUP.

How is the main switch actually wired?

Are you sure a mouse isn't pressing the switch?

Hey Paul,
I have the main Switch wired directly to Digital pin 9 and Gnd. When the momentary switch is pressed, setting the pin LOW, correct?

Seeed Relay that I'm using:
http://www.seeedstudio.com/wiki/Relay_Shield_V1.0

Wondering if this, which currently is in void setup():

digitalWrite(mainSwitch, HIGH);

Should go here:

void loop()
{
digitalWrite(mainSwitch, HIGH);

int buttonState = digitalRead(mainSwitch);
  if(buttonState == LOW && lastButtonState == HIGH)  // main button is pressed
//...
//...

}

Wondering if this, which currently is in void setup(): ... Should go here:

Why would you need to turn the internal pullup resistor on on every pass through loop()?

When the momentary switch is pressed, setting the pin LOW, correct?

The pin is pulled LOW when the switch is pressed.

PaulS:

When the momentary switch is pressed, setting the pin LOW, correct?

The pin is pulled LOW when the switch is pressed.

Why would you need to turn the internal pullup resistor on on every pass through loop()?

I don't know, this has me stumped... I guess I put it there to make sure it stays HIGH lol... unless it's pulled LOW because I 'physically' press the switch.
:confused: