The forum guide say give as much detail as possible.
I have managed to use a Uno and a relay shield to control the electric seat heaters on a car. The 4 relay on the shield are 1-2 for drivers seat Low and High. 3 and 4 for passengers seat low and high. The modified original vehicles switches "centre off rocker switches Grnd digital inputs 2 , 3 , 9 ,10
2 = Drivers on (Don), 3 = Drivers Off (Doff), 9= Pas on (Pon), 10= Pass off(Poff)
If the Don button is pressed the heating goes to low, if the Don button is pressed again it goes to High, If the Don button is pressed again it goes back to low (caries on).
If the Doff button is pressed the heating goes to Off.
Pas side is separate but identical.
Originally this all worked perfectly, but if you kept the Don or Pon buttons pressed longer than the delay the program obviously just kept going low / high low / high.
The requirement is if the Don or Pon button is pressed and held in, it will only advance the count 1.
If the Doff or Poff button is pressed the The count will return to 1
I have tried to modify the program using the buttonstate functions but have had no success. any help would be appreciated.
const int Don = 2; //pin 2 as Don (Drivers Side On)
const int Doff = 3; //pin 3 as Doff (Drivers Side off)
const int Pon = 8; //pin 8 as Pon (Passengers Side on)
const int Poff = 9; //pin 9 as Poff (passengers Side Off)
const int DL = 7; //pin 7 as Drivers Side Low, Relay 1
const int DH = 6; //pin 6 as Drivers Side High, Relay 2
const int PL = 5; //pin 5 as Passengers Side Low, Relay 3
const int PH = 4; //pin 4 as Passenger Side High, Relay 4
int countD = 1; //Set CountD to "1"
int buttonStateD = 0;
int lastButtonStateD = 0;
int countP = 1; //Set CountP to "1"
int buttonStateP = 0;
int lastButtonStateP = 0;
void setup()
{
pinMode(Don,INPUT); //Don as INPUT to Arduino
pinMode(Doff,INPUT); //Doff as INPUT to Arduino
pinMode(Pon,INPUT); //Pon as INPUT to Arduino
pinMode(Poff,INPUT); //Poff as Input to Arduino
pinMode(DL,OUTPUT); //DL as Output to relay 1
pinMode(DH,OUTPUT); //DH as Output to relay 2
pinMode(PL,OUTPUT); //PL as Output to relay 3
pinMode(PH,OUTPUT); //DH as Output to relay 4
digitalWrite(Don,HIGH); // Use internal Pull up to hold input high
digitalWrite(Doff,HIGH);
digitalWrite(Pon,HIGH);
digitalWrite(Poff,HIGH);
Serial.begin(9600);
Serial.println("Heated Seat Control"); // Send "Heated Seat Control" to serial monitor
}
void loop()
{
buttonStateD = digitalRead(Don);
if(buttonStateD != lastButtonStateD)
{
if(buttonStateD == LOW)
{
countD++; // Increment CountD by 1
if(countD >3) // if count goes higher than 3
countD=2; // limit count to max of 3
Serial.print("number of Drivers button pushes:");
Serial.println(countD);
}
delay(50); // delay to avoid bounce
}
if(digitalRead(Doff) == LOW)
{
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
delay(50);
}
buttonStateP = digitalRead(Pon);
if(buttonStateP != lastButtonStateP)
{
if(buttonStateP == LOW)
{
countP++; // Increment CountD by 1
if(countP >3) // if count goes higher than 3
countP=2; // limit count to max of 3
Serial.print("number of Passengers button pushes:");
Serial.println(countP);
}
delay(50);
}
if(digitalRead(Poff) == LOW)
{ // 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
delay(100);
} // close loop
switch (countD) // start Drivers switch routine
{
case 1: // If countD = 1 do this
digitalWrite(DL,LOW); // switch the digital pin DL low ( this insures the low seat heating relay is off)
digitalWrite(DH,LOW); // switch the digital pin DH low ( this insures the high seat heating relay is off)
delay(100); // Pause for 100 milliseconds
break; // Close 1st case statement
case 2: // If countD = 2 do this
digitalWrite(DL,HIGH); // Switch the digital pin DL high (Turns on the drivers side low relay)
digitalWrite(DH,LOW); // Switch the digital pin DL Low (Turns off the drivers side high relay)
delay(100); // Pause for 100 milliseconds
break; // Close 2nd case statement
case 3: // If countD = 3 do this
digitalWrite(DL,LOW); // Switch the digital pin DL Low (Turns off the drivers side low relay)
digitalWrite(DH,HIGH); // Switch the digital pin DH High (Turns on the drivers side high relay)
delay(100); // Pause for 100 milliseconds
break; // Close 3rd case statement
} // Close switch routine
switch (countP) // start Passengers switch routine
{ // Open 1st case statement
case 1: // If countP = 1 do this
digitalWrite(PL,LOW); // switch the digital pin DL low ( this insures the low seat heating relay is off)
digitalWrite(PH,LOW); // switch the digital pin DH low ( this insures the high seat heating relay is off)
delay(100); // Pause for 100 milliseconds
break; // Close 1st case statement
case 2: // If countP = 2 do this
digitalWrite(PL,HIGH); // Switch the digital pin DL high (Turns on the passenger side low relay)
digitalWrite(PH,LOW); // Switch the digital pin DL Low (Turns off the passenger side high relay)
delay(100); // Pause for 100 milliseconds
break; // Close 2nd case statement
case 3: // If countP = 3 do this
digitalWrite(PL,LOW); // Switch the digital pin PL Low (Turns off the Passenger side low relay)
digitalWrite(PH,HIGH); // Switch the digital pin PH High (Turns on the passenger side high relay)
delay(100); // Pause for 100 milliseconds
break; // Close 3rd case statement
} // Close switch routine
} // Close Main loop