Hi everybody,
This is my first time posting here, so I hope that everything is placed correct.
For a school project I am working on a Porsche that is being built to racing spec. To accomodate for easy use of flash to pass and patterns in hazard lights, I thought it would be nice to use an Arduino for all of this.
The Arduino should be able to switch the low and high beams on, flash the high beams several times when a specific button is pressed, activate the turn signals and switch the rainlight on the back of the vehicle.
For this, I wanted to use an Arduino Uno, combined with a MOSFET shield from Sparkfun (6 predefined MOSFET outputs). To show what is activated, I wanted to use an 16x2 LCD which I had at home, this isn't implemented in the code yet. The switches are connected to GND with the internal pull-up resistors enabled.
This is just the first part of the code, the things I have until now. When I wanted to test the code, halfway through programming the functions, I got no response at all when pushing the buttons.
What should be happening is when the LowBeamSwitch is pushed, the low beams should turn on, when the HighBeamSwitch button is pressed, the high beams should turn on and when the FlashToPassSwitch button is pressed, the high beams (and if not active already, the low beams as well) should flash 3 times. In order to not let this interfere with the regular highbeam and lowbeam functions, I set flags so they couldn't interfere.
I ran over my code several times now, but I still am unable to find the error in my code. I guess it will be in my switch debounce part, but to me it looks alright.
I would greatly appreciate any help!
Yours Sincerely,
Joël
// For programming pins 0 and 1 should be disconnected
// Pin definitions for switches
const byte LowBeamSwitch = 0;
const byte HighBeamSwitch = 1;
const byte FlashToPassSwitch = 2;
// Pin definitions for MOSFET's
const byte LowBeam = 3;
const byte HighBeam = 5;
// Constants
const int ShortFlash = 125; // Indicates the time [ms] needed for a short flash, as used in Hazard mode
const int LongFlash = 250; // Indicates the time [ms] needed for a long flash, as used in hazard mode and turn signals
const byte FlashToPassFlashes = 3; // Indicates how many flashes are done when the flash to pass button is pressed.
const int DebounceDelay = 50; // The debounce time when a switch is activated
// Variables for debouncing
byte LowBeamSwitchState; // The current state of the switch
byte HighBeamSwitchState;
byte FlashToPassSwitchState;
byte LastLowBeamSwitchState = 1; // The previous state of the switch (HIGH/LOW)
byte LastHighBeamSwitchState = 1;
byte LastFlashToPassSwitchState = 1;
unsigned long LastDebounceTimeLowBeam = 0; //Reference for measuring debounce, initialised at 0
unsigned long LastDebounceTimeHighBeam = 0;
unsigned long LastDebounceTimeFlashToPass = 0;
// Variables for millis delay
unsigned long PreviousMillisLowBeam = 0; // Will store last time MOSFET was updated
unsigned long PreviousMillisHighBeam = 0;
unsigned long PreviousMillisFlashToPass = 0;
// Variables for toggling outputs
byte LowBeamState = 0;
byte HighBeamState = 0;
byte FlashToPassFlag = LOW; // When this flag is toggled, the flash to pass loop is active and the high beam loop is interrupted
byte LowBeamFlag = LOW; // When this flag is toggled, the LowBeam loop is active and LowBeam isn't included in flash to pass
int FlashToPassCounter = 0;
// Include the lcd library code
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);
void setup() {
// Initialise switches as inputs with internal pull-up resistors enabled
pinMode (LowBeamSwitch, INPUT_PULLUP);
pinMode (HighBeamSwitch, INPUT_PULLUP);
pinMode (FlashToPassSwitch, INPUT_PULLUP);
// Initialise MOSFET's as outputs
pinMode (LowBeam, OUTPUT);
pinMode (HighBeam, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
LowBeamLoop();
FlashToPassLoop();
HighBeamLoop();
}
//-----------------------------------------------------------------------------------------
void LowBeamLoop() {
if (digitalRead(LowBeamSwitch) != LastLowBeamSwitchState) { // If the switch changed, due to noise of switching
LastDebounceTimeLowBeam = millis(); // reset the debouncing timer
}
if ((millis() - LastDebounceTimeLowBeam) > DebounceDelay) {
// The debounce time has passed, so it is valid switching
if (digitalRead(LowBeamSwitch) != LowBeamSwitchState) {
LowBeamSwitchState = digitalRead(LowBeamSwitch);// reset the debouncing timer
if (LowBeamSwitchState != LastLowBeamSwitchState) { // If the switch has been switched
LowBeamState = !LowBeamState; // Toggle the Low Beam
digitalWrite(LowBeam, LowBeamState); // Output to MOSFET
LowBeamFlag = !LowBeamFlag; // Flip the LowBeam Flag
}
}
}
LastLowBeamSwitchState = digitalRead(LowBeamSwitch); // Set the last value to detect changes next time
}
//-----------------------------------------------------------------------------------------
void FlashToPassLoop() {
if (FlashToPassFlag == LOW) {
if (digitalRead(FlashToPassSwitch) != LastFlashToPassSwitchState) { // If the switch changed, due to noise of switching
LastDebounceTimeFlashToPass = millis(); // reset the debouncing timer
}
if ((millis() - LastDebounceTimeFlashToPass) > DebounceDelay) {
// The debounce time has passed, so it is valid switching
if (digitalRead(FlashToPassSwitch) != FlashToPassSwitchState) {
FlashToPassSwitchState = digitalRead(FlashToPassSwitch);// reset the debouncing timer
if (FlashToPassSwitchState != LastFlashToPassSwitchState) { // If the switch has been switched
FlashToPassFlag = !FlashToPassFlag; // Flip the Flash to pass Flag
}
}
}
LastFlashToPassSwitchState = digitalRead(FlashToPassSwitch); // Set the last value to detect changes next time
}
else {
if ((millis() - PreviousMillisFlashToPass) >= LongFlash) {
PreviousMillisFlashToPass = millis();
if (FlashToPassCounter >= 0 && FlashToPassCounter <= ((FlashToPassFlashes * 2) - 1)) {
FlashToPassCounter++; // Increment the flash to pass counter
HighBeamState = !HighBeamState; // Toggle the High Beam
digitalWrite(HighBeam, HighBeamState); // Output to MOSFET
if (LowBeamFlag == LOW) {
LowBeamState = !LowBeamState; // Toggle the Low Beam if it is not activated regular
digitalWrite(LowBeam, LowBeamState); // Output to MOSFET
}
}
else {
FlashToPassFlag = LOW; //Reset the flash to pass flag
}
}
}
}
//-----------------------------------------------------------------------------------------
void HighBeamLoop() {
if (FlashToPassFlag == LOW) {
if (digitalRead(HighBeamSwitch) != LastHighBeamSwitchState) { // If the switch changed, due to noise of switching
LastDebounceTimeHighBeam = millis(); // reset the debouncing timer
}
if ((millis() - LastDebounceTimeHighBeam) > DebounceDelay) {
// The debounce time has passed, so it is valid switching
if (digitalRead(HighBeamSwitch) != HighBeamSwitchState) {
HighBeamSwitchState = digitalRead(HighBeamSwitch);// reset the debouncing timer
if (HighBeamSwitchState != LastHighBeamSwitchState) { // If the switch has been switched
HighBeamState = !HighBeamState; // Toggle the High Beam
digitalWrite(HighBeam, HighBeamState); // Output to MOSFET
}
}
}
}
}
//-----------------------------------------------------------------------------------------