Pages: [1]   Go Down
Author Topic: One output, reacting to many inputs.  (Read 256 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
   }
 }
 
« Last Edit: May 14, 2013, 11:00:54 am by Rich196 » Logged

France
Offline Offline
Edison Member
*
Karma: 37
Posts: 1005
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
« Last Edit: May 14, 2013, 11:35:00 am by guix » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
}
« Last Edit: May 14, 2013, 11:26:25 am by lloyddean » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley-sad
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4257
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

France
Offline Offline
Edison Member
*
Karma: 37
Posts: 1005
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley
« Last Edit: May 14, 2013, 11:37:26 am by guix » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you everyone!! I shall be back with more silly questions soon im sure!!! smiley-sad
Logged

Pages: [1]   Go Up
Jump to: