Loading...
Pages: [1]   Go Down
Author Topic: Easy question about Arduino syntax, please help!!  (Read 362 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
God Member
*****
Karma: 14
Posts: 998
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
Edison Member
*
Karma: 43
Posts: 2478
"We're a proud service of the Lost Electricity Reclamation Agency"
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

Get the infamous "One Million Ohms" board at tINDIE.com: http://tinyurl.com/BuyMohms

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
Print
 
Jump to: