Pages: [1]   Go Down
Author Topic: Easy question about Arduino syntax, please help!!  (Read 609 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!

Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

for last condition, only use else I think.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

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

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

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (sensorValue1 == HIGH && sensorValue2 == HIGH) {
//run this code}
else if (sensorValue1 == LOW || sensorValue2 == LOW {
//run this code}
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3532
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yess!! Thank you so much. My code works now smiley
Logged

Pages: [1]   Go Up
Jump to: