Normally Closed Button

Hi,

I am coding a roller coaster control panel. All of my buttons work apart from one red button (Called sstop in the program). I have one other NC button which works perfectly fine. I have tried changed it to == HIGH instead of LOW but all this does is repeatedly press the 's' button which isn't ideal, it doesn't even stop when you press it down.

Here is the program.

#include <Keyboard.h>
// ====================================================================

//Set Pin Values for buttons
const int power = 1;
const int reset = 2;
const int switch1 = 3;
const int switch2 = 4;
const int switch3 = 5;
const int switch4 = 6;
const int sstop = 7;
const int preshow = 8;
const int estop = 9;
const int gates = 10;
const int restraintsOPEN = 11;
const int restraintsCLOSED = 12;
const int tilt = 13;
const int selector = A0;
const int launch = A1;
const int dispatch = A2;

void setup(){
  // Pin modes
  pinMode(power, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);
  pinMode(sstop, INPUT_PULLUP);
  pinMode(preshow, INPUT_PULLUP);
  pinMode(estop, INPUT_PULLUP);
  pinMode(gates, INPUT_PULLUP);
  pinMode(restraintsOPEN, INPUT_PULLUP);
  pinMode(restraintsCLOSED, INPUT_PULLUP);
  pinMode(tilt, INPUT_PULLUP);
  pinMode(selector, INPUT_PULLUP);
  pinMode(launch, INPUT_PULLUP);
  pinMode(dispatch, INPUT_PULLUP);}
 
void loop() {
// Set variables for button states

int powerPOS = digitalRead(power);
int resetPOS = digitalRead(reset);
int switch1POS = digitalRead(switch1);
int switch2POS = digitalRead(switch2);
int switch3POS = digitalRead(switch3);
int switch4POS = digitalRead(switch4);
int sstopPOS = digitalRead(sstop);
int preshowPOS = digitalRead(preshow);
int estopPOS = digitalRead(estop);
int gatesPOS = digitalRead(gates);
int tiltPOS = digitalRead(tilt);
int selectorPOS = digitalRead(selector);
int launchPOS = digitalRead(launch);
int dispatchPOS = digitalRead(dispatch);
int prevestoppos = HIGH;
int power = 0;

// |------------------------------------------------------------------------------|
// |===============================Station Controls===============================|
// |------------------------------------------------------------------------------|

//Power (stops keyboard working when down)
if (powerPOS == LOW){ //When down
    power = 1;} //Power on
if (powerPOS == HIGH){
    power = 0;} //Off

 //Reset
 if (estopPOS == HIGH and resetPOS == LOW and power == 1){
  Keyboard.press('r');
  delay(100);
  Keyboard.releaseAll();}

//Station Stop
if (sstopPOS == LOW and power == 1){
  Keyboard.press('s');
  delay(100);
  Keyboard.releaseAll();}

//Emergency Stop
if (estopPOS != prevestoppos and power == 1){
  Keyboard.press('e');
  Keyboard.press('s');
  delay(100);
  Keyboard.releaseAll();
  estopPOS = prevestoppos;}
//Grey switch 1
if (switch1POS == LOW){
  Keyboard.press('+');
  delay(100);
  Keyboard.releaseAll();}

//Grey Switch 2
if (switch2POS == LOW){
  Keyboard.press('1');
  Keyboard.press('v');
  delay(100);
  Keyboard.releaseAll();}

//Grey switch 3
if (switch3POS == LOW){
  Keyboard.press('-');
  delay(100);
  Keyboard.releaseAll();}

//Grey switch 4
if (switch4POS == LOW){
  Keyboard.press('2');
  Keyboard.press('z');
  delay(100);
  Keyboard.releaseAll();}

//Gates
if (gatesPOS == LOW and power == 1){
  Keyboard.press(KEY_RIGHT_ARROW);
  delay (100);
  Keyboard.releaseAll();}
if (gatesPOS == HIGH and power == 1){
  Keyboard.press(KEY_LEFT_ARROW);
  delay (100);
  Keyboard.releaseAll();}

//Restraints
if (digitalRead(restraintsOPEN) == LOW and power == 1){
  Keyboard.press(KEY_UP_ARROW);
  delay(100);
  Keyboard.releaseAll();}
  
if (digitalRead(restraintsCLOSED) == LOW and power == 1){
  Keyboard.press(KEY_DOWN_ARROW);
  delay(100);
  Keyboard.releaseAll();}

// Station Stop Button
if (tiltPOS == LOW and power == 1){ // If tilt is pressed
  Keyboard.press(KEY_RETURN); // Press enter for 4 seconds
  delay (100); 
  Keyboard.releaseAll();}

//2nd 2 pos switch/selector switch

//Launch Button
if (launchPOS == LOW and power == 1){ // If launch is pressed
  Keyboard.press('l'); // Press L
  delay (100);
  Keyboard.releaseAll();}

// Dispatch Button
if (dispatchPOS == LOW and power == 1){ // If dispatch is pressed
  Keyboard.press(' '); // Press spacebar
  delay (100);
  Keyboard.releaseAll();}



//Preshow doors/start
if (preshowPOS == LOW and power == 1){ // If preshow is pressed
  Keyboard.press(KEY_RETURN); // press return
  delay(100); 
  Keyboard.releaseAll();}

}

As you are using INPUT_PULLUP I presume when your switches are closed they pull the pin to GND. In that case I would expect a NC switch to report HIGH when pressed.

It would only take 5 minutes to write a short program to verify that.

I have no idea what this means

but all this does is repeatedly press the 's' button which isn't ideal

I don't see any variable called "s" - which would, in any case, be a lousy name.

As a separate matter if the RED SSTOP switch is an emergency switch and if there is a risk of people being injured I would make the switch work completely independently of the Arduino.

...R

//Emergency Stop
if (estopPOS != prevestoppos and power == 1){
  Keyboard.press('e');
  Keyboard.press('s');
  delay(100);
  Keyboard.releaseAll();
  estopPOS = prevestoppos;}

in code line 89 you have to write the prevestoppos variable first.
like this... prevestoppos = estopPOS;

Robin2:
As you are using INPUT_PULLUP I presume when your switches are closed they pull the pin to GND. In that case I would expect a NC switch to report HIGH when pressed.

It would only take 5 minutes to write a short program to verify that.

I have no idea what this meansI don't see any variable called "s" - which would, in any case, be a lousy name.

As a separate matter if the RED SSTOP switch is an emergency switch and if there is a risk of people being injured I would make the switch work completely independently of the Arduino.

...R

I have tried changing it to HIGH but it repeatedly presses the 's' key, like the key on the keyboard, not a button called s.

The panel is for fun, there is no risk to life or limb, it is for a simulation game.

GrOnThOs:

//Emergency Stop

if (estopPOS != prevestoppos and power == 1){
  Keyboard.press('e');
  Keyboard.press('s');
  delay(100);
  Keyboard.releaseAll();
  estopPOS = prevestoppos;}




in code line 89 you have to write the prevestoppos variable first.
like this... prevestoppos = estopPOS;

Thanks I'll change it now!