int gillyPin1 = 6;
int gillyPin2 = 7;
int ledPin = 8;
void setup() {
// put your setup code here, to run once:
pinMode(gillyPin1,INPUT);
pinMode(gillyPin2,INPUT);
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,LOW);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(gillyPin1 = HIGH) or digitalRead(gillyPin2 = HIGH)) {
digitalWrite(ledPin,HIGH);
Serial.println("Out!");
}
else {
digitalWrite(ledPin,0);
}
delay(500);
}
For you it may be self explanatory. I am trying to output "Out!" whenever gillyPin1 or gillyPin2 goes high. However I always get high in the serial monitor even if none of the pins is high. I know my Arduino has no problems. But why this?
if(digitalRead(gillyPin1 = HIGH) or digitalRead(gillyPin2 = HIGH))
the = will assign the value HIGH to gillyPin1 and gillyPin2, with HIGH being equal to 1, so effectively you have written:
if (digitalRead(1) or digitalRead(1))
if you change the = to ==, then you will be evaluating the expression (gillyPin1 == HIGH), which will return true, because gillyPin1 is 6, and any number other than 0 is considered to be HIGH. Same for gillyPin2. True is defined as 1, so again you have:
if (digitalRead(gillyPin1 == HIGH) or digitalRead(gillyPin2 == HIGH))
being equivalent to
if (digitalRead(1) or digitalRead(1))
You want to read the input, then compare it to HIGH, such as:
if ((digitalRead(gillyPin1) == HIGH) or (digitalRead(gillyPin2) == HIGH))
I'm a bit surprised it works, but the normal symbol for or is || , and the statement is usually written as:
if ((digitalRead(gillyPin1) == HIGH) || (digitalRead(gillyPin2) == HIGH))