Go Down

Topic: One output, reacting to many inputs. (Read 317 times) previous topic - next topic

Rich196

May 14, 2013, 05:56 pm Last Edit: May 14, 2013, 06:00 pm by Rich196 Reason: 1
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);
  }
}

guix

#1
May 14, 2013, 06:04 pm Last Edit: May 14, 2013, 06:35 pm by guix Reason: 1
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:
Code: [Select]

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?

lloyddean

#2
May 14, 2013, 06:20 pm Last Edit: May 14, 2013, 06:26 pm by lloyddean Reason: 1
Code: [Select]

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


Rich196


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:
Code: [Select]

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 :(

JimboZA

Quote
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
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

guix

#5
May 14, 2013, 06:34 pm Last Edit: May 14, 2013, 06:37 pm by guix Reason: 1
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
Code: [Select]

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

Rich196

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

Go Up