Problem using case

I am trying to get a uno to control a 4 relay shield to control electric seating on a vehicle. I have 2 original switches that are centre off, with a momentry on in both directions. named as Don (drivers On), Doff (drivers Off), Pon (passengers On), Poff (passengers off). Each switch connects to a digital pin on the uno.

There are two heat setting each side. Press Don once = low heat, press Don again = high Heat, press Don again = back to low Heat (flip flops between low and high) if you press Doff Drivers heating goes Off.

Passenger side seperate but identical.

All works fine using "IF" statement, but if you keep button pressed continues to flip flop low high etc.

tried to modify coding using state change detection.
i just cant get function to operate correctly.

const int Don = 2;        // Name digital pin 2 as Don (Drivers Side On)
const int Doff = 3;       // Name digital pin 3 as Doff (Drivers Side off)
const int Pon = 8;        // Name digital pin 8 as Pon (Passengers Side on)
const int Poff = 9;       // Name digital pin 9 as Poff (passengers Side Off)
const int DL = 7;         // Name digital pin 7 as Drivers Side Low, Relay 1
const int DH = 6;         // Name digital pin 6 as Drivers Side High, Relay 2 
const int PL = 5;         // Name digital pin 5 as Passengers Side Low, Relay 3
const int PH = 4;         // Name digital pin 4 as Passengers Side High, Relay 4


int DonState = 0;
int lastDonState = 0; 
        
int DoffState = 0;
int lastDoffState = 0; 

int PonState = 0;
int lastPonState = 0; 
        
int PoffState = 0;
int lastPoffState = 0; 


int countD = 1;           // Creat a count function and Set CountD to "1" 
int countP = 1;           // Creat a count function and Set CountP to "1" 



void setup()        
{                        
  pinMode(Don,INPUT);          // Declare Don as INPUT to Arduino
  pinMode(Doff,INPUT);         // Declare Doff as INPUT to Arduino
  pinMode(Pon,INPUT);          // Declare Pon as INPUT to Arduino 
  pinMode(Poff,INPUT);         // Declare Poff as Input to Arduino
  pinMode(DL,OUTPUT);          // Declare DL as Output (connected to relay 1)
  pinMode(DH,OUTPUT);          // Declare DH as Output (connected to relay 2)
  pinMode(PL,OUTPUT);          // Declare PL as Output (connected to relay 3)
  pinMode(PH,OUTPUT);          // Declare DH as Output (connected to relay 4)
  
  digitalWrite(Don,HIGH);      // Use internal Pull up to hold input high
  digitalWrite(Doff,HIGH);     // Use internal Pull up to hold input high
  digitalWrite(Pon,HIGH);      // Use internal Pull up to hold input high 
  digitalWrite(Poff,HIGH);     // Use internal Pull up to hold input high

  Serial.begin(9600);          // Send serial data at 9600 bps to enable monitoring 
}                             
               


void loop()  

