Need help programming in a switch..

alright so i got this code online for a 2 step launch control for my car and i want to code in 2 switches so that if both switch 1 (s1) and switch 2 (s2) are flipped off then the 2 outputs in the code (spark 1 and spark 2) will just stay off but if s2 is off and s1 is on then i want the outputs to go through the code normally as if no switches were there, but if s2 is on at all at anypoint then it will go through the code normally as well... if someone can do this for me that would be lovey... thanks ... heres the code..

int rpmpin = 8;
int spark1 = 7;
int spark2 = 6;


int lastrpm = 0;
int cutspark = 0;
long cuttime = 0;

int allowance = 100;
long cutduration = 400;
int cutrpm = 2000;

void setup()
{
  Serial.begin(9600);
  pinMode(rpmpin, INPUT);
  pinMode(spark1, OUTPUT);
  pinMode(spark2, OUTPUT);
}

void loop()
{
  unsigned long duration = pulseIn(rpmpin, HIGH);
  int rpm = (((1000000 / duration) * 60) / 5.1);
  if(rpm >= 1)
  {
    Serial.println(rpm);
    if(rpm >= cutrpm && lastrpm >= cutrpm && cutspark == 0)
    {
      //Serial.println(rpm);
      Serial.println("------CUT-------");
      cutspark = 1;
      cuttime = millis();
      digitalWrite(spark1, HIGH);
      digitalWrite(spark2, HIGH);
    } else 
    lastrpm = rpm;
    if((cutrpm - allowance) > rpm && cutspark == 1)
    {
      cutduration -= 5;
      Serial.println(cutduration);
    }
    else if ((cutrpm + allowance) < rpm && cutspark == 1)
    {
      cutduration += 5;
      Serial.println(cutduration);
    }
  }
  if(cuttime + cutduration >= millis()) {
      cutspark = 0;
      digitalWrite(spark1, LOW);
      digitalWrite(spark2, LOW);
   }
}

If I understand right, and what you want is:

S1     S2    
off  off  -> off
on   off  -> normal
off  on   -> normal
on   on   -> normal

the code will be:

int rpmpin = 8;
int spark1 = 7;
int spark2 = 6;


int lastrpm = 0;
int cutspark = 0;
long cuttime = 0;

int allowance = 100;
long cutduration = 400;
int cutrpm = 2000;


int switch1 = 8;
int switch2 = 9;

int sw1State;
int sw2State;

void setup()
{
  Serial.begin(9600);
  pinMode(rpmpin, INPUT);
  pinMode(spark1, OUTPUT);
  pinMode(spark2, OUTPUT);

  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
}

void loop()
{
  unsigned long duration = pulseIn(rpmpin, HIGH);

  int rpm = (((1000000 / duration) * 60) / 5.1);
  
  sw1State = digitalRead(switch1);
  sw2State = digitalRead(switch2);

  if (sw1State == LOW && sw2State == LOW) {
    Serial.println("STOPPED");
  }
  else {
    if (rpm >= 1)
    {
      Serial.println(rpm);
      if (rpm >= cutrpm && lastrpm >= cutrpm && cutspark == 0)
      {
        //Serial.println(rpm);
        Serial.println("------CUT-------");
        cutspark = 1;
        cuttime = millis();
        digitalWrite(spark1, HIGH);
        digitalWrite(spark2, HIGH);
      } else
        lastrpm = rpm;
      if ((cutrpm - allowance) > rpm && cutspark == 1)
      {
        cutduration -= 5;
        Serial.println(cutduration);
      }
      else if ((cutrpm + allowance) < rpm && cutspark == 1)
      {
        cutduration += 5;
        Serial.println(cutduration);
      }
    }
    if (cuttime + cutduration >= millis()) {
      cutspark = 0;
      digitalWrite(spark1, LOW);
      digitalWrite(spark2, LOW);
    }
  }
}

luisilva:
If I understand right, and what you want is:

S1     S2    

off  off  -> off
on   off  -> normal
off  on   -> normal
on   on   -> normal





the code will be:


int rpmpin = 8;
int spark1 = 7;
int spark2 = 6;

int lastrpm = 0;
int cutspark = 0;
long cuttime = 0;

int allowance = 100;
long cutduration = 400;
int cutrpm = 2000;

int switch1 = 8;
int switch2 = 9;

int sw1State;
int sw2State;

void setup()
{
 Serial.begin(9600);
 pinMode(rpmpin, INPUT);
 pinMode(spark1, OUTPUT);
 pinMode(spark2, OUTPUT);

pinMode(switch1, INPUT_PULLUP);
 pinMode(switch2, INPUT_PULLUP);
}

