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.