How to use C# if else Statement

Hello friends, I hope you all are fine and having fun with your lives. Today, I am going to show you How to use C# if else statement. Previously we have seen the Introduction of C# Windows Forms an then we have also discussed different data types and variables used for programming in C#, you can read these tutorials on C# Tutorial page.

So, before starting this C# if else tutorial, I would suggest you to first read all of those basic C# tutorials so that you know how to use different data types. Before starting this tutorial, you should also read How to add C# Control in Windows Form because we are gonna use three C# controls in this tutorial.So, let's get started with How to use C# if else statement:

How to use C# if else Statement ???

  • First of all, let's have an overview of C# if else statement.
Overview
  • If Else Statements are named as conditional logic.
  • C# If statements are essential for any kind of project , IF statement is the most basic conditional logic of C# and is used in almost every project.
  • It acts as if and then you have to specify some statement which is a check statement. In simple words it acts as if this condition is true then do this task. :)
  • Let's have a look at its pseudo code:

IF ( Condition is TRUE )  ====>  Then do this TASK.     

  • Now let me give you an example suppose you are designing some calculator then it must have some buttons on it like Multiply.
  • And you want to multiply any two integers when you click this MULTIPLY button.
  • Now in your code you need to add something like:

IF ( MULTIPLY button is pressed ) ====> Then multiply the two Integer variables

  • So, you can see in the above C# IF statement that clicking of MULTIPLY button is the condition and the multiplication of two integers is the TASK.
  • This C# IF Statement is making the relation between the Condition and the Task.
  • So, now let's have a look at the real C# IF Statement syntax:
C# IF Syntax
  • In C# IF Statements, first of all we place the word "IF".
  • After that, we place the condition in small brackets ( ), the condition must be a comparison which should be either true or false. We will shortly have a look at some example.
  • Now after specifying the condition, next thing we need to do is to specify the task, which comes in curly brackets { }.
  • So, the overall if statement looks something as shown below:
If ( CONDITION comes here ) { TASKS are specified here. }
  • Now, I hope that you have got the complete understanding of this C# IF Statement so let's design a simple example so that I can show you How to use this IF Statement.
Example
  • Now let's design a simple C# Windows Form application and add some Control in it.
  • I have added one Button, one TextBox and one Label and it looks like as shown in below figure:
  • Now I have created a small IF condition that if TEP is written in the Text box then the Label Text will be changed to My Blog.
  • Its just a random logic but quite simple so I thought that it will better explain the logic.
  • So, when you click this button then if TEP is written in the Text Box then we will get My Blog as shown in below figure:
  • You can see in the above figure that when I click the button then it checked the textbox and because TEP is written there that's why Label text is changed to My Blog.
  • Here's the code for the button click function which is quite simple:
if (txtClick.Text == "TEP")
{
    label1.Text = "My Blog";
}
  • So, that's our IF logic as I discussed earlier the condition is in ( ) small brackets and then in curly brackets { } we have our task.
  • But there's a little problem in above condition that I have mentioned to display My blog on TEP but I haven't mentioned what to type when there's no TEP in Text Box.
  • So, what should happen when there's something else in Text box.
  • That's where we encountered with ELSE Statement.
  • In ELSE we specify that TASK which will occur when IF condition is FALSE.
  • So ,let's change our code a little:
if (txtClick.Text == "TEP")
{
   label1.Text = "My Blog";
}
else
{
   label1.Text = " NOT my Blog";
}
  • Now you can see in the above code that I have added the ELSE statement, in which I have mentioned that Not my Blog.
  • So, now when the IF condition is TRUE then Label text will be My Blog and when its FALSE then ELSE will work and we will get NOT My Blog.
  • Check the below image:
  • So, that's how C# if else statement works.
That's all for today. I hope you have enjoyed today's tutorial. I will cover more conditional logic in the coming tutorials. Till then Take care and have fun !!! :)

LPG Gas Leak Detector using Arduino

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a new project named LPG Gas Leak Detector using Arduino in Proteus ISIS. Before reading this tutorial, you must first download the Gas Sensor Library for Proteus because we are gonna use that Library and will simulate the Gas Sensor in Proteus.

In this library you will find eight sensors and all of them works exactly the same so that's why we are gonna use one of them. For LPG Gas Leak Detector Project I have used MQ-2 sensor which is used for detection of LPG gas. I have also used Arduino UNO board which you can simulate in Proteus using Arduino Library for Proteus. Moreover, I have also placed an LCD which will display either LPG gas Leak Detected or not. So, let's get started with LPG Gas Leak Detector using Arduino in Proteus ISIS.

LPG Gas Leak Detector using Arduino in Proteus ISIS

  • First of all, download the Gas Sensor Library for Proteus and install it in your Proteus software so that you can it in Proteus.
  • After installing the Gas Sensor Library, now download the LPG Gas Leak Detector Project's simulation and programming code by clicking the below button:

