If statements being read as or, please help

Forgive the newbie question here, I only started using the program yesterday, I'm sure I'm overlooking something trivial.

I'm attempting to writing a code to that would require multiple (2 or 3) 5v inputs to then send signal to a relay controlling a 12v system. I'm running into an issue with the (if) statement. It appears the code is being read as (or) where my (if) statement is. To further elaborate, If I put 5 volts to either port I have set as an input, I get power to the output. Whereas I would like to have it set to only give output when I have signal to ALL inputs.

Any help is much appreciated, thanks in advance!

Compartment_Code.ino (587 Bytes)

eurotex96's code:

int out1 = 7;
int in1 = A0;
int in2 = A5;
int in3 = A2;
int threshold = 900;
void setup() {
  pinMode(out1, OUTPUT);
  pinMode(in1, INPUT);
  pinMode(in2, INPUT);
  pinMode(in3, INPUT);
  digitalWrite(out1, HIGH);
  Serial.begin(9600);
}
void loop() {
  int analogValue1 = analogRead(in1);
  int analogValue2 = analogRead(in2);

  if ((analogValue1 > threshold) && (analogValue2 > threshold)){
    digitalWrite(out1, HIGH);
  } else {
    digitalWrite(out1, LOW);
  }

  Serial.println(analogValue1);
  delay(1);
  Serial.println(analogValue2);
  delay(1);
}

If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's OK to add it as an attachment. In this case, it's nowhere near the 9000 characters limit.

You missed to present the Serial output, which most probably would explain the behaviour.

A circuit diagram would be helpful, perhaps you receive music off the air on the analog inputs?

If I put 5 volts to either port I have set as an input, I get power to the output. Whereas I would like to have it set to only give output when I have signal to ALL inputs.

How are the inputs wired ? Are they in a known state at all times or are they floating at an unknown value ?

What do you see when you print the values ?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

What values do you see when you print analogueValue 1 and analogValue2?

Thanks everyone for the input so far, I've reviewed the guidelines and will be sure to adhere to them from here on out. Sorry for the slow response as I've been in class all day. I spent an hour trying to get the fritzing program to run without success, so instead I've attached two photos of the setup I have. The brown and white wires running out of the picture frame from A0 and A5 are my 5v inputs. Thanks again!

Hi,
A hand drawn picture would be great?
Please include the two 5V sources you are reading from.

What are the sources?
Does their outputs go from 5V to gnd when operating or are they 5v then open circuit, like the source is a switch?

Thanks.. Tom.. :slight_smile:

I'm working on a diagram at the moment, I apologize as I'm much better at reading electrical diagrams than I am at writing them. The sources are switched 12v sources that are stepped down to 5v using L7805 regulators. Both sources are open when not activated, I've tried changing the pins to see if that's the issue with no avail.

Another thing, when I open the serial plotter I get an even 1020 when I put 5v to either INPUT pin

I can't see any ground wires going to the mystery devices off-screen.

Are the ground pins of your two L7805 regulators connected to an Arduino ground pin?

eurotex96:
Another thing, when I open the serial plotter I get an even 1020 when I put 5v to either INPUT pin

What do you get when you have have 0V?
Tom.... :slight_smile:

Tom, at "0v" I get a range of about 90-150

And John, the regulators are currently grounded to the chassis of the car. However I would like to utilize a ground pin off of the arduino if at all possible.

eurotex96:
Tom, at "0v" I get a range of about 90-150

Place a 10K resisitor from each analog input to gnd.
Your 0V looks like it is just letting the analog input pin float.
Thanks.. Tom.. :slight_smile: