If statement, relay on and off won't work

Hey guys I'm having a bit of a trouble with my code. I can't figure out why the first if statement in my code below is not working correctly. I've tried to make it a while statement but that didn't work either.

I have a pro mini 5v controlling a 4, relay break out board.

The first if statement is suppose to read a voltage on pin A1 and and if it exceeds the threshold, it turns the relay on for 2.5 seconds, then off after that.

Another question I have is how to make that if statement only run once? To give a little background....I'm using this relay as a turn on controller for a carpc. The WL trigger is actually my interior lighting. I plan on tapping 12v off an interior light, then using a 12v-5v invertor for say a cell phone to step the voltage down for use with the arduino pin A1. I'm hoping this will allow me to use something like this:

for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=10) //WL fade speed on
analogWrite(AEDrivers, fadeValue);

to fade the drivers for my LED angel eyes on for welcome lighting. So I'd like to control the drivers as well as the relay with the if statement or another type of statement. But the relay only needs to run once per, say 5-10 minutes.

Any help is appreciated. Thanks in advanced!

const int IGN = A1; //Ingition trigger for AE DRL/CarPC Charger
const int WL = A3; //Interior lighting trigger for welcome lighting
const int RELAY1 = 6; //CarPC Start, hold 2.5 seconds
const int RELAY2 = 7; //CarPC Charger
const int RELAY3 = 8; //AE 12v supply
//Pin to control AE Drivers
//Threshold for triggers

void setup()
{
//Alters PWM Frequency
pinMode(IGN, INPUT);
pinMode(WL, INPUT);
pinMode(RELAY1, OUTPUT); //Sets assigned pins as outputs
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
digitalWrite(RELAY1, HIGH); //Sets relays to off on startup
digitalWrite(RELAY2, HIGH);
digitalWrite(RELAY3, HIGH);
}

void loop() //Relay Control Program
{
int WLValue = analogRead(WL); //Reads WL Voltage
int IGNValue = analogRead(IGN); //Reads IGN voltage
int threshold = 400;

if (WLValue > threshold)
{
digitalWrite(RELAY1, LOW);
delay(2500);
digitalWrite(RELAY1, HIGH);
}
if (IGNValue > threshold)
{
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
delay(1000);
}
else (IGNValue < threshold);
{
digitalWrite(RELAY2, HIGH);
digitalWrite(RELAY3, HIGH);
}
}

take a look at this:

it will cycle your relays every 5 mins (set at the top) without the use of blocking delay()s which will make your sketch more scalable. I added the LED to toggle along with the first relay so you can do both relays in one function.

if you want to add other things to your loop() function, add it where I identified...

untested

#define RESET_RELAYS_MINS 5 // set to cycle relays every 5 minutes

const int IGN = A1;            //Ingition trigger for AE DRL/CarPC Charger
const int WL = A3;             //Interior lighting trigger for welcome lighting
const int RELAY1 = 6;          //CarPC Start, hold 2.5 seconds
const int RELAY2 = 7;          //CarPC Charger
const int RELAY3 = 8;          //AE 12v supply
const int RELAY4 = 13;  //unused relay using pin13 the on-board LED
int threshold = 400; //Threshold for triggers
unsigned long start1Time, start2Time, resetRelayTime;
unsigned long relayInterval = 60000UL * RESET_RELAYS_MINS;
boolean firstDone, secondDone;
boolean readSensors = true;
int WLValue, IGNValue;
//
void setup()
{             //Alters PWM Frequency
   pinMode(RELAY1, OUTPUT);                           //Sets assigned pins as outputs
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  digitalWrite(RELAY1, HIGH);                        //Sets relays to off on startup >>> must be active LOW, correct?
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, HIGH);//LED
}
//
void loop()       //Relay Control Program
{
  if (readSensors)
  {
    WLValue = analogRead(WL);                      //Reads WL Voltage
    IGNValue = analogRead(IGN);                    //Reads IGN voltage
    Serial.print(F("WLvalue= "));
    Serial.println(WLValue);
    Serial.print(F("IGNvalue= "));
    Serial.println(IGNValue);
    readSensors = false;
  }
  if (WLValue >= threshold)
  {
    if (!firstDone)
    {
      start1Time = millis();
      firstDone = true;
    }
    relayOn(RELAY1, RELAY4, start1Time, 2500UL);
  }
  if (IGNValue >= threshold)
  {
    if (!secondDone)
    {
      start2Time = millis();
      secondDone = true;
    }
    relayOn(RELAY2, RELAY3, start2Time, 1000UL);
  }
  if (millis() - resetRelayTime >= relayInterval)
  {
    resetRelayTime += relayInterval;
    firstDone = false;
    secondDone = false;
    readSensors = true;
    
  }
  //
  // Rest of your sketch here...
  //
}

void relayOn(int relayOne, int relayTwo, unsigned long myStart, unsigned long duration)
{
  if (millis() - myStart < duration)
  {
    digitalWrite(relayOne, LOW);
    digitalWrite(relayTwo, LOW);
  }
  else 
  {
    digitalWrite(relayOne, HIGH);
    digitalWrite(relayTwo, HIGH);
  }
}
  pinMode(IGN, INPUT);
  pinMode(WL, INPUT);

The pinMode() function sets the status of the digital pins. It does NOTHING for analog pins. Since you are not using the pins as digital pins, the pinMode() call is useless.

  int WLValue = analogRead(WL);                      //Reads WL Voltage

And what value did you get? Why don't you know?

Another question I have is how to make that if statement only run once?

You can embed it inside another if statement:

bool beenThereDoneThat = false; // global
if(!beenThereDoneThat)
{
    if(WLValue > threshold)
    {
       // Do whatever
       beenThereDoneThat = true;
    }
}

Thank you for the replies.

I will comb through them and see if I can my code working the way I want.

I'll post back with my results.

PaulS:

  pinMode(IGN, INPUT);

pinMode(WL, INPUT);



The pinMode() function sets the status of the digital pins. It does NOTHING for analog pins. Since you are not using the pins as digital pins, the pinMode() call is useless.



int WLValue = analogRead(WL);                      //Reads WL Voltage



And what value did you get? Why don't you know?



> Another question I have is how to make that if statement only run once?


You can embed it inside another if statement:



bool beenThereDoneThat = false; // global






if(!beenThereDoneThat)
{
    if(WLValue > threshold)
    {
       // Do whatever
       beenThereDoneThat = true;
    }
}

I had a feeling that setting the analog pins that way did nothing but I wasn't sure and it pasted verification so I didn't think anything of it. Thanks for pointing that out.

I'm not sure what value I get what I analogRead a pin. I'm assuming it will be 1023 for 5v and close to 0 for 0v. I need to install the wiring in my car to actually test the circuit I think