Download Proteus Simulation & Code

  • Now, let's design this project so that you can get a better idea of how it works.
  • First of all, design a small circuit in your Proteus software as shown in below figure:
  • Now you can see in the above figure that I have used Arduino UNO board along with 20 x 4 LCD and Gas Sensor MQ-2.
  • You can use this LCD by download this New LCD Library for Proteus.
  • Next thing you need to do is to download the below code and get your hex file.
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

int Gas = 7;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  // Print a message to the LCD.

  lcd.setCursor(0,0);
  lcd.print("Gas Detected :");
  
  lcd.setCursor(1,2);
  lcd.print("www.TheEngineering");
  lcd.setCursor(4,3);
  lcd.print("Projects.com");
  pinMode(Gas , INPUT);
}

void loop() {
  
  if(digitalRead(Gas) == HIGH){lcd.setCursor(14,0);lcd.print(" Yes");}
  if(digitalRead(Gas) == LOW){lcd.setCursor(14,0);lcd.print(" No ");}
 
}
  • If you don't know about Hex file then read How to Get Hex file from Arduino Software.
  • Upload this Hex File in your Proteus Arduino software and then run your simulation.
  • If everything goes fine then you will get results as shown in below figure:
  • So, you can see in the above figure that when Gas Sensor is HIGH then its written on the LCD that Gas Detected: Yes.
  • Here's a video which will explain this LPG Gas Leak Detection using Arduino in Proteus ISIS:
So, that's all for today. I hope you have enjoyed this project named LPG Gas Leak Detection using Arduino in Proteus ISIS. Will meet you guys in the next tutorial. Till then take care and have fun !!! :)

Line Following Robotic Waiter using Arduino

Hello friends, I hope you all are fine and having fun with your lives. Today, I am going to share a new project which is Line Following Robotic Waiter. I hope you guys are gonna enjoy it. I have shared all the details below so that if you wanna design it then you can easily do it. I have designed this project for a client and it worked quite perfectly so I thought to share it with you guys as well.

I have designed this Line Following Robotic Waiter using Arduino UNO board. I have also shared the code below and have given all the instructions but still if you got into any problem then ask in comments and I will solve your problems. I have also shared a video below which will show you the working of Line Following robotic Waiter. So, let's get started with it:

Line Following Robotic Waiter

First of all, let's have an overview of this Line Following Robotic Waiter:

Overview
  • In this project, I have designed an arena which has four tables on it as shown in below figure:
  • The robot will start from the Table 1 side and whenever someone call it from any table then it will reach that table and take the order.
  • After taking the order it will move back and will reach to the starting point again and wait for the next table call.
  • Now let's have a look at the components required to design thi Line Following Robotic Waiter:
Components
  • Let's have a look at the components list, which is required for designing this Robot. Here it is:
  • Arduino UNO
  • DC Motors
  • RF Module
  • 2 Relay Board
  • IR Sensors
  • These are the components required in order to design this Line following robotic waiter.
Mechanical Design
  • I have designed a three wheeler robot in which there were two wheels at the front side with DC motors while a free caster wheel at the back side.
  • I have used Acrylic Sheet as the body of the robot.
  • The DC Motor I have used for designing this project is shown in the below figure:
  • The caster wheel used is shown in below figure:
  • The coupled DC gear Motor with wheel is shown in below figure:
  • So I have designed two of such coupled motors and then combining all the above things together, we finally designed our Line Following Robotic Waiter as shown in below figure:
  • Now our mechanical Design is ready so let's design the electronic hardware:
Electronic Circuit Design
  • First of all, I have designed the DC Motor Driver which is also called 2 Relay Board.
  • This circuit diagram is shown in below figure:
  • Now in order to apply the signals to move the Motors I have used Arduino UNO board.
  • Moreover I have placed four IR sensors below this robot.
  • Two of these IR sensors are used for line tracking while the remaining two were placed on the sides for detecting the tables on both sides.
  • I have also designed a power supply to convert 12V into 5V.
  • The circuit diagram of power supply is as follows:
  • So, now let's have a look at the Arduino code required for Line Following Robotic Waiter:
Arduino Code
  • Here's the Arduino Code requried for Line Following Robotic Waiter.
#define motorL1 8
#define motorL2 9
#define motorR1 10
#define motorR2 11

#define PwmLeft 5
#define PwmRight 6

#define SensorR 2
#define SensorL 3
#define Sensor3 A0
#define Sensor4 A1

#define TableA A4
#define TableB A2
#define TableC A5
#define TableD A3

int OriginalSpeed = 200;
int TableCount = 0;
int TableCheck = 0;
int RFCheck = 10;

