Run Programme When Key Switch Enabled

Hi All, so I am writing code for NL2 Dispatch Control panel and the code its self works fine etc however one aspect I'm not sure how to implement the key switch. I would like it so the main body of the code like the dispatch button etc only run when the key is turned on. Ive attached the code below. Thanks

#include <Keyboard.h>

const int Dis1 = 1;
const int Dis2 = 13;
const int Floor = 3;
const int Train = 4;
const int Gate = 5;
const int Restraint = 6;
const int Reset = 7;
const int EStop = 8;

boolean FloorOpen = false;
boolean TrainRaised = false;
boolean GateOpen = false;
boolean RestraintOpen = false;
boolean RideStopped = false;

void setup() {
  pinMode(Dis1, INPUT_PULLUP);
  pinMode(Dis2, INPUT_PULLUP);
  pinMode(Floor, INPUT_PULLUP);
  pinMode(Train, INPUT_PULLUP);
  pinMode(Gate, INPUT_PULLUP);
  pinMode(Restraint, INPUT_PULLUP);
  pinMode(Reset, INPUT_PULLUP);
  pinMode(EStop, INPUT_PULLUP);

  Keyboard.begin();
}
void Dispatch(){
  Keyboard.print('1');
  delay(1000);
}
void FloorLower(){
  Keyboard.print('2');
  FloorOpen = true;
  delay(1000);
}
void FloorRaise(){
  Keyboard.print('3');
  FloorOpen = false;
  delay(1000);
}
void TrainRaise(){
  Keyboard.print('4');
  TrainRaised = true;
  delay(1000);
}
void TrainLower(){
  Keyboard.print('5');
  TrainRaised = false;
  delay(1000);

}
void OpenGate(){
  Keyboard.print('6');
  GateOpen = true;
  delay(1000);
}
void CloseGate(){
  Keyboard.print('/');
  GateOpen = false;
  delay(1000);
}
void OpenRestraint(){
  Keyboard.print('7');
  RestraintOpen = true;
  delay(1000);
}
void CloseRestraint(){
  Keyboard.print('8');
  RestraintOpen = false;
  delay(1000);
}
void ResetRide(){
  Keyboard.print('9');
  RideStopped = false;
  delay(1000);
}
void StopRide(){
  Keyboard.print('?');
  RideStopped = true;
  delay(1000);
}

void loop() {
  
  if (digitalRead(Dis1) == LOW && digitalRead(Dis2) == LOW ) {
    Dispatch();   
  }
  if (digitalRead(Floor) == LOW && FloorOpen == false ) {
    FloorLower();
  }
  if (digitalRead(Floor) == LOW && FloorOpen == true ) {
    FloorRaise();
  }
  if (digitalRead(Train) == LOW && TrainRaised == false ) {
    TrainRaise();
  }
  if (digitalRead(Train) == LOW && TrainRaised == true ) {
    TrainLower();
  }
  if (digitalRead(Gate) == LOW && GateOpen == false ) {
    OpenGate();
  }
   if (digitalRead(Gate) == HIGH && GateOpen == true ) {
    CloseGate();
  }
  if (digitalRead(Restraint) == LOW && RestraintOpen == false ) {
    OpenRestraint();
  }
  if (digitalRead(Restraint) == HIGH && RestraintOpen == true ) {
    CloseRestraint();
  }
   if (digitalRead(Reset) == LOW) {
    ResetRide();   
  }
   if (digitalRead(EStop) == LOW && RideStopped == false ) {
    StopRide();
  }
}
1 Like

First things first

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

As to your question, it is not clear to me what exactly you want to do. What key do you want to use to activate the code ? Is it one of the keys on the keyboard or perhaps an external key switch ?

Use code tags when posting your code

#include <Keyboard.h>

const int Dis1 = 1;
const int Dis2 = 13;
const int Floor = 3;
const int Train = 4;
const int Gate = 5;
const int Restraint = 6;
const int Reset = 7;
const int EStop = 8;

boolean FloorOpen = false;
boolean TrainRaised = false;
boolean GateOpen = false;
boolean RestraintOpen = false;
boolean RideStopped = false;

void setup() {
pinMode(Dis1, INPUT_PULLUP);
pinMode(Dis2, INPUT_PULLUP);
pinMode(Floor, INPUT_PULLUP);
pinMode(Train, INPUT_PULLUP);
pinMode(Gate, INPUT_PULLUP);
pinMode(Restraint, INPUT_PULLUP);
pinMode(Reset, INPUT_PULLUP);
pinMode(EStop, INPUT_PULLUP);

Keyboard.begin();
}
void Dispatch(){
Keyboard.print('1');
delay(1000);
}
void FloorLower(){
Keyboard.print('2');
FloorOpen = true;
delay(1000);
}
void FloorRaise(){
Keyboard.print('3');
FloorOpen = false;
delay(1000);
}
void TrainRaise(){
Keyboard.print('4');
TrainRaised = true;
delay(1000);
}
void TrainLower(){
Keyboard.print('5');
TrainRaised = false;
delay(1000);

}
void OpenGate(){
Keyboard.print('6');
GateOpen = true;
delay(1000);
}
void CloseGate(){
Keyboard.print('/');
GateOpen = false;
delay(1000);
}
void OpenRestraint(){
Keyboard.print('7');
RestraintOpen = true;
delay(1000);
}
void CloseRestraint(){
Keyboard.print('8');
RestraintOpen = false;
delay(1000);
}
void ResetRide(){
Keyboard.print('9');
RideStopped = false;
delay(1000);
}
void StopRide(){
Keyboard.print('?');
RideStopped = true;
delay(1000);
}

void loop() {
If(key ==dothething)
{

if (digitalRead(Dis1) == LOW && digitalRead(Dis2) == LOW ) {
Dispatch();
}
if (digitalRead(Floor) == LOW && FloorOpen == false ) {
FloorLower();
}
if (digitalRead(Floor) == LOW && FloorOpen == true ) {
FloorRaise();
}
if (digitalRead(Train) == LOW && TrainRaised == false ) {
TrainRaise();
}
if (digitalRead(Train) == LOW && TrainRaised == true ) {
TrainLower();
}
if (digitalRead(Gate) == LOW && GateOpen == false ) {
OpenGate();
}
if (digitalRead(Gate) == HIGH && GateOpen == true ) {
CloseGate();
}
if (digitalRead(Restraint) == LOW && RestraintOpen == false ) {
OpenRestraint();
}
if (digitalRead(Restraint) == HIGH && RestraintOpen == true ) {
CloseRestraint();
}
if (digitalRead(Reset) == LOW) {
ResetRide();
}
if (digitalRead(EStop) == LOW && RideStopped == false ) {
StopRide();
}
}
}

note the If(key ==dothething)
{

1 Like