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.
Thanks a lot Sir! 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.
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.
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
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
}