Multiple Sensors to one output

Hi!

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!

Thanks,

Gibbo.

Welcome to the forum

Presumably you can read the inputs from the sensors. What type of sensors are they and are the digital or analogue ?

Can you print the values that they return ?

Hey,

Here are the sensors i'm using:
Hygrometer V1.2
Basic LDR sensor
DHT11 (Temp and humidity)

So i have 3 sensors but 4 outputs (DHT11 giving both temperatures and humidity)

They're all analogue and they can all print outputs.

Hi!
first I wish you luck with your program.

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;

or less readable:

RESULT = (SENSOR_1 << 0) | (SENSOR_2 << 1) | (SENSOR_3 << 2) | (SENSOR_4 << 3);

you can now set/clear the led according to your needs and keep tracks of your sensors state.

A multi-colored led driven by PWM would give differing colors depending upon the OP's programming.

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

They don't

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);

I've been searching for help on this for weeks now. I wish I just came straight to the forums!

massive help from everyone, thanks!

I'm sure I'll run into problems in the future and you'll see me post again.

In the mean time.... THANK YOU!!

Hello gibbo_makes_stuff

Make it simple and stupid.

Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

Yes. I saw

Sensor[(s) ] within certain levels

and

Sensor[s ] are good [or] bad

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.

Why kill it when you can overkill it? :expressionless:

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.