In previous three posts we have seen about arithmetic, relational and bitwise operators. We have one more operators left, which we will discuss in this tutorial. So, we have total four operators.
- Arithmetic operators
- Relational (comparison) operators
- Bitwise operators
- Boolean operator
Arduino buying links
Amazon link for India | Amazon link for other countries |
In this article we will see about logical operator aka Boolean operator.
Table of Contents
Boolean operators
These operators return true (1) or false (0) output according to operand given to it and operator used. It is can be used to check two conditions and perform any action if to or more condition are met. Let’s see how many operator we have in this category.
Operator name | Operator symbol |
Logical AND | && |
Logical OR | || |
Logical NOT | ! |
Suppose we are performing any operation based on any condition and we have more than one condition. In that case these operators are used mostly.
Logical AND
This operator takes two numbers as input parameter on its right and left side of ‘&’ symbol. Logical AND operator returns ‘1’ if both operands (number) are non-zero.
int a = 1; // declaring a as integer and assigning it 1
int b = 2; // declaring b as integer and assigning it 2
int c; // to store the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600); // initializing the serial communication
c = a&&b; //performing logical AND
Serial.println(c); // serial printing the result
b=0; // re-assigning b with 0
c = a&&b; //performing logical AND
Serial.println(c); // serial printing the result
}
void loop() {
// put your main code here, to run repeatedly:
}
Output on serial monitor: 1
0
As you can see that we have the output as we have expected. First of all we have declared three variable a, b and c. We have assigned 1 to a and 2 to b. ‘c’ is used to store the result of operation we will perform on a and b.
In the void setup block we have initialized the serial communication at the 9600 baud rate. Then we have performed the logical and operation on a and b and stored the result in c. Then we have serial printed the value of c. In the next line of code we have changed the value of b to 0 from 2 and repeated the same process.
This operator is mostly used to check conditions. Suppose we have more than one condition and we want to perform any operation if all our condition are satisfied. Let’s see an example.
int age;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Enter the age: ");
while (1)
{
if (Serial.available() > 0) {
age = Serial.parseInt();
break;
}
}
Serial.println(age);
if (age <= 12) {
Serial.println("You are child.");
}
else if (age > 12 && age < 20) {
Serial.println("You are teenager.");
}
else if (age > 19 && age < 40) {
Serial.println("You are adult");
}
else{
Serial.println("You are senior citizen");
}
Serial.read();
}
Output on serial monitor:
Here we have designed a software using logical operator in Arduino serial monitor which will tell us in what age stage we are based upon our age. For this we have to enter our age on serial monitor and it will tell us our age stage. For age below 12 years it will show child, age between 12 years and 20 years it will show teenager, age between 20 and 40 it will show adult and age above 40 it will show senior citizen. Now let’s understand the code.
We have declared a integer type variable called “age” to store the age that we will give in serial monitor. In void setup block we have initialized the serial communication at the baud rate of 9600.
In void loop block we have serial printed a message asking for the enter the age. Then we have started a infinite while loop and continuously checked for any serial data. This loop will continue looping until any serial data is not received. This way the code will pause until we don’t enter the age. As soon as we enter the age it will store it to age variable and break the loop.
Now if age is less than 12 years then we serial printed “You are child”. In the second and third condition we have logical and operator to check for two condition to define an age band. In second condition we have checked if age is greater than 12 years and less than 20 years. If it is true then we have serial printed that “You are teenager”. In third condition we have checked if age is greater than 20 years and less than 40 years.
So this is how you can make decisions based upon two or more conditions using logical operators.
Logical OR
This operator also takes two numbers as input parameter on its right and left side of ‘||’ symbol. Logical OR operator returns ‘1’ if any of both or both operands (number) are non-zero.
int a = 1; // declaring a as integer and assigning it 1
int b = 2; // declaring b as integer and assigning it 2
int c; // to store the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600); // initializing the serial communication
c = a||b; //performing logical OR
Serial.println(c); // serial printing the result
b=0; // re-assigning b with 0
c = a||b; //performing logical OR
Serial.println(c); // serial printing the result
a=0; // re-assigning a with 0
c = a||b; //performing logical OR
Serial.println(c); // serial printing the result
}
void loop() {
// put your main code here, to run repeatedly:
}
Output on serial monitor: 1
1
0
Here we have declared three variable a, b and c. a nd b are used as operand on which we will perform logical operation and c is used to store the result of that operation. a is initially assigned with 1 and b is initially assigned with 2.
In void setup block we have initialized the serial communication to see the outputs. Then we have performed first logical OR between a and b when a is 1 and b is 2. So answer will be 1 and that is stored in the variable c. Then we have serial printed the c. Then we have changed the value of b and a then performed logical OR operation and printed the result.
We have seen that using logical AND operator we can perform any operation if two more than two conditions are met. But using logical OR operator we can perform an operation if one or more of our conditions are met.
Logical NOT
Logical NOT invert the logical state of its operand. Operand should be written on the right side of “!” symbol. It will turn a ‘0’ to ‘1’ and any non-zero number to ‘0’.
int c; // to store the result
void setup() { // put your setup code here, to run once:
Serial.begin(9600); // initializing the serial communication
c = !0; //performing logical NOT
Serial.println(c); // serial printing the result
c = !1; //performing logical NOT
Serial.println(c); // serial printing the result
c = !2; //performing logical NOT
Serial.println(c); // serial printing the result
c = !(1&&2); //performing logical NOT
Serial.println(c); // serial printing the result
}
void loop() {
// put your main code here, to run repeatedly:
}
Output on serial monitor: 1
0
0
0
We will understand this operator using an example. We will create a program who will ask us for a number through serial monitor. Then it will tell us whether it is even or odd. Let’s see the screenshot of that serial monitor.
So you can see that it showing that whether number is even or odd. Now let’s see the code for this program.
int number;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Enter a number: ");
while (1) {
if (Serial.available() > 0) {
number = Serial.parseInt();
break;
}
}
Serial.println(number);
if (number % 2 != 0) {
Serial.println("Number is odd");
}
else {
Serial.println("Numebr is even");
}
Serial.read();
}
We have first declared a integer type variable called number to store the number that we will give through serial monitor. In the setup section we have initialized the serial communication.
In the loop function we have sent a message to user to enter the number. Then we started an infinite while loop which will keep running until we don’t give any number as input.
After we have given the input then we will check the number if it is divisible by 2. For this we have divided the number and check the remainder. Modulus(%) is that operator which will give us remainder. After getting the remainder we will equate it with 0. If it is not equal to (!=) 0 then it is odd number else it is even number.
Having read this I believed it was rather enlightening.
I appreciate you taking the time and effort to put this short article together.
I once again find myself spending way too much time both reading and commenting.
But so what, it was still worthwhile!
Interesting explanation, how can you implement the OR , AND and NOT in for electrical control system. That is for switches in control system as if S1 AND S3 = 1 , SET OUTPUT 2 HIGH. If S4 OR S1 = 1 ,SET OUTPUT 3 HIGH. And so on ,some example will be welcome for application of motor drive controls.
Wish to hear from You Soon
Safoor Ramjan
From Mauritius
Welcome, Ramjan. You have given a very good explanation. Thank you.
My family members every time say that I am wasting my time here at web, except I know I am getting know-how daily
by reading thes good content.
Wow, this article is pleasant, my younger sister is analyzing these things,
thus I am going to convey her.
I would like to take the ability of saying thanks to you for that professional guidance I have often enjoyed viewing your site.
We are looking forward to the actual commencement of my college research and the complete groundwork would never have been complete
without dropping by your blog. If I could be of any help
to others, I will be delighted to help via what I have discovered from here.
Generally I do not learn article on blogs, but I would like to say
that this write-up very forced me to take a look at and do it!
Your writing style has been amazed me. Thank you, quite great article.
Hello i am kavin, its my first occasion to commenting anyplace, when i read this article i thought i could also make comment due to this good article.
Appreciate the recommendation. Will try it out.
It’s really a nice and useful piece of info. I’m satisfied that you just shared
this helpful information with us. Please keep us up to date like this.
Thank you for sharing.
Good way of telling, and good paragraph to obtain data about my presentation subject matter, which i am going to deliver in academy.
I’m gone to tell my little brother, that he should also pay a visit this blog on regular basis to take updated from most up-to-date gossip.
Hi there to every one, because I am genuinely keen of reading this blog’s post to be updated
daily. It contains good material.
Spot on with this write-up, I actually feel this web site needs a great deal more attention. I’ll probably be back again to read more, thanks
for the information!
This is a topic that’s near to my heart…
Thank you! Exactly where are your contact details though?
Contact me through the contact page.
Hmm is anyone else encountering problems with the
images on this blog loading? I’m trying to figure out if its a problem on my end
or if it’s the blog. Any suggestions would be greatly appreciated.
This is a good tip particularly to those new to the blogosphere.
Short but very precise info… Appreciate your sharing this one.
A must read post!
Hi there mates, its wonderful article about educationand completely defined, keep it up
all the time.
Wow, incredible blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your site
is great, as well as the content!
I’ve been exploring for a little bit for any high-quality
articles or blog posts on this sort of area . Exploring in Yahoo I ultimately stumbled upon this website.
Reading this information So i am happy to show that I have
an incredibly excellent uncanny feeling I came upon exactly what I
needed. I most indubitably will make certain to do not disregard this website and provides it a look on a relentless basis.
obviously like yohr website hkwever you hav too take a look at the spelling on quite a few oof your posts.
Severral of them aare rife with spelling issues and I in finding it very troublesome to inform the truth nevertheless I will surely come back again.
I will surely take a look.
magnificent publish, very informative. I ponder why the other specialists of this sector don’t notice
this. You must continue your writing. I am sure, you have a huge readers’ base already!
Heya terrific blog! Does running a blog like this require a large amount of work?
I have absolutely no knowledge of coding but I had been hoping
to start my own blog soon. Anyway, if you have any recommendations or
techniques for new blog owners please share. I understand this is off subject nevertheless I simply needed to ask.
Appreciate it!
Hey! I know this is kind of off topic but I was wondering if you knew where I could
find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one?
Thanks a lot!
Use google captcha service.
Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment
didn’t appear. Grrrr… well I’m not writing all that over again. Regardless, just
wanted to say excellent blog!
Every comment needs to be moderated.
I am extremely impressed with your writing skills as well as with
the layout on your blog. Is this a paid theme or did you customize it yourself?
Either way keep up the nice quality writing,
it’s rare to see a nice blog like this one today.
Very good site you have here but I was curious if you knew of any
community forums that cover the same topics discussed in this article?
I’d really love to be a part of online community where I can get suggestions
from other experienced people that share the same interest.
If you have any recommendations, please let me know.
Thank you!
Every weekend i used to visit this site, for the reason that i wish for enjoyment, since this this web site conations truly good funny information too.
I’m now not positive the place you’re getting your information, but great topic.
I must spend some time studying much more or working out more.
Thanks for excellent info I used to be looking for this information for my mission.
What’s up, its good article on the topic of media print, we
all know media is a enormous source of facts.
Exceptional post however , I was wondering if you could write a litte more on this topic?
I’d be very grateful if you could elaborate a little bit more.
Kudos!
Hi mates, fastidious paragraph and nice arguments commented here, I
am really enjoying by these.
It’s amazing designed for me to have a web site,
which is good in support of my experience.
thanks admin
I have been surfing on-line more than 3 hours today, yet
I never discovered any interesting article like yours.
It’s lovely value sufficient for me. Personally, if all web owners and bloggers made just right content material as you probably did, the net
will be a lot more helpful than ever before.
Terrific article! That is the type of information that should
be shared around the web. Disgrace on Google for no longer
positioning this submit upper! Come on over and seek
advice from my site . Thanks =)
I always spent my half an hour to read this website’s articles every day along
with a cup of coffee.
bookmarked!!, I like your web site!
First off I want to say great blog! I had a
quick question which I’d like to ask if you do not mind. I was interested
to find out how you center yourself and clear your thoughts before writing.
I’ve had a difficult time clearing my thoughts in getting
my thoughts out there. I truly do take pleasure in writing however it
just seems like the first 10 to 15 minutes are wasted just
trying to figure out how to begin. Any ideas or hints?
Thank you!
This piece of writing is genuinely a nice one
it assists new internet visitors, who are wishing for blogging.
Thank you for any other magnificent article. The place else may just anyone get that
kind of information in such a perfect method of writing? I’ve a presentation next week,
and I am on the search for such information.
I’m really loving the theme/design of your blog.
Do you ever run into any browser compatibility issues? A couple of my blog visitors
have complained about my blog not working correctly in Explorer but looks great in Opera.
Do you have any advice to help fix this problem?
You ought to take part in a contest for one
of the most useful sites on the internet. I will highly recommend this site!
That is a great tip especially to those fresh to the blogosphere.
Brief but very accurate information… Many thanks for sharing this one.
A must read article!
My homepage :: plumbing company near me
Thank you for the auspicious writeup. It in truth was a entertainment account it.
Look advanced to more brought agreeable from you! However, how could we
communicate?
We stumbled over here different website and thought I should check things out.
I like what I see so now i am following you. Look forward to
looking over your web page again.
I have read so many posts on the topic of the blogger lovers except this piece
of writing is genuinely a nice post, keep it up.
Greetings! Very useful advice within this article!
It’s the little changes that will make the biggest changes.
Thanks for sharing!
Hi, for all time i used to check blog posts here early in the
daylight, as i enjoy to gain knowledge of more and more.
Thanks for the marvelous posting! I truly enjoyed reading
it, you’re a great author.I will be sure to bookmark your blog and may come back from now on. I want to encourage yourself to continue
your great job, have a nice morning!
Wow! At last I got a webpage from where I be able to actually obtain valuable data concerning my
study and knowledge.
We are a group of volunteers and opening a brand new scheme in our community. Your website offered us with useful info to work on. You have performed an impressive process and our whole community can be grateful to you.