loading

arduino coding question - if any of the 3 sensors are low, turn off led?

hello everyone. i have no background in programming. i am currently working on a plant health monitor. the monitor has 3 sensors, water - temperature - light. i can successfully read data from sensors and turn on/off leds depending on the values.
what I just CAN'T figure out how to do yet is this;
i want to be able to keep a green led lit as long as all the environmental conditions are good from the 3 analog sensors and turn the led off if either sensor is off. 
is this clear enough? - let me know if you don't understand my question and here is my code so far...

plant health monitor V1.0 by Akin Yildiz

int sensor_temp = A0;
int value_temp;
int sensor_light = A1;
int value_light;
int sensor_water = A2;
int value_water;

void setup() {
Serial.begin(9600);
pinMode (11, OUTPUT); // temperature LED, red color
pinMode (10, OUTPUT); // light LED, yellow color
pinMode (9, OUTPUT); // water LED, blue color
pinMode (6, OUTPUT); // this is the green LED i want to program
}

void loop() {
value_temp = analogRead(sensor_temp);
Serial.print("temperature");
Serial.println( value_temp );
value_light = analogRead(sensor_light);
Serial.print("light");
Serial.println( value_light );
value_water = analogRead(sensor_water);
Serial.print("water");
Serial.println( value_water );

delay(1000);

if (value_temp < 400){
  digitalWrite (11, HIGH); // plant too cold - red led on
}else{
  digitalWrite (11, LOW); // normal temp - red led off
}

if (value_light < 300) {
  digitalWrite (10, HIGH); // not enough light - yellow led on
}else{
  digitalWrite (10, LOW); // enough light - yellow led off
}

if (value_water < 600) {
  digitalWrite (9, HIGH); // plant thirsty - blue led on
}else{
  digitalWrite (9, LOW); // soil is moist - blue led off
}

WE HAVE TO ADD SOME LINES HERE FOR GREEN LED ON DIGITAL PIN 6. I need this LED to come on when all other LEDs are off and turn off when either of the other LEDs are on!

}


--

thank you for your time, i'm sure it is an easy line to add but again i'm amazed that I got this far, all learned from this webpage!

akin,

sort by: active | newest | oldest

Add a variable called "status" set it to 1 at the top of the program.

In each error condition, set status = 0 if there is an error.

At the end of the loop, if status ==1, then turn on the green LED. else turn it off.

Akin Yildiz (author)  steveastrouk2 years ago

hello once again, i think you should see what you helped me with to build....

The Plant Arm - Garduino Upgrade

thank you so very much, you have no idea how much it means to me. i'm sure i'll be back with more question soon enough, have a nice day!

FBR3UWWHWZ9VX8L.jpgIMG_6691.JPG

I am very happy to have helped. Thanks for your link.

Akin Yildiz (author)  steveastrouk3 years ago

very nice sir.! let's see if I can read it right. in the beginning of the loop arduino sees that status=0 then it checks the sensor readings and if either of them is above normal then the status gets set to status=1 automatically ? is this why you wrote status=1 before each " }else{ " line? because if you wrote it after the }else{ line then the arduino would think the extreme weather conditions are status=1 which isn't what we want. and at the end of each loop after arduino reads the sensor values, it looks to see if any of the statuses were ever set to 1, if not status is still =0 and the green led stays lit, otherwise green led turns off assuming something is wrong, and at this point which ever sensor is reading dangerous conditions is also turns on the corresponding led. i'm pretty sure i got it, but what about the two equal signs? status ==0, why does it get two equal signs? is that just how the language works? i'm just trying to learn it better. thank you so much already for a crazy fast answer !!!

You have it pretty well correct.

Most decent computer languages make a distinction between an assignment what we would write as A=B+1, 'C' writes as A=B+1, Pascal and Delphi write as A:=B+1, which is read as A is assigned equal to B+1; and an equality , which is a QUESTION or assertion, which we would read as IS A=B+1, and 'C' uses A==B+1, Pascal uses A=B+1.

So be careful, you can get some truly odd error messages confusing the two, particularly on Arduino !

int sensor_temp = A0;
int value_temp;
int sensor_light = A1;
int value_light;
int sensor_water = A2;
int value_water;
int status;

void setup() {
Serial.begin(9600);
pinMode (11, OUTPUT); // temperature LED, red color
pinMode (10, OUTPUT); // light LED, yellow color
pinMode (9, OUTPUT); // water LED, blue color
pinMode (6, OUTPUT); // this is the green LED i want to program
}

void loop() {

status=0;
value_temp = analogRead(sensor_temp);
Serial.print("temperature");
Serial.println( value_temp );
value_light = analogRead(sensor_light);
Serial.print("light");
Serial.println( value_light );
value_water = analogRead(sensor_water);
Serial.print("water");
Serial.println( value_water );

delay(1000);

if (value_temp < 400){
digitalWrite (11, HIGH); // plant too cold - red led on
status=1;
}else{
digitalWrite (11, LOW); // normal temp - red led off
}

if (value_light < 300) {
digitalWrite (10, HIGH); // not enough light - yellow led on
status=1;
}else{
digitalWrite (10, LOW); // enough light - yellow led off
}

if (value_water < 600) {
digitalWrite (9, HIGH); // plant thirsty - blue led on
status=1;
}else{
digitalWrite (9, LOW); // soil is moist - blue led off
}

WE HAVE TO ADD SOME LINES HERE FOR GREEN LED ON DIGITAL PIN 6. I need this LED to come on when all other LEDs are off and turn off when either of the other LEDs are on!
if (status==0) digitalWrite(6,HIGH)
else digitalWrite(6,LOW);

}