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 )
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()?
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.
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;
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....
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;
}
}
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.