void loop()
{
 unsigned long duration = pulseIn(rpmpin, HIGH);

int rpm = (((1000000 / duration) * 60) / 5.1);
 
 sw1State = digitalRead(switch1);
 sw2State = digitalRead(switch2);

if (sw1State == LOW && sw2State == LOW) {
   Serial.println("STOPPED");
 }
 else {
   if (rpm >= 1)
   {
     Serial.println(rpm);
     if (rpm >= cutrpm && lastrpm >= cutrpm && cutspark == 0)
     {
       //Serial.println(rpm);
       Serial.println("------CUT-------");
       cutspark = 1;
       cuttime = millis();
       digitalWrite(spark1, HIGH);
       digitalWrite(spark2, HIGH);
     } else
       lastrpm = rpm;
     if ((cutrpm - allowance) > rpm && cutspark == 1)
     {
       cutduration -= 5;
       Serial.println(cutduration);
     }
     else if ((cutrpm + allowance) < rpm && cutspark == 1)
     {
       cutduration += 5;
       Serial.println(cutduration);
     }
   }
   if (cuttime + cutduration >= millis()) {
     cutspark = 0;
     digitalWrite(spark1, LOW);
     digitalWrite(spark2, LOW);
   }
 }
}

you interpreted me trying to say it out right but i said it wrong haha, a graph made it much more simple, what i want is this

S1 S2
off off -> off
on off -> off
off on -> off
on on -> normal

sorry about that haha

OK, is not a big deal.

int rpmpin = 8;
int spark1 = 7;
int spark2 = 6;


int lastrpm = 0;
int cutspark = 0;
long cuttime = 0;

int allowance = 100;
long cutduration = 400;
int cutrpm = 2000;


int switch1 = 8;
int switch2 = 9;

int sw1State;
int sw2State;

void setup()
{
  Serial.begin(9600);
  pinMode(rpmpin, INPUT);
  pinMode(spark1, OUTPUT);
  pinMode(spark2, OUTPUT);

  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
}

void loop()
{
  unsigned long duration = pulseIn(rpmpin, HIGH);

  int rpm = (((1000000 / duration) * 60) / 5.1);

  sw1State = digitalRead(switch1);
  sw2State = digitalRead(switch2);

  if (sw1State == HIGH && sw2State == HIGH) {
    if (rpm >= 1)
    {
      Serial.println(rpm);
      if (rpm >= cutrpm && lastrpm >= cutrpm && cutspark == 0)
      {
        //Serial.println(rpm);
        Serial.println("------CUT-------");
        cutspark = 1;
        cuttime = millis();
        digitalWrite(spark1, HIGH);
        digitalWrite(spark2, HIGH);
      } else
        lastrpm = rpm;
      if ((cutrpm - allowance) > rpm && cutspark == 1)
      {
        cutduration -= 5;
        Serial.println(cutduration);
      }
      else if ((cutrpm + allowance) < rpm && cutspark == 1)
      {
        cutduration += 5;
        Serial.println(cutduration);
      }
    }
    if (cuttime + cutduration >= millis()) {
      cutspark = 0;
      digitalWrite(spark1, LOW);
      digitalWrite(spark2, LOW);
    }
  }
  else {
    Serial.println("STOPPED");
  }
}

luisilva:
OK, is not a big deal.

int rpmpin = 8;

int spark1 = 7;
int spark2 = 6;

int lastrpm = 0;
int cutspark = 0;
long cuttime = 0;

int allowance = 100;
long cutduration = 400;
int cutrpm = 2000;

int switch1 = 8;
int switch2 = 9;

int sw1State;
int sw2State;

void setup()
{
  Serial.begin(9600);
  pinMode(rpmpin, INPUT);
  pinMode(spark1, OUTPUT);
  pinMode(spark2, OUTPUT);

pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
}

void loop()
{
  unsigned long duration = pulseIn(rpmpin, HIGH);

int rpm = (((1000000 / duration) * 60) / 5.1);

sw1State = digitalRead(switch1);
  sw2State = digitalRead(switch2);

if (sw1State == HIGH && sw2State == HIGH) {
    if (rpm >= 1)
    {
      Serial.println(rpm);
      if (rpm >= cutrpm && lastrpm >= cutrpm && cutspark == 0)
      {
        //Serial.println(rpm);
        Serial.println("------CUT-------");
        cutspark = 1;
        cuttime = millis();
        digitalWrite(spark1, HIGH);
        digitalWrite(spark2, HIGH);
      } else
        lastrpm = rpm;
      if ((cutrpm - allowance) > rpm && cutspark == 1)
      {
        cutduration -= 5;
        Serial.println(cutduration);
      }
      else if ((cutrpm + allowance) < rpm && cutspark == 1)
      {
        cutduration += 5;
        Serial.println(cutduration);
      }
    }
    if (cuttime + cutduration >= millis()) {
      cutspark = 0;
      digitalWrite(spark1, LOW);
      digitalWrite(spark2, LOW);
    }
  }
  else {
    Serial.println("STOPPED");
  }
}

Awesome man! thanks it works great! (:

Or just wire the two switches in series and treat them as one input.

Chris24f22:
a graph made it much more simple

It's called a Truth Table and in a digital world make things much easier to follow.

Henry_Best:
Or just wire the two switches in series and treat them as one input.

i would of done that but the switches are built in switches on my car one on clutch and the other on gas and if i did that then it could mess up the cruise control lol