# boolean with else?

hello, trying to set up some indicator lights for a project… ex. if voltage range is between x and y, turn on green light, if its not turn on red light…

digitalWrite(8, HIGH);

digitalWrite(9, HIGH);

both lights stay on and dont go off, even if read voltage is 0

i did some ifs with an else… if voltage is > x, then light on
else light off

but thats not working well either, if you put in another set of statements for the upper end, it doesnt work either…

can cant get the && to work with else…

ie

digital write 9 high
else
digital write 8 high

how to do? thank you

``````if (analogRead(A3) < 80 && analogRead(A3) > 100);
``````

1 - lose the semi colon on the end otherwise that is all that will be executed of the test returns true and digitalWrite(8, HIGH); will always be executed.
2 - is analogRead(A3) ever going to be smaller than 80 and greater than 100 at the same time ?

DM2186:
hello, trying to set up some indicator lights for a project… ex. if voltage range is between x and y, turn on green light, if its not turn on red light…

digitalWrite(8, HIGH);

digitalWrite(9, HIGH);

both lights stay on and dont go off, even if read voltage is 0

i did some ifs with an else… if voltage is > x, then light on
else light off

but thats not working well either, if you put in another set of statements for the upper end, it doesnt work either…

can cant get the && to work with else…

ie

digital write 9 high
else
digital write 8 high

how to do? thank you

how can A3 be less than 80 and greater than 100 at the same time??? It should be || which means “or” … and you have to write one light high and the other light low…

``````const int lowLimit = 80;
const int highLimit = 100;
int voltage;

void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}//setup()

void loop(){
if(voltage<lowLimit)//voltage below lower limit
red();
else if(voltage<=highLimit)//now we know that voltage is between low and high
green();
else//now we know that voltage is above high
red();
}//loop()

void green(){
digitalWrite(8, 0);//red light off
digitalWrite(9, 1);//green light on
}//gren()

void red(){
digitalWrite(8, 1);//red light on
digitalWrite(9, 0);//green light off
}//red()
``````

UKHeliBob:

``````if (analogRead(A3) < 80 && analogRead(A3) > 100);
``````

1 - lose the semi colon on the end otherwise that is all that will be executed of the test returns true and digitalWrite(8, HIGH); will always be executed.
2 - is analogRead(A3) ever going to be smaller than 80 and greater than 100 at the same time ?

yup mess that up… basically i want the green light to come on between reading 80 to 100, and a red light to be on any other time… i added what nilton61 did to the rest of my code… now getting a bunch of { errors:

``````#include <LiquidCrystal.h>

// number of analog samples to take per reading, per channel
#define NUM_SAMPLES 10
// voltage divider calibration values
#define DIV_1    11.255
#define DIV_2    11.255

// ADC reference voltage / calibration value
#define V_REF    4.991

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sum[4] = {0};                // sums of samples taken
unsigned char sample_count = 0;  // current sample number
float voltage[4] = {0.0};        // calculated voltages
char l_cnt = 0;                  // used in 'for' loops
const int lowLimit = 80;
const int highLimit = 100;
int volt;

void setup()
{
lcd.begin(16, 2);

pinMode (2, INPUT);  // raw voltage input
pinMode (3, INPUT);  // raw voltage output

Serial.begin(9600);  // set up Serial library at 9600 bps, monitor

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);

void loop()
{
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES)

{
// sample each channel A2 to A5
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
}
sample_count++;
delay(50);
}

// calculate the voltage for each channel
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
}

// display voltages on LCD
// each voltage is multiplied by the resistor network
// division factor to calculate the actual voltage
// voltage 1 - A (pin A2)
lcd.setCursor(0, 0);
lcd.print("IN ");
lcd.print(voltage[0] * DIV_1, 1);
lcd.print("V ");

// voltage 2 - B (pin A3)
lcd.setCursor(8, 0);
lcd.print("OUT ");
lcd.print(voltage[1] * DIV_2, 1);
lcd.print("V ");

// voltge 3 - C (pin A4)
lcd.setCursor(0, 1);

lcd.print("STATUS: ");
// voltage 4 - D (pin A5)
lcd.setCursor(12, 1);
lcd.print("CAL!");

// reset count and sums
sample_count = 0;
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
sum[l_cnt] = 0;
}

//INDICATOR LIGHTS SETUPS

Serial.println(sensorValue); // print out the value you read:
delay(5); // refresh rate

if(volt<lowLimit)//voltage below lower limit
red();
else if(volt<=highLimit)//now we know that voltage is between low and high
green();
else//now we know that voltage is above high
red();

void green(){
digitalWrite(8, 0);   //red light off
digitalWrite(9, 1);   //green light on
}                     //gren()

void red(){
digitalWrite(8, 1);   //red light on
digitalWrite(9, 0);   //green light off
}                     //red()

}
``````

