I'm pretty new to programming and just coding in general. I've learnt the very basics so far (using one sensor) but I have an idea for a project that I could really use some help with.
I am wanting to use 4 inputs to create a singular output. For example:
If Sensor 1,2,3,4 are showing within certain levels then the green LED is on
If Sensor 1,2 are good but 3,4 are bad then the amber LED is on
If Sensor 1 is good but 2,3,4, are bad then the red LED is on
I've had a look around different forums, youtube videos and even resorted to asking my friends (shock, non of them had a clue what I was going on about) but I've got nothing.
If there are any suggestions then I would be extremely appreciative!
I think your question will be easy to answer. Actually, you precisely write down what you expect from your code and it is a very good way to work.
There must be a LOT of different way to get what you want. Here is 2 can imagine:
Fisrt: create a value "RESULT" wich will contain the sum of your sensors in "good state". Then, add 1 to RESULT for every sensor in "good" state and substract 1 for every sensor in "bad" state.
You now have a value between 0 (every sensors are "bad") and 4 (every sensors are "good"). Connect this value to the led this siple way:
for (byte i=0;i<3;i++){
digitalWrite(Led_number,LOW);} // set all 3 leds off
// assuming prompt your sensors will return 0 or 1:
RESULT = SENSOR_1+ SENSOR_2 + SENSOR_3 + SENSOR_4;
if (RESULT<2); digitalWrite(RED_LED, HIGH);
if (RESULT<4)|(RESULT>1); digitalWrite(AMBER_LED, HIGH);
if (RESULT==4); digitalWrite(GREEN_LED, HIGH);
That should be working but RESULT only know the sum of sensors in good state, not the exact configuration.
If you feel yourself brave enough, use a "bit-value" rather than a sum:
Create a RESULT value wich will vary from 0b0000 to 0b1111. each byte (from first at right to fourth at left) will represent an unique sensor and its value will be the state of the sensor.
As an example: RESULT = 0b0101 indicates sensor_3 and sensor_1 are in good state while sensor_4 and sensor_2 are in bad state.
you get RESULT this way:
RESULT |= SENSOR_1 << 0;
RESULT |= SENSOR_2 << 1;
RESULT |= SENSOR_3 << 2;
RESULT |= SENSOR_4 << 3;
The answer is really quite simple
If h1 is the Hygrometer reading, light the LDR reading, temp the temperature reading and h2 the DHT11 humidity reading
if ((h1 > 10 && h1 < 30) && (light > 20 && ligh < 50) && (temp > 20 && temp < 30) && (h2 > 20 && h2 < 30))
{
// code here to turn on green LED
}
and so on with other tests
The names of your variables and the ranges that you test for, will, of course, be different than my example
ok. that was for the example.
You will be certainly able to add a command to filter the sensor result into a 0 or 1 wich could be a proper value to set/clear the led (by passing it into the digitalWrite(LED, HIGH or LOW);
So the values or test that inform making one number for ranges and perhaps another for good/bad can certainly be 1 or 0, true or false.
Those can be handled with @GrandPete's REUSLT type calculation. Then there would be 16 cases that could be switched amongst, or tested in if/else statements.