void setup() 
{
  Serial.begin (9600);
 
  pinMode(motorR1, OUTPUT);
  pinMode(motorR2, OUTPUT);
  pinMode(motorL1, OUTPUT);
  pinMode(motorL2, OUTPUT);
  
  pinMode(PwmLeft, OUTPUT);
  pinMode(PwmRight, OUTPUT);
  
  pinMode(SensorL, INPUT);
  pinMode(SensorR, INPUT);
  pinMode(Sensor3, INPUT);
  pinMode(Sensor4, INPUT);
  
  pinMode(TableA, INPUT);
  pinMode(TableB, INPUT);
  pinMode(TableC, INPUT);
  pinMode(TableD, INPUT);
  
  MotorsStop();
  
  analogWrite(PwmLeft, 0); 
  analogWrite(PwmRight, 0);
  delay(2000); 
 // Serial.println("fghfg");
  
}

void loop() {
  MotorsForward();
if((digitalRead(Sensor3) == LOW) && (TableCheck == 0)){TableCount++; TableCheck = 1;}
if((digitalRead(Sensor3) == HIGH) && (TableCheck == 1)){TableCheck = 2;}

if((digitalRead(Sensor3) == LOW) && (TableCheck == 2)){TableCount++; TableCheck = 3;}
if((digitalRead(Sensor3) == HIGH) && (TableCheck == 3)){TableCheck = 4;}
if((digitalRead(Sensor3) == LOW) && (TableCheck == 4)){TableCount++; TableCheck = 5;}
if((digitalRead(Sensor3) == HIGH) && (TableCheck == 5)){TableCheck = 0;}

if(digitalRead(TableA) == HIGH){RFCheck = 1;}
if(digitalRead(TableB) == HIGH){RFCheck = 2;}
if(digitalRead(TableC) == HIGH){RFCheck = 3;}
if(digitalRead(TableD) == HIGH){RFCheck = 4;}

if(RFCheck == TableCount){Table1();}
PIDController();
}

void MotorsBackward()
{
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, LOW);
    digitalWrite(motorR1, HIGH);
    digitalWrite(motorR2, LOW);
}

void MotorsForward()
{
    digitalWrite(motorL1, LOW);
    digitalWrite(motorL2, HIGH);
    digitalWrite(motorR1, LOW);
    digitalWrite(motorR2, HIGH);
}

void MotorsStop()
{
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, HIGH);
    digitalWrite(motorR1, HIGH);
    digitalWrite(motorR2, HIGH);
}

void MotorsLeft()
{
    analogWrite(PwmLeft, 0); 
  analogWrite(PwmRight, 0);
    digitalWrite(motorR1, HIGH);
    digitalWrite(motorR2, HIGH);
    digitalWrite(motorL1, LOW);
    digitalWrite(motorL2, HIGH);
}

void MotorsRight()
{
      analogWrite(PwmLeft, 0); 
  analogWrite(PwmRight, 0);
    digitalWrite(motorR1, LOW);
    digitalWrite(motorR2, HIGH);
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, HIGH);
}

void Motors180()
{
    analogWrite(PwmLeft, 0); 
    analogWrite(PwmRight, 0);
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, LOW);
    digitalWrite(motorR1, LOW);
    digitalWrite(motorR2, HIGH);
}

void PIDController()
{
  if(digitalRead(SensorL) == HIGH){analogWrite(PwmRight, 250);analogWrite(PwmLeft, 0);}
  if(digitalRead(SensorR) == HIGH){analogWrite(PwmLeft, 250);analogWrite(PwmRight,0);}
  if((digitalRead(SensorL) == LOW) && (digitalRead(SensorR) == LOW)){analogWrite(PwmRight, 0);analogWrite(PwmLeft, 0);}
}

void Table1()
{
   TableCount = 0;
   MotorsRight();
   delay(4000);
   while(digitalRead(SensorR) == HIGH);
   while(digitalRead(SensorL) == HIGH);
   while(1)
   {
       MotorsForward();
       PIDController();
       if(digitalRead(Sensor3) == LOW){break;}
   }
   MotorsStop();
   delay(1000);
   Motors180();
   delay(2000);
   while(digitalRead(Sensor3) == HIGH);
   while(digitalRead(Sensor3) == LOW);
   while(digitalRead(SensorL) == HIGH);
   //delay(500);
   while(1)
   {
   MotorsForward();
   PIDController();
   if(digitalRead(Sensor3) == LOW){break;}
   }
   //delay(1000);
   MotorsLeft();
   delay(4000);
   while(digitalRead(SensorL) == HIGH);
   while(digitalRead(SensorR) == LOW);
   while(1)
   {
       MotorsForward();
       PIDController();
       if(digitalRead(Sensor3) == LOW){break;}
   }
   MotorsStop();
   delay(1000);
}
  • This code will not work for you exactly.
  • The robot is following the line so I have placed some value for IR sensors so that my robot can follow.
  • When right IR sensor is HIGH then right motor moves a little faster and left motor slows down.
  • In this way I am making it to follow the line so you have to change such values in the code.
  • If you got into any issue regarding this project then add us on Skype and we will help you out.
  • Here's our final robot looks like:
  • Here's the video of Line following Robotic Waiter which will give you the better idea of how it worked:
That's all for today, I hope you have enjoyed today's project. Will meet you guys in the next tutorial. till then take care and have fun !!! :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir