Coding Question

My code will not recognize the second part of the "AND" condition.
My "light" is greater than 200 and my "volt" is greater than 820 but digitalWrite 7 (relay) will not turn on.
Only the LED turns on.
I have verified this via the serial monitor.

I have listed the faulty code below:

int light;
int volt;

void setup()
{
pinMode(13, OUTPUT); // put your setup code here, to run once:
pinMode(7, OUTPUT);
Serial.begin(9600);
}

void loop()
{
light = analogRead(A0); // put your main code here, to run repeatedly:
volt = analogRead (A4);

//*********************
if (light > 200 && volt > 820)
{
digitalWrite(13, HIGH); // LED on:
digitalWrite(7, HIGH); // Relay on:
}

else
{
digitalWrite(13, LOW); //LED off:
}

//*********************
if (volt < 660)
{
digitalWrite(7, LOW); //Relay off:
}

else
{
digitalWrite(7, HIGH); //Relay on:
}

//*********************
Serial.println(volt);
delay(100);

} //END of loop()

if (light > 200 && volt > 820)
{
. . .
}

if (volt < 660)
{
digitalWrite(7, LOW); //Relay off:
}

else <———<<<<< what does this do ?
{
digitalWrite(7, HIGH); //Relay on:
}

Are you using a relay module? Most are active-LOW (you set the pin LOW to turn the relay ON).

Hi Larryd,

"else <------<<<<< what does this do ?"

I used this to keep the relay looping high until my battery voltage drops below 660 then I will implement a delay to allow for battery recharge.
I just don't want the loop to stop until the voltage actually drops below 660.

Also I tried compiling your code and it stops at the ". . ." and an error message says "expected primary-expression before '.' token"

Thanks,

Hi Johnwasser,

I am using a relay shield without an external 5V power supply. The digital out high is providing the power to close the relay straight from the arduino.

Thanks,

arduino_user789:
Hi Johnwasser,

I am using a relay shield without an external 5V power supply. The digital out high is providing the power to close the relay straight from the arduino.

Thanks,

So you have a special low-power 5V relay where the coil current is well below the 40mA "Absolute Maximum" of the Arduino pin?

So when volt >= 660

do you realize pin 7 will be always HIGH

else
{
digitalWrite(7, HIGH); //Relay on:
}

Johnwasser,

You were correct! The digital low did activate the relay! I just assumed it was a high that activated it.
Thanks for asking the right question.

Larryd,

Yes I realize that now. Thanks for the input.

I am good to run this code now. Hopefully all goes well.