Reading Digital and Analog Input at the same time and 'Or' them.

I have an analog input connected to A0 of my Arduino 2560 and a digital input conected to the pin 7. I want the digital output at pin 13 to be high either when the value at analog input goes below a certain value or when the digital input at pin 7 reads high.

Similarly, i want the pin 13 output to be low for the reverse similar conditions like wise. I have already tried the following code:

// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int transistorPin = 13;       // pin that the LED is attached to
const int dtmfPin = 7;// pin that the Q4 of MT8870 is attached to
int value = 0;
void setup() {
  // initialize the LED pin as an output:
  pinMode(transistorPin, OUTPUT);
//initialize the DTMF output pin as input
  pinMode(dtmfPin, INPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);
  value = digitalRead(dtmfPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue < 150 || dtmfPin == HIGH ) {
    digitalWrite(transistorPin, HIGH);
  }


  else if (analogValue > 450 || dtmfPin == LOW){
    digitalWrite(transistorPin,LOW);
  }

  // print the analog value:
  Serial.println(analogValue, DEC);

}

But its only the analog input that seems to work correctly, the output at pin 13 pays no heed to the digital input.

What i am actually trying to do is controlling a pump through a moisture senser and a digital output from DTMF reciever IC.

Nothing is to change with analog value from 150 to 450?

analog value 1023 = 5V input, 0 = 0v input.
150 = ~.73V,
450 = ~2.2V

Try print the Hi/Lo state of the DTMF input also.

These lines have logic error:
if (analogValue < 150 || dtmfPin == HIGH ) {
else if (analogValue > 450 || dtmfPin == LOW){

replace "dtmfPin" with "value"

Thanks a lot Sir!:slight_smile: I have replaced 'dtmfPin' with value but now the digital input and analog input works only seperately; i can output high or low at pin 13 either by getting a high or low at pin 7 or a high or low at pin A0. But if i turn the pin 13 high by reading a high on pin 7 then i can not turn it off by controlling analog input...i hope i could clarify...

sanowar:
I want the digital output at pin 13 to be high either when the value at analog input goes below a certain value or when the digital input at pin 7 reads high.

It's doing what you said you wanted. If pin 7 is high, then pin 13 is high.

Yes...but the digital input to pin 7 us from a dtmf decoder which gets high or low when particular buttons of the connected cell phone are pressed and the analog input to AO is being being fed from a humidity sensor.

i can turn the output high or low by pressing buttons, the output also changes depending on the soil moisture content..but whent the output is high to to the analog input i cant make to low by pressing the button from the cell.Also, when the output is high due to the press of a button of the phone, the output doesnt get low when the value at analog input is increased. It was supposed to accroding to the progam. Like as the code goes:

if (analogValue < 150 || value == HIGH ) {
digitalWrite(transistorPin, HIGH);
}
else if (analogValue > 450 || value == LOW){
digitalWrite(transistorPin,LOW);

the output was supposed to be low by either of the two conditions mentioned and vice-versa but when the output is high due to 'analogValue<150' ; the output that is the 'transistorPin' does not go low when 'value == Low' but it does when 'analogValue>450'.

I am not being able to figure out where am i going wrong in the code.

Thanks.

The Else won't get executed when either analogValue<150 OR value is HIGH.

Sounds like you need to test for a different conditions, maybe:

if (analogValue <150){
// output will be based on value
digitalWrite (transistorPin, value);
}

Can you clarify the requirements? Let's do a truth table. You have 6 possibilities:

  • analogValue < 150 AND value == HIGH
  • analogValue < 150 AND value == LOW
  • analogValue >= 150 AND analogValue <= 450 AND value == HIGH
  • analogValue >= 150 AND analogValue <= 450 AND value == LOW
  • analogValue > 450 AND value == HIGH
  • analogValue > 450 AND value == LOW

Now for each case, do you want the transistor pin high or low?

I have just figured out that there is a contradiction in the logic. how to eleminate that?

Rewrite the conditions you test for.
Maybe use && (AND) instead or || (OR) ?
Figure out what you want per Nick's suggestion and the input possibilities and the desired outcome.

The if-else construct seems to be part of the problem.

Thanks a lot Mr. CrossRoads and Mr. Nick.

Mr. Nick here i go:

if the analogValue is <150, transistorpin high
if analongValue is >450, transistorPin low

if value = high, transistor pin =high
if value = low, transistor pin = low

if value== high AND analogValue>450, transistorPin high
if value == low AND analogValue<450, transistorPin low

analogValue >= 150 AND analogValue <= 450 AND value == HIGH ----transistorPin=high
analogValue >= 150 AND analogValue <= 450 AND value == LOW------transistorPin=low

Sir, actually i want my system to turn on a pump through that transistor pin when the analogValue<150 and to turn the pump off when analog
Value>450

i also want to turn on the pump with the value==HiGH and turn that off with value==Low. But when the conditions are such that they oppose one another i want to give ther priority to value

Please could u help me with the logic.

Thanks a lot again

sanowar:
if the analogValue is <150, transistor pin high

...

if value = low, transistor pin = low

OK I'll bite...

If analogValue is < 150 AND value == low

do you want the transistor pin to be high or low?

You are not completely specifying what you want. Forget about the code until you can do that.

In the case of what you mentioned...i need the transistor pin to be low as i want to give value the greater priority. Thanks Sir

If I read you right, you want the dial tone buttons to always control the pump and when they aren't it should go by the analog reading.

You have phone tone for run the pump and another for stop the pump but none for go back to automatic. You might want that and a timer to kick things back into automatic just in case.

Smart thing to do is to Not Combine your conditions in one statement. Always check for manual control and if not in manual, then check the analog and proceed accordingly.

What you suggested is really smart..its better to keep a botton which will switch the mode of control from manual to automatic and otherwise. Yes..i would like to do that..but i need the logic for that..i am going wrong there

i have two buttons that can switch the pump on and off. i also have an analog input that can automatically switch the pump on and off depending on moisture level.

I want to combine both and what should i do in the code if i wanqt to include a third button(i.e another digital input) that can switch the control mode from automatic to manual? Thanks

"I want to combine both and what should i do in the code if i wanqt to include a third button(i.e another digital input) that can switch the control mode from automatic to manual?"

Add another input pin; and "nest" your checking of data pins:

if (digitalRead (automaticPin) == 1){  // 1 == auto, 0 = manual
    // make decisions based on analog Value
}
else { 
   // make decisions based on two buttons
}

Maybe define modes -- error=0, auto_pump_off=1, auto_pump_on=2, man_pump_off=3, man_pump_on=4
Always be ready for errors!

loop()
{
-- check for button press, if so then change mode to suit (3rd button would set to auto_pump_off)
-- if mode is an auto mode then check humidity and set mode accordingly
-- if mode is a pump_on mode then run the pump
-- else if mode is a pump_off mode then stop the pump
-- if mode is error then handle it or don't but at least you should know your code needs fixing
}

This is just one of many ways you could do it.

Thanks!:slight_smile: