First time poster, long time reader, and let me start by saying I'm not much of a programmer. I've fought my way through learning some simple functions, I've been working on project with a Mega 2560 that i'm putting in a car project to act like a Body Control Module "BCM" to control headlights, wipers, door locks maybe down the road Cruise control.
I've been testing code as I go, and I've ran into an issue that I can't seem to figure out why the controller want to do what it is doing.
the section of code I'm having an issue is the Mood lighting im dimming the lights with two push buttons on analog input with resistive values. One button press will add 50 PWM count the other button press will reduce 50 PWM count. The PWM starts at 100 when the program starts. The issue im having the down button push only works one time. The up-button press seems to work just fine.
The second problem im still working on is i would like to turn on the dome light by having the PWM count to 250 and hold down the up button for 5 seconds to turn on the dome light. Currently dome light comes on with the count get to 250.
#define WiperLS 10
#define WiperHS 11
#define WasherPump 12
#define WiperSW A1
#define HeadlightDim A2
#define MoodLight 9
#define DomeLight 8
#define DoorSW 19
#define LineLOC 12
#define RFID
#define DoorLock 13
#define DoorUnlock 14
#define DoorLockSW 15
#define DoorUnlockSW 16
#define TrunkOpener 17
#define HeartBeat 18
//***************************************************Wiper Contorls and timer**************************************************************
bool WashCycle = LOW;
const int WiperSWLS = 1;
const int WiperSWHS = 4;
const int WiperSWWash = 5;
const int Val = 15;
const long WasherTimer = 5000;
unsigned long LastWasherPush = 0;
int WiperSWVal = 0;
int CurrentWasherPush = 0;
//***************************************************Cruise Contorl ***********************************************************************
const int CruiseONOFF = 1023;
const int MPHUPSet = 6;
const int MPHDW = 9;
const int Abort = 10;
int CurrentCCON = 0;
int LASTCCON = 0;
//**************************************************Headlight Contorl**********************************************************************
const int DimmerSWUP = 1023;
const int DimmerSWDW = 435;
const long DomeLightTimer = 5000;
int DimmerStateVal = 0;
int CurrentDimmerSWUP = 0;
int LASTDimmerSWUp = 0;
int CurrentDimmerSWDW = 0;
int LASTDimmerSWDW = 0;
int count = 100;
unsigned long CurrentDimmerSWState = 0;
unsigned long LastDimmerState= 0;
//************************************************Door Lock contorls*********************************************************************
void setup() {
pinMode(WiperLS, OUTPUT);
pinMode(WiperHS, OUTPUT);
pinMode(WiperSW, INPUT);
pinMode(WasherPump, OUTPUT);
pinMode(HeadlightDim, INPUT);
pinMode(MoodLight, OUTPUT);
pinMode(DomeLight, OUTPUT);
pinMode(DoorLock, OUTPUT);
pinMode(DoorUnlock, OUTPUT);
pinMode(DoorLockSW, INPUT);
pinMode(DoorUnlockSW, INPUT);
pinMode(TrunkOpener, OUTPUT);
pinMode(HeartBeat, OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//*****************************************************Wiper Switch control******************************************************************
int WiperSWVal = analogRead(WiperSW);
if (WiperSWVal < (WiperSWLS + Val) && WiperSWVal > (WiperSWLS - Val)) {
digitalWrite(WiperLS, HIGH);
} else {
digitalWrite(WiperLS, LOW);
}
if (WiperSWVal < (WiperSWHS + Val) && WiperSWVal > (WiperSWHS - Val)) {
digitalWrite(WiperHS, HIGH);
} else {
digitalWrite(WiperHS, LOW);
}
//***************************************************Washer Cycle and Timer********************************************************************
unsigned long CurrentWasherPush = millis();
if (WiperSWVal < (WasherPump + Val) && WiperSWVal > (WasherPump - Val)) {
LastWasherPush = millis();
digitalWrite(WiperLS, HIGH);
digitalWrite(WasherPump, HIGH);
}
if (CurrentWasherPush - LastWasherPush <= WasherTimer) {
LastWasherPush = CurrentWasherPush;
digitalWrite(WiperLS, HIGH);
} else {
digitalWrite(WiperLS, LOW);
}
//******************************************************MoodLighting*******************************************************************************
int DimmerStateVal = analogRead(A2);
Serial.println(count);
Serial.println(DimmerStateVal);
if (DimmerStateVal < (DimmerSWUP + Val) && DimmerStateVal > (DimmerSWUP - Val)) {
CurrentDimmerSWUP ++;
}
if (DimmerStateVal < (DimmerSWDW + Val) && DimmerStateVal > (DimmerSWDW - Val)) {
CurrentDimmerSWDW ++;
}
if (CurrentDimmerSWUP != LASTDimmerSWUp) {
if (CurrentDimmerSWUP == HIGH) {
if (count < 249) {
count = count + 50;
analogWrite(MoodLight, count);
delay(50);
}
}
LASTDimmerSWUp = CurrentDimmerSWUP;
}
if (CurrentDimmerSWDW != LASTDimmerSWDW) {
if (CurrentDimmerSWDW == HIGH) {
if (count > 51) {
count = count - 50;
analogWrite(MoodLight, count);
delay(50);
}
}
LASTDimmerSWDW = CurrentDimmerSWDW;
}
int CurrentDimmerSWState= millis();
if(CurrentDimmerSWUP=HIGH &&(count==250)){
LastDimmerState=millis();
if(CurrentDimmerSWState-LastDimmerState<=DomeLightTimer){
digitalWrite(DomeLight,HIGH);
LastDimmerState=CurrentDimmerSWState;
}
}
else {
digitalWrite(DomeLight,LOW);
}
//***********************************************DomeLight control**************************************************************************
int DoorSwitch=digitalRead(DoorSW);
if(DoorSwitch=HIGH){
digitalWrite(DomeLight,LOW);
}
else
{digitalWrite(DomeLight,LOW);
}
}
any help would be apparated.