thank you

now getting a bunch of { errors:

Where is the end of your setup() function ?

Why are red() and green() functions defined inside the loop() function ?

ok i found the void setup error…
not sure what you mean by red and green functions defined in the loop…?

``````#include <LiquidCrystal.h>

// number of analog samples to take per reading, per channel
#define NUM_SAMPLES 10
// voltage divider calibration values
#define DIV_1    11.255
#define DIV_2    11.255

// ADC reference voltage / calibration value
#define V_REF    4.991

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sum[4] = {0};                // sums of samples taken
unsigned char sample_count = 0;  // current sample number
float voltage[4] = {0.0};        // calculated voltages
char l_cnt = 0;                  // used in 'for' loops
const int lowLimit = 80;
const int highLimit = 100;
int volt;

void setup()
{
lcd.begin(16, 2);

pinMode (2, INPUT);  // raw voltage input
pinMode (3, INPUT);  // raw voltage output

Serial.begin(9600);  // set up Serial library at 9600 bps, monitor

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}

void loop()
{
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES)

{
// sample each channel A2 to A5
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
}
sample_count++;
delay(50);
}

// calculate the voltage for each channel
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
}

// display voltages on LCD
// each voltage is multiplied by the resistor network
// division factor to calculate the actual voltage
// voltage 1 - A (pin A2)
lcd.setCursor(0, 0);
lcd.print("IN ");
lcd.print(voltage[0] * DIV_1, 1);
lcd.print("V ");

// voltage 2 - B (pin A3)
lcd.setCursor(8, 0);
lcd.print("OUT ");
lcd.print(voltage[1] * DIV_2, 1);
lcd.print("V ");

// voltge 3 - C (pin A4)
lcd.setCursor(0, 1);

lcd.print("STATUS: ");
// voltage 4 - D (pin A5)
lcd.setCursor(12, 1);
lcd.print("CAL!");

// reset count and sums
sample_count = 0;
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
sum[l_cnt] = 0;
}

//INDICATOR LIGHTS SETUPS

Serial.println(sensorValue); // print out the value you read:
delay(5); // refresh rate

if(volt<lowLimit)//voltage below lower limit
red();
else if(volt<=highLimit)//now we know that voltage is between low and high
green();
else//now we know that voltage is above high
red();
#include <LiquidCrystal.h>

// number of analog samples to take per reading, per channel
#define NUM_SAMPLES 10
// voltage divider calibration values
#define DIV_1    11.255
#define DIV_2    11.255

// ADC reference voltage / calibration value
#define V_REF    4.991

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sum[4] = {0};                // sums of samples taken
unsigned char sample_count = 0;  // current sample number
float voltage[4] = {0.0};        // calculated voltages
char l_cnt = 0;                  // used in 'for' loops
const int lowLimit = 80;
const int highLimit = 100;
int volt;

void setup()
{
lcd.begin(16, 2);

pinMode (2, INPUT);  // raw voltage input
pinMode (3, INPUT);  // raw voltage output

Serial.begin(9600);  // set up Serial library at 9600 bps, monitor

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);

void loop()
{
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES)

{
// sample each channel A2 to A5
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
}
sample_count++;
delay(50);
}

// calculate the voltage for each channel
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
}

// display voltages on LCD
// each voltage is multiplied by the resistor network
// division factor to calculate the actual voltage
// voltage 1 - A (pin A2)
lcd.setCursor(0, 0);
lcd.print("IN ");
lcd.print(voltage[0] * DIV_1, 1);
lcd.print("V ");

// voltage 2 - B (pin A3)
lcd.setCursor(8, 0);
lcd.print("OUT ");
lcd.print(voltage[1] * DIV_2, 1);
lcd.print("V ");

// voltge 3 - C (pin A4)
lcd.setCursor(0, 1);

lcd.print("STATUS: ");
// voltage 4 - D (pin A5)
lcd.setCursor(12, 1);
lcd.print("CAL!");

// reset count and sums
sample_count = 0;
for (l_cnt = 0; l_cnt < 4; l_cnt++)

{
sum[l_cnt] = 0;
}

//INDICATOR LIGHTS SETUPS

Serial.println(sensorValue); // print out the value you read:
delay(5); // refresh rate

if(volt<lowLimit)//voltage below lower limit
red();
else if(volt<=highLimit)//now we know that voltage is between low and high
green();
else//now we know that voltage is above high
red();

void green(){
digitalWrite(8, 0);   //red light off
digitalWrite(9, 1);   //green light on
}                     //gren()

void red(){
digitalWrite(8, 1);   //red light on
digitalWrite(9, 0);   //green light off
}                     //red()

}
}
``````

You've got two "loop"s and two "setup"s.
That's not allowed.

Defining functions inside other functions is also not allowed