Switching between multiple state detection scripts with a switch

Hi. I am making a program in which I can switch between multiple scripts for a state detection counter.

I have a test bench that needs to do a certain amount of revolutions before a relay is switched off, so the test bench also stops. I want to chose between 3 ammounts of pulses (4 per revolution) using a rotary switch: 800, 2000 and 4000.
I used the state change detection script and got everything working, but now I want to intergrate the switch in it. It should be possible to do it with a ‘button’ script, but it doesnt seem to work.

What should I do different/ what do I need to change in the code?

This is what I’ve got so far (only 2 options now, just to test):

const int pulsePin = 4; // The pin receiving the pulses
const int relay = 13; // The pin switching the relay
const int Switch1 = 5; // 1st switch position ( 800 pulses)
const int Switch2 = 6; // 2nd switch position (2000 pulses)
const int Switch3 = 7; // 3rd switch position (4000 pulses)

int SwitchState = 0;
int pulseCounter = 0;
int pulseState = 0;
int lastPulseState = 0;

long lastEvent = 0;  // the last time the output pin was toggled
long interval = 1000;    // the debounce time; increase if the output flickers

void setup() {
pinMode(pulsePin, INPUT);
pinMode(Switch1, INPUT);
pinMode(Switch2, INPUT);
pinMode(Switch3, INPUT); 
pinMode(relay, OUTPUT);   //Relay is the output
Serial.begin(9600);       //Serial

}

void loop() {
  SwitchState = digitalRead(Switch1);
  if (Switch1 == HIGH) {
    pulseState = digitalRead(pulsePin);
  // compare the buttonState to its previous state
  if (pulseState != lastPulseState)
  {
    // if the state has changed, increment the counter
    if (pulseState == HIGH && millis() - lastPulseState > interval); // <-- Ignore this press if it is too close to the last one
    {
      lastEvent = millis(); // <-- record when the last good event occurred
  if (pulseState == HIGH){
    pulseCounter++;
    Serial.println("on");
    Serial.print("numer of pulses:  ");
    Serial.println(pulseCounter);
  } else {
    Serial.println("off");
  }
  lastPulseState = pulseState;
  if (pulseCounter > 400== 0){
    digitalWrite(relay, HIGH);
  } else{
    digitalWrite(relay, LOW);
  }
  }
}
  } else {
  SwitchState = digitalRead(Switch2);
  if (Switch2 == HIGH) {
    pulseState = digitalRead(pulsePin);
  // compare the buttonState to its previous state
  if (pulseState != lastPulseState)
  {
    // if the state has changed, increment the counter
    if (pulseState == HIGH && millis() - lastPulseState > interval); // <-- Ignore this press if it is too close to the last one
    {
      lastEvent = millis(); // <-- record when the last good event occurred
  if (pulseState == HIGH){
    pulseCounter++;
    Serial.println("on");
    Serial.print("numer of pulses:  ");
    Serial.println(pulseCounter);
  } else {
    Serial.println("off");
  }
  lastPulseState = pulseState;
  if (pulseCounter > 2000== 0){
    digitalWrite(relay, HIGH);
  } else{
    digitalWrite(relay, LOW);
  }
  }
}
  }
}
}

Any help is welcome!

  if (Switch1 == HIGH)The value of Switch1 will always be 5 in your program. You probably meant to check the value of SwitchState but I did not look any further.

Doesn’t ‘lastPulseState’ hold the result of a ‘digitalRead()’? You need to rethink this bit:-if (pulseState == HIGH && millis() - lastPulseState > interval); // <-- Ignore this press if it is too close to the last one(I think you meant to put ‘lastEvent’ in both of these conditionals.)

You need to use an ‘unsigned long’ to store ‘millis()’ values, not ‘long’:-

long lastEvent = 0;  // the last time the output pin was toggled
.
lastEvent = millis(); // <-- record when the last good event occurred

I don’t know what you’re trying to do here:-if (pulseCounter > 2000== 0){
You should format your code, or hit Ctrl-T or “>Tools >Autoformat” to do it automatically, before posting it. It’s very hard to follow what’s going on the way it’s presented.

Oh, and what Bob said also applies to your use of ‘Switch2’.

Re the point Bob mentioned, I've seen this problem posted multiple times. You have to READ the switch:

if (digitalRead(Switch1) == HIGH)

Best,
Michael