I've just started with an Arduino Uno V3 and a Playing With Fusion CAN bus Interface and a generic PCB shield for connecting resistors and input/output wires. I'm trying to adapt heated/cooled seats from a CAN bus vehicle into an older car.
I have 4 seat buttons (Driver Heat, Driver Cool, Passenger Heat, Passenger Cool) and there are 5 LED for both Driver and Passenger side. I'm using the Uno to read and count button presses (state ranges from 0-3, 0=off, 1=low heat/cool, 2=med heat/cool, 3=high heat/cool). Based on the state, different combinations of LED's in the switches will be HIGH, if state=0 then all LED are LOW.
Besides being more than a decade since I've really programmed or worked on any type of controller, the biggest problems I'm facing are:
- Pause or delay to prevent inadvertent button presses. It's real easy to get 2 presses when I only wanted 1.
- Canceling the opposite heat/cool when the other button is pressed. E.g., I want to change the driver's seat from hot to cold with a single button press instead of possibly having both hot and cold activated at the same time.
- Sniffing out the CAN bus messages from the original vehicle.
- Sending the correct CAN bus message for the selected switch-state.
I'll paste in my code as I go. I'm open to suggestions and am searching/reading/watching what I can. As you can see below, I only have the basics for turning on the 4 LEDs for a single switch. Good news is that the driver and passenger side switches don't interact. Just 2 switches working on each other to change the state.
// Pins for DH = Driver Heat, PH = Passenger Heat, DC = Driver Cool, PC = Passenger Cool
// swt = switch
int DH_swt = 0;
int DC_swt = 1;
int DH_LED = 2; //resistors added between all LED pins and wires using proto-board
int D_L1_LED = 3;
int D_L2_LED = 4;
int DC_LED = 5;
int D_L3_LED = 6;
int PH_swt = 7;
int PC_swt = 8;
int PH_LED = 9;
int P_L1_LED = 10;
int P_L2_LED = 11;
int PC_LED = 12;
int P_L3_LED = 13;
// variables to hold the switch states
boolean newDHSwitchState = LOW;
boolean oldDHSwitchState = LOW;
boolean newDCSwitchState = LOW;
boolean oldDCSwitchState = LOW;
boolean newPHSwitchState = LOW;
boolean oldPHSwitchState = LOW;
boolean newPCSwitchState = LOW;
boolean oldPCSwitchState = LOW;
// setting the switch states to 4 at the start due to initial switch trigger during system start.
// this starts all heat/cool in off condition.
byte DHswstate = 4;
byte DCswstate = 4;
byte PHswstate = 4;
byte PCswstate = 4;
// setting up the inputs/outputs and setting outputs to low
void setup()
{
pinMode(DH_swt, INPUT_PULLUP);
pinMode(DC_swt, INPUT_PULLUP);
pinMode(DH_LED, OUTPUT); digitalWrite(DH_LED, LOW);
pinMode(DC_LED, OUTPUT); digitalWrite(DC_LED, LOW);
pinMode(D_L1_LED, OUTPUT); digitalWrite(D_L1_LED, LOW);
pinMode(D_L2_LED, OUTPUT); digitalWrite(D_L2_LED, LOW);
pinMode(D_L3_LED, OUTPUT); digitalWrite(D_L3_LED, LOW);
pinMode(PH_swt, INPUT_PULLUP);
pinMode(PC_swt, INPUT_PULLUP);
pinMode(PH_LED, OUTPUT); digitalWrite(PH_LED, LOW);
pinMode(PC_LED, OUTPUT); digitalWrite(PC_LED, LOW);
pinMode(P_L1_LED, OUTPUT); digitalWrite(P_L1_LED, LOW);
pinMode(P_L2_LED, OUTPUT); digitalWrite(P_L2_LED, LOW);
pinMode(P_L3_LED, OUTPUT); digitalWrite(P_L3_LED, LOW);
}
void loop()
{
// Driver Side
// Driver Heat Switch
newDHSwitchState = digitalRead(DH_swt);
if (newDHSwitchState != oldDHSwitchState)
{
// has the switch been closed?
if ( newDHSwitchState == HIGH )
{
// increments the value
DHswstate++;
if (DHswstate > 3) {
DHswstate = 0;
}
// turn all of the LEDs off.
digitalWrite(DH_LED, LOW);
digitalWrite(DC_LED, LOW);
digitalWrite(D_L1_LED, LOW);
digitalWrite(D_L2_LED, LOW);
digitalWrite(D_L3_LED, LOW);
// SEND CAN bus DH OFF Message here.
// Turn on the DH indicator LED and PowerLevel 1 LED.
if (DHswstate == 1) {
digitalWrite(DH_LED, HIGH);
digitalWrite(D_L1_LED, HIGH);
digitalWrite(DC_LED, LOW);
// SEND CAN bus DH Lvl 1 Message here.
DCswstate = 0;
delay(50);
}
if (DHswstate == 2) {
digitalWrite(DH_LED, HIGH);
digitalWrite(D_L1_LED, HIGH);
digitalWrite(D_L2_LED, HIGH);
digitalWrite(DC_LED, LOW);
// SEND CAN bus DH Lvl 2 Message here.
DCswstate = 0;
delay(50);
}
if (DHswstate == 3) {
digitalWrite(DH_LED, HIGH);
digitalWrite(D_L1_LED, HIGH);
digitalWrite(D_L2_LED, HIGH);
digitalWrite(D_L3_LED, HIGH);
digitalWrite(DC_LED, LOW);
// SEND CAN bus DH Lvl 3 Message here.
DCswstate = 0;
delay(50);
}
}
oldDHSwitchState = newDHSwitchState;
}
// Driver Cool Switch
newDCSwitchState = digitalRead(DC_swt);
if (newDCSwitchState != oldDCSwitchState)
{
// has the switch been closed?
if ( newDCSwitchState == HIGH )
{
// increments the value
DCswstate++;
if (DCswstate > 3) {
DCswstate = 0;
}
// Turn everything off.
digitalWrite(DC_LED, LOW);
digitalWrite(DH_LED, LOW);
digitalWrite(D_L1_LED, LOW);
digitalWrite(D_L2_LED, LOW);
digitalWrite(D_L3_LED, LOW);
// SEND CAN bus DC OFF Message here.
// Turn on the DH indicator LED and PowerLevel 1 LED.
if (DCswstate == 1) {
digitalWrite(DC_LED, HIGH);
digitalWrite(D_L1_LED, HIGH);
digitalWrite(DH_LED, LOW);
// SEND CAN bus DC Lvl 1 Message here.
DHswstate = 0;
delay(50);
}
if (DCswstate == 2) {
digitalWrite(DC_LED, HIGH);
digitalWrite(D_L1_LED, HIGH);
digitalWrite(D_L2_LED, HIGH);
digitalWrite(DH_LED, LOW);
// SEND CAN bus DC Lvl 2 Message here.
DHswstate = 0;
delay(50);
}
if (DCswstate == 3) {
digitalWrite(DC_LED, HIGH);
digitalWrite(D_L1_LED, HIGH);
digitalWrite(D_L2_LED, HIGH);
digitalWrite(D_L3_LED, HIGH);
digitalWrite(DH_LED, LOW);
// SEND CAN bus DC Lvl 3 Message here.
DHswstate = 0;
delay(50);
}
}
oldDCSwitchState = newDCSwitchState;
}
// Passenger Side
// Passenger Heat Switch
newPHSwitchState = digitalRead(PH_swt);
if (newPHSwitchState != oldPHSwitchState)
{
// has the switch been closed?
if ( newPHSwitchState == HIGH )
{
// increment the value of state
PHswstate++;
if (PHswstate > 3) {
PHswstate = 0;
}
// turn everything off.
digitalWrite(PH_LED, LOW);
digitalWrite(PC_LED, LOW);
digitalWrite(P_L1_LED, LOW);
digitalWrite(P_L2_LED, LOW);
digitalWrite(P_L3_LED, LOW);
// SEND CAN bus PH OFF Message here.
// Turn on the DH indicator LED and PowerLevel 1 LED.
if (PHswstate == 1) {
digitalWrite(PH_LED, HIGH);
digitalWrite(P_L1_LED, HIGH);
digitalWrite(PC_LED, LOW);
// SEND CAN bus PH Lvl 1 Message here.
PCswstate = 0;
delay(50);
}
if (PHswstate == 2) {
digitalWrite(PH_LED, HIGH);
digitalWrite(P_L1_LED, HIGH);
digitalWrite(P_L2_LED, HIGH);
digitalWrite(PC_LED, LOW);
// SEND CAN bus PH Lvl 2 Message here.
PCswstate = 0;
delay(50);
}
if (PHswstate == 3) {
digitalWrite(PH_LED, HIGH);
digitalWrite(P_L1_LED, HIGH);
digitalWrite(P_L2_LED, HIGH);
digitalWrite(P_L3_LED, HIGH);
digitalWrite(PC_LED, LOW);
// SEND CAN bus PH Lvl 3 Message here.
PCswstate = 0;
delay(50);
}
}
oldPHSwitchState = newPHSwitchState;
}
// Passenger Cool Switch
newPCSwitchState = digitalRead(PC_swt);
if (newPCSwitchState != oldPCSwitchState)
{
// has the switch been closed?
if ( newPCSwitchState == HIGH )
{
// incremement the value of state
PCswstate++;
if (PCswstate > 3) {
PCswstate = 0;
}
// turn everything off.
digitalWrite(PC_LED, LOW);
digitalWrite(PH_LED, LOW);
digitalWrite(P_L1_LED, LOW);
digitalWrite(P_L2_LED, LOW);
digitalWrite(P_L3_LED, LOW);
// SEND CAN bus PC OFF Message here.
// Turn on the DH indicator LED and PowerLevel 1 LED.
if (PCswstate == 1) {
digitalWrite(PC_LED, HIGH);
digitalWrite(P_L1_LED, HIGH);
digitalWrite(PH_LED, LOW);
// SEND CAN bus PC Lvl 1 Message here.
PHswstate = 0;
delay(50);
}
if (PCswstate == 2) {
digitalWrite(PC_LED, HIGH);
digitalWrite(P_L1_LED, HIGH);
digitalWrite(P_L2_LED, HIGH);
digitalWrite(PH_LED, LOW);
// SEND CAN bus PC Lvl 2 Message here.
PHswstate = 0;
delay(50);
}
if (PCswstate == 3) {
digitalWrite(PC_LED, HIGH);
digitalWrite(P_L1_LED, HIGH);
digitalWrite(P_L2_LED, HIGH);
digitalWrite(P_L3_LED, HIGH);
digitalWrite(PH_LED, LOW);
// SEND CAN bus PC Lvl 3 Message here.
PHswstate = 0;
delay(50);
}
}
oldPCSwitchState = newPCSwitchState;
}
}