One output, reacting to many inputs.

I am completely new to all of this so please bare with me.

What I would like to be able to do is have 1 LED turn on, when any one of the 8 digital inputs comes on. In the code below I have got it reading multipul inputs but the LED only reacts on the highest port number in this case input4.

I understand its because the final input read will over write the variable, is there a simple way of doing it differently? Can I read inputs 2-10 as a block and the react on a boolean if any bits are a 1?

If someone can tell me where I am going wrong I would much appreciate it!


/* Water sensor

This sketch will light up the LED on Pin 9, when water (anything conductive) bridges the gap in the sensor pins 2-10

*/

const int floodSensor1 = 2; // the number of the Flood Sensor pin
const int floodSensor2 = 3; // the number of the Flood Sensor pin
const int floodSensor3 = 4; // the number of the Flood Sensor pin
// const int floodSensor4 = 5; // the number of the Flood Sensor pin
// const int floodSensor5 = 6; // the number of the Flood Sensor pin
const int ledPin = 9; // the number of the LED pin (pin 9 is the on board led)

// variables will change:
int floodSensorState = 0; // variable for reading the floodSensors status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the flood Sensor pin as an input:
pinMode(floodSensor1, INPUT);
pinMode(floodSensor2, INPUT);
pinMode(floodSensor3, INPUT);

}

void loop(){

// read the state of the flood Sensor value:
floodSensorState = digitalRead(floodSensor1);
floodSensorState = digitalRead(floodSensor2);
floodSensorState = digitalRead(floodSensor3);
// check if the flood Sensor is wet.
// if it is, the floodSensorState is HIGH:
if (floodSensorState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Problem is that you reassign the same variable "floodSensorState", so it's value will always be the value of digitalRead(floodSensor3) (since it is the last assignment).

You could do something like that:

if ( digitalRead(floodSensor1) || digitalRead(floodSensor2) || digitalRead(floodSensor3) )
{
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
} 
else
{
  // turn LED off:
  digitalWrite(ledPin, LOW); 
}

|| mean "or"

It is also possible, if all the floor sensors are on the same PORT, to check if the corresponding bits of that port are set, with only a few bitwise operations single operation. Which Arduino do you use?

const int floodSensor1 = 2; // the number of the Flood Sensor pin
const int floodSensor2 = 3; // the number of the Flood Sensor pin
const int floodSensor3 = 4; // the number of the Flood Sensor pin
#if 0
const int floodSensor4 = 5; // the number of the Flood Sensor pin
const int floodSensor5 = 6; // the number of the Flood Sensor pin
#endif
const int ledPin = 9;       // the number of the LED pin (pin 9 is the on board led)


void setup()
{
    // initialize the LED pin as an output:
    pinMode(ledPin,       OUTPUT);

    // initialize the flood Sensor pin as an input:
    pinMode(floodSensor1, INPUT);
    pinMode(floodSensor2, INPUT);
    pinMode(floodSensor3, INPUT);
}

void loop()
{
    // variable for reading the floodSensors status
    int floodSensorState;

    // read the state of the flood Sensor value:
    floodSensorState  = digitalRead(floodSensor1);
    floodSensorState |= digitalRead(floodSensor2);
    floodSensorState |= digitalRead(floodSensor3);

    // check if the flood Sensor is wet.
    // if it is, the floodSensorState is HIGH:
    digitalWrite(ledPin, floodSensorState);
}

guix:
Problem is that you reassign the same variable "floodSensorState", so it's value will always be the value of digitalRead(floodSensor3) (since it is the last assignment).

You could do something like that:

if ( digitalRead(floodSensor1) || digitalRead(floodSensor2) || digitalRead(floodSensor3) )

{
  // turn LED on:    
  digitalWrite(ledPin, HIGH);  
}
else
{
 // turn LED off:
 digitalWrite(ledPin, LOW);
}




|| mean "or"


It is also possible, if all the floor sensors are on the same PORT, to check if the corresponding bits of that port are set, with only a few bitwise operations. Which Arduino do you use?

Thats fantastic thank you!! I goggled for an Arduino or function and drew a blank!! Can you recommend an where I can start looking up stuff like this? I tried using the word or where you put the bars like in VB? and nothing happened :frowning:

Can you recommend an where I can start looking up stuff like this? I tried using the word or where you put the bars like in VB?

About half-way down the left-hand column of the reference page

NP, and just so you know, if you used an Arduino Mega and for example your 8 sensors were connected to pins 22 to 29, then you could just do

if ( PORTA ) // if PORTA not equal 0 mean one or more sensors detected something
{
  // turn LED on:    
  digitalWrite(ledPin, HIGH);  
} 
else
{
  // turn LED off:
  digitalWrite(ledPin, LOW); 
}

( Arduino Mega port mapping )

Welcome BTW :slight_smile:

Thank you everyone!! I shall be back with more silly questions soon im sure!!! :frowning: