help with and logic

Hi i am new here and just started using arduino.

here is my issue when using the && logic I can only get it to work with 2 inputs but need 3 heres my code

int ina = 3;
int inb = 4;
int inc = 5;
int outa = 9;

// variable to store the value coming from the sensora
//int analogPin2 = 2; // variable to store the value coming from the sensorb
//int analogPin3 = 3; // variable to store the value coming from the sensorc

void setup()
{
// declare the ledPin as an OUTPUT:
pinMode(ina, INPUT);
pinMode(ina, INPUT);
pinMode(ina, INPUT);
pinMode(outa, OUTPUT);

}

void loop()
{

digitalRead(ina);
digitalRead(inb);
digitalRead(inc);
if (digitalRead(ina) == HIGH && digitalRead(inb) == HIGH ) {
digitalWrite(outa, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(outa, LOW); // sets the LED off
delay(1000); // waits for a second;

here is what i have tryed
if (digitalRead(ina) == HIGH && digitalRead(inb) == HIGH && digitalRead(inc) == HIGH )

any hep would be great Thanks

   pinMode(ina, INPUT);
        pinMode(ina, INPUT);
        pinMode(ina, INPUT);

What about inb and inc?

I always like to wrap my conditions in brackets, as the order of precedence when working with logic can be a bit strange:

  if ((digitalRead(ina) == HIGH)  && (digitalRead(inb) == HIGH)   && (digitalRead(inc) == HIGH)  )

(The most confusing is:

if (val & 0x04 == 0) { ...

The == has a higher precedence than the &, so it tries to match 0x04 with 0, which equates to 0 or false, then ANDs that with val, which will always end up as 0 - just a little one to watch out for.)

And why are you reading the inputs individually and discarding the results, then reading the inputs again in the if()?

I am new to this all I really need it to have 3 inputs && together

zachdreiling:
I am new to this all I really need it to have 3 inputs && together

Yes, we know that, you said that in your original question. Now, read my answer, absorb it, and then ask questions about what I said to get clarification on the bits you don't understand. Just whingeing that you're new to all this and whining about what you want it to do, without actually trying things or reading and attempting to understand the answers, isn't the way to garner good feelings (and as a result useful and helpful answers) from other people.

zachdreiling:
I am new to this all I really need it to have 3 inputs && together

So did you try majenko's suggestion of putting each piece in brackets?

(And change those pinMode lines to be inb and inc)

Oh, and another two things you really need to do:

  1. post ALL your code, not just the first few lines, and
  2. post it in [code]...[/code] tags.

I have made the changes suggested but the output will still go high with only 2 of the 3 inputs high.
code
[
//define var
int ina = 3;
int inb = 4;
int inc = 5;
int outa = 9;

// variable to store the value coming from the sensora
//int analogPin2 = 2; // variable to store the value coming from the sensorb
//int analogPin3 = 3; // variable to store the value coming from the sensorc

void setup()
{
// declare the ledPin as an OUTPUT:
pinMode(ina, INPUT);
pinMode(inb, INPUT);
pinMode(inc, INPUT);
pinMode(outa, OUTPUT);

}

void loop()
{

if ((digitalRead(ina) == HIGH) && (digitalRead(inb) == HIGH) && (digitalRead(inc) == HIGH) ) {
digitalWrite(outa, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(outa, LOW); // sets the LED off
delay(1000); // waits for a second;

}

}
]

What ARE the inputs? If you have switches connected to the pins, how are they wired? I suspect a floating pin condition.

I just have 5vdc going to them to make them go high

To use the code tags, click on "Preview" and then you'll see a menu of tags just above the edit window.

I was wondering much the same as PaulS and was going to ask how you know the inputs' actual values.

If I were you I'd use Serial.print to print the actual values to the monitor just before the if{}, so you can see what's what.

Post your circuit. Have you got pullups or pulldowns on the inputs to prevent floating as PaulS fears might be the case?

PS.... you haven't quite got the hang of the code tags. Select the code, then hit the # icon which will put a code tag in front and a /code tag at the back. But then you won't be able to use the funky yellow....

zachdreiling:
I just have 5vdc going to them to make them go high

Floating input then as PaulS said. Pull the other one to ground.

not sure what you mean. put what pin to ground?

wildbill:

zachdreiling:
I just have 5vdc going to them to make them go high

Floating input then as PaulS said. Pull the other one to ground.

If he's jammed a 5V supply into the input pins which is what it sounds like to me, they won't be floating they'll be high all the time, surely?

I don't like putting digitalread in the middle of any operation. What if one or more changes during the test?

This is redundant:
(digitalRead(ina) == HIGH)

Within an If operation, the only answer to the test is True or False. Just boolean AND each input to each other.

//define var
int ina = 3;
int inb = 4;
int inc = 5;
int outa = 9; 

// variable to store the value coming from the sensora
//int analogPin2 = 2;  // variable to store the value coming from the sensorb
//int analogPin3 = 3;  // variable to store the value coming from the sensorc

void setup()
{
        // declare the ledPin as an OUTPUT:
        pinMode(ina, INPUT);
        pinMode(inb, INPUT);
        pinMode(inc, INPUT);
        pinMode(outa, OUTPUT);
        
}

void loop()
{
   
  if ((digitalRead(ina) == HIGH)  && (digitalRead(inb) == HIGH) && (digitalRead(inc) == HIGH)  ) {
     digitalWrite(outa, HIGH);   // sets the LED on
     delay(1000);                  // waits for a second
     digitalWrite(outa, LOW);    // sets the LED off
     delay(1000);                  // waits for a second;

}
 
}

can you show me an example. Also for the floating inputs do I just put a resistor from +5 to ground on my input rail?

JimboZA:

wildbill:

zachdreiling:
I just have 5vdc going to them to make them go high

Floating input then as PaulS said. Pull the other one to ground.

If he's jammed a 5V supply into the input pins which is what it sounds like to me, they won't be floating they'll be high all the time, surely?

Right, but he's trying to test it by the sound of it with only two of them held high.

By the way, you should really get used to using capitalization and punctuation. It makes it a bit more difficult to help you.

Logical operations are, by convention, written all in caps: AND OR NOT NOR XOR

"help with and logic" is a lot less clear than "Help with AND logic".

zachdreiling:
can you show me an example. Also for the floating inputs do I just put a resistor from +5 to ground on my input rail?

For the purposes of your test, connect whichever input is not connected to 5V directly to ground.

Right, but he's trying to test it by the sound of it with only two of them held high.

But, when the pin is not held HIGH, what is it's value? It floats, unless it is held HIGH or LOW. That requires using either the internal pullup resistor or an external pullup or pulldown resistor.