Go Down

Topic: Easy question about Arduino syntax, please help!! (Read 735 times) previous topic - next topic

ShefBrah

Hi,

I have two push buttons and need to run some code if EITHER ONE or BOTH are pushed.
So, basically I need to know the correct syntax for doing this in an if statement.

I have something like this:

Code: [Select]
if (sensorValue1 and sensorValue2 == HIGH) {
//run this code}
else if (sensorValue1 or sensorValue2 == LOW {
//run this code}


I am aware this is incorrect syntax and just need someone to help me out with the correct syntax for this kind of condition.

Thanks in advance!


creativen

if (sensorValue1 and sensorValue2 == HIGH) {
//run this code}
else (sensorValue1 or sensorValue2 == LOW {
//run that code}

for last condition, only use else I think.

ShefBrah

That doesn't work. I think that it's not Arduino syntax to have the 'and' and 'or' there to seperate the two conditions? Someone please aware me on the correct syntax.

Aeturnalus

Try this:

Code: [Select]

if ((a ^ b) || (a && b)) {
// do something
}


the ^ is an XOR, the || is OR, and the && is AND.

WizenedEE

you can replace the 'and's in your code with && and the 'or's with ||. You will also need to have the == HIGH for both, like this:
Code: [Select]
if (sensorValue1 == HIGH && sensorValue2 == HIGH) {
//run this code}
else if (sensorValue1 == LOW || sensorValue2 == LOW {
//run this code}

Jack Christensen

There is not really such a thing as "Arduino syntax". Arduino programs (sketches) are written in C/C++. Required reading here: http://arduino.cc/en/Reference/HomePage
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

ShefBrah

Yess!! Thank you so much. My code works now :)

Go Up