{
 // Read the input Pins
 DonState = digitalRead (Don);
 DoffState = digitalRead (Doff);
 PonState = digitalRead (Pon);
 PoffState = digitalRead (Poff);

//Don Switch -----------------------------------------------------------------------

 if(DonState != lastDonState)
 {
 if(Don == LOW)                  // if Don is pressed perform action described in loop
 {                               // open loop
    countD++;                    // Increment CountD by 1
    if(countD >3)                // if count goes higher than 3
    countD=2;                    // limit count to max of 3
    Serial.println ("Don is closed");
   }                       
 else
 {
  Serial.println("Don is open");
  }
 }



 //Doff Switch ---------------------------------------------------------------------
 
   if(DoffState != lastDoffState)
 {
  
  if(Doff == LOW)                // if Dof is pressed perform action described in loop
  {                              // open loop     
     countD--;                   // Decrement CountD by 1
     countD--;                   // Decrement CountD By another 1 (if heating is on High  
     if(countD < 1)              // if count goes lower than 1
     countD = 1;                 // Limit count to min of 1
    Serial.println ("Doff is closed");
  }                              
else
{
   Serial.println("Doff is open");
}
 }

  
  // Pon Switch --------------------------------------------------------------------
  
 if(PonState != lastPonState)
 {
  
 if(Pon == LOW)                  // if Pon is pressed perform action described in loop
 {     
  
                                 // open loop
    countP++;                    // Increment CountD by 1
    if(countP >3)                // if count goes higher than 3
    countP=2;                    // limit count to max of 3
    Serial.println("Pon is closed");
 }
 else
 {
   Serial.println("Pon is open");
 } 
}

 
  
  // Poff Switch ---------------------------------------------------------------------

 if(PoffState != lastPoffState)
 {
  
  if(digitalRead(Poff) == LOW)   // if Poff is pressed perform action described in loop
  
  {                              // open loop
     countP--;                   // Decrement CountP by 1
     countP--;                   // Decrement CountP by another 1
     if(countP < 1)              // if count goes lower than 1
     countP = 1;                 // Limit count to min of 1
     Serial.println("Poff is closed");
  }                                 
else
{
    Serial.println("Poff is open");
 }
}
  delay (50);
  
  lastDonState = DonState;
  lastDoffState = DoffState;
  lastPonState = PonState;
  lastPoffState = PoffState;

If i load to uno and operate the 4 switches in turn i get

Don is open
Doff is open
Pon is open
Poff is open

Doff is open
Doff is open
Don is open
Don is open
Poff is closed
Poff is open
Pon is open
Pon is open

The first 4 lines are before any button is pressed

each button press gets 2 reports ???

POff is the only record off a switch closing. ????

Any help or guidance appreceated.

  pinMode(Don,INPUT);          // Declare Don as INPUT to Arduino
  pinMode(Doff,INPUT);         // Declare Doff as INPUT to Arduino
  pinMode(Pon,INPUT);          // Declare Pon as INPUT to Arduino
  pinMode(Poff,INPUT);         // Declare Poff as Input to Arduino

It is pointless to read any further, since you haven't told us HOW the switches are wired.

Incorrectly is what I'm going to guess.

Each switch connects to a digital pin on the uno.

Have you got any pullup resistors in place to keep the inputs at a known state at all times or is the voltage on them just floating at an uncertain voltage.

The switches are wired correctly They are two centre off momentary rocker switches. The common terminal is wired to Grnd and the two switch contacts are connected to digital pins 2 and 3 for the drivers switch and 8 and 9 for the passenger switch.

As the code I posted shows I'm using the internal pullups as
digitalWrite(Don,HIGH);
digitalWrite(Doff,HIGH);
digitalWrite(Pon,HIGH);
digitalWrite(Poff,HIGH);

Lynton:
As the code I posted shows I'm using the internal pullups as
digitalWrite(Don,HIGH); ...

It's done like this:

pinMode(A3, INPUT_PULLUP);

The word PULLUP appears nowhere in the code you posted.

Prior to Arduino 1.0.1, it was possible to configure the internal pull-ups in the following manner:
pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors

larryd:
Prior to Arduino 1.0.1, it was possible to configure the internal pull-ups in the following manner:
pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors

I think you are missing an only in the sentence.

The mentioned technique still works.

As larryd says

pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors

is in my code and works correctly.

The previous version of my code worked perfectly with the same hardware no changes to wiring or switches !!!
but using "if" just means that if the Don or Pon buttons are held longer than the delay setting the circuit flip flops from low to high. although it works its rather annoying.
I added if(DoffState != lastDoffState) etc to try and change it so it would only operate when the switches went from hight to low etc.
if I revert back to the previous code it works correctly as before. proving the switches and the pullups are working correctly.

larryd:
Prior to Arduino 1.0.1, it was possible to configure the internal pull-ups...

Well, I'll be jiggered.

Try doubling that delay(50) to see if the buttons take forever to debounce. Do they feel solid?