I had been reading about conditional statement (if, else or if else or if or if) and Now I want to use conditional statement in Program. Let’s suppose, four switches and one LED connected to microcontroller. I wrote some c programs with example but I am not sure whether its right or wrong. so if there is any mistake please let me know.
If press switch1, LED should be ON and If don’t press switch1, LED should be OFF
If press switch2, LED should be ON and If don’t press switch2, LED should be OFF
If press switch3, LED should be ON and If don’t press switch3, LED should be OFF
If press switch4, LED should be ON and If don’t press switch4, LED should be OFF
C program sing If and else condition
C program with If and if else condition
If press switch1, LED should be ON
If press switch2, LED should be ON
If press switch3, LED should be ON
If press switch4, LED should be ON
If don’t press any switches, LED should be OFF
C programming using if if if and else conditions
I think my last program is not in correct way. if you find anywhere wrong please let me know
If press switch1, LED should be ON and If don’t press switch1, LED should be OFF
If press switch2, LED should be ON and If don’t press switch2, LED should be OFF
If press switch3, LED should be ON and If don’t press switch3, LED should be OFF
If press switch4, LED should be ON and If don’t press switch4, LED should be OFF
C program sing If and else condition
Code:
#include<REGX51.h>
#define LED_ON (1)
#define LED_OFF (0)
#define Switch1_ON (1)
#define Switch2_ON (1)
#define Switch3_ON (1)
#define Switch4_ON (1)
/*set bit to LED*/
sbit LED = P1^0;
/*set bits to Switch*/
sbit Switch1 = P2^0;
sbit Switch2 = P2^1;
sbit Switch3 = P2^2;
sbit Switch4 = P2^3;
void main()
{
while (1)
{
if(Switch1 == Switch1_ON)
{
LED = LED_ON;
}
else
{
LED = LED_OFF;
}
if(Switch2 == Switch2_ON)
{
LED = LED_ON;
}
else
{
LED = LED_OFF;
}
if(Switch3 == Switch3_ON)
{
LED = LED_ON;
}
else
{
LED = LED_OFF;
}
if(Switch4 == Switch4_ON)
{
LED = LED_ON;
}
else
{
LED = LED_OFF;
}
}
}
Code:
#include<REGX51.h>
#define LED_ON (1)
#define LED_OFF (0)
#define Switch1_ON (1)
#define Switch2_ON (1)
#define Switch3_ON (1)
#define Switch4_ON (1)
#define Switch1_OFF (0)
#define Switch2_OFF (0)
#define Switch3_OFF (0)
#define Switch4_OFF (0)
/*set bit to LED*/
sbit LED = P1^0;
/*set bits to Switch*/
sbit Switch1 = P2^0;
sbit Switch2 = P2^1;
sbit Switch3 = P2^2;
sbit Switch4 = P2^3;
void main()
{
while (1)
{
if(Switch1 == Switch1_ON)
{
LED = LED_ON;
}
else if (Switch1 == Switch1_OFF)
{
LED = LED_OFF;
}
if(Switch2 == Switch2_ON)
{
LED = LED_ON;
}
else if (Switch2 == Switch2_OFF)
{
LED = LED_OFF;
}
if(Switch3 == Switch3_ON)
{
LED = LED_ON;
}
else if (Switch3 == Switch4_OFF)
{
LED = LED_OFF;
}
if(Switch4 == Switch4_ON)
{
LED = LED_ON;
}
else if (Switch4 == Switch4_OFF)
{
LED = LED_OFF;
}
}
}
If press switch2, LED should be ON
If press switch3, LED should be ON
If press switch4, LED should be ON
If don’t press any switches, LED should be OFF
C programming using if if if and else conditions
Code:
#include<REGX51.h>
#define LED_ON (1)
#define LED_OFF (0)
#define Switch1_ON (1)
#define Switch2_ON (1)
#define Switch3_ON (1)
#define Switch4_ON (1)
/*set bit to LED*/
sbit LED = P1^0;
/*set bits to Switch*/
sbit Switch1 = P2^0;
sbit Switch2 = P2^1;
sbit Switch3 = P2^2;
sbit Switch4 = P2^3;
void main()
{
while (1)
{
if(Switch1 == Switch1_ON)
{
LED = LED_ON;
if(Switch2 == Switch2_ON)
{
LED = LED_ON;
if(Switch3 == Switch3_ON)
{
LED = LED_ON;
if(Switch4 == Switch4_ON)
{
LED = LED_ON;
}
}
}
}
else
{
LED = LED_OFF;
}
}
}
Last edited: