Hi all,
After a little help with a piece of code I have written that's already working in one way, I'm modifying it to add a new feature.
Essentially, the code sits on an Arduino Pro Mini 5.5v and installs into a Nissan 350 or 370z and when a button is pushed in the car, it disabled the Traction control and Vehicle Dynamic control systems to enable you to push the limits on a track day without traction control getting in the way.
This is all done by simply "faulting" the traction control system by disconnecting its ground pin for a second, then reconnecting it using the relay on a PCB I have designed.
I now want to add a "Brake Boost" option of this, which I have created the PCB mods for and written most of the extra code to control the second relay. However if my system is "armed" the code also needs to look out for a brake input signal and de-activate the Brake_Control_Relay for a second, then activate it again.
On the line that starts with else if (button_state == 0)
This is the part when the "enable" button is pressed, the system sees the module was previously disabled (State 0) then sets the State to 1 (enabled) and activates both of the the relays.
However, in this section, how would I then go about both listening for a new button press (the code already handles this with the "waitButtonPress" void ) but ALSO looking for a high input on pin 7, this is the signal from the car the brake has been pressed. If the module sees this, it needs to de-activate the BRAKE_CONTROL_RELAY again for 1 second, then activate it again.
This looking for the extra input should only happen when the "button_state" is 1 (system is enabled) when the button state is 0, it just needs to sit and wait for a new button press as it already does.
#include <EEPROM.h>
// Pin Definitions
#define BUTTON_PIN A0
#define VDC_RELAY_PIN 6
#define LED_PIN A1
#define BRAKE_SENSE 7
#define BRAKE_RELAY_PIN 5
// 1 for debugging mode, 0 for turning it off
#define DEBUG 1
bool button_state = 0;
int address = 0;
void setup()
{
// initialize pins
pinMode(VDC_RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BRAKE_RELAY_PIN, OUTPUT);
pinMode(BRAKE_SENSE, INPUT);
// initialize serial for debugging
Serial.begin(9600);
// read the last button state from the memory for VDC Status
button_state = EEPROM.read(address);
#if DEBUG == 1
Serial.print("Button state read from the memory: ");
Serial.println(button_state);
#endif
}
void loop()
{
// saved state is 1
if (button_state == 1)
{
// activate the relays
VDCrelayControl();
BRAKErelayControl();
//turn off led
digitalWrite(LED_PIN, LOW);
}
// saved state is 0
else if (button_state == 0)
{
// do nothing with the relay
//turn on the LED
digitalWrite(LED_PIN, HIGH);
}
// wait for button press
waitButtonPress();
// saved state was 1
if (button_state == 1)
{
// set state to 0 system is now safe and LED illuminated to show VDC can be disabled again and brakes are at OEM
button_state = 0;
digitalWrite(LED_PIN, HIGH);
// save the state in memory
EEPROM.write(address, button_state);
#if DEBUG == 1
Serial.print("new Button state read from the memory: ");
Serial.println(button_state);
#endif
}
// saved state was 0
else if (button_state == 0)
{
// set state to 1 system is now disabled and LED turned off to show VDC is disabled and brake boost mod is waiting
button_state = 1;
digitalWrite(LED_PIN, LOW);
// save the state in memory
EEPROM.write(address, button_state);
#if DEBUG == 1
Serial.print("new Button state read from the memory: ");
Serial.println(button_state);
#endif
// activate the relays
VDCrelayControl();
BRAKErelayControl();
}
}
// Latches, waits and unlatches the VDC relay
void VDCrelayControl()
{
// latch the relay
digitalWrite(VDC_RELAY_PIN, 1);
// wait for 1 second
delay(1000);
// un-latch the relay
digitalWrite(VDC_RELAY_PIN, 0);
Serial.println("VDC Latch Enabled and System Faulted ");
}
// Latches, the brake relay
void BRAKErelayControl()
{
// latch the relay
digitalWrite(BRAKE_RELAY_PIN, 1);
Serial.println("BRAKE Latch Enabled ");
}
// Pull-down resistor is connected
// When button is not pressed, 0 volts should be read.
void waitButtonPress()
{
// wait until button is pressed
while (digitalRead(BUTTON_PIN) == LOW);
#if DEBUG == 1
Serial.println("Button has been pressed.");
#endif
// allow some time for debouncing
delay(1000);
}
Im not the best at Arduino code, so im trying to pick up things as I go. Its taken me a long while to get the code to a working state, but as I say, doing a loop, whilst waiting for another input has got me confused a little!
Appreciate any replies or advice