In: Electrical Engineering
4. Write a code that S1(push button -P1.1) turns LED1 (Red LED- P1.0) ON and stays on when you push once and turns off and stays OFF when you push 3 times. This should repeat forever.
5. Write a code that S2(push button -P1.2) turns LED2 (Red LED- P9.7) ON and stays ON when you push once and turns OFF and stays OFF when you push 3 times. This should repeat five times.
6. Write a code that LED2 (Red LED- P9.7) ON when as long as you push S2(push button - P1.2) and LED1 (Red LED- P1.0) on as long as S2(push button -P1.2) not pushed.
#include <stdio.h>
enum LED1{
ON,
OFF
}led; //enum variable for LED ON/OFF
int pushcount; //counter to count push number
int flag; //a flag to check LED
switch off (Q no 2)
int button; //global variable for
push button
//use your own variable to get pushbutton
value
//Function to turn LED ON/OFF
void ledOnOff(int pushbutton)
{
if(led==OFF)
{
if(pushbutton==1 &&
flag!=1) //flag is used to check if LED turned
Off
{
led=ON;
pushcount=0;
}
}
else
{
if(pushbutton==1)
{
pushcount++;
if(pushcount==3)
{
led=OFF;
pushcount=0;
}
}
}
}
//Function to switch LED OFF
void ledSwitch(int pushbutton)
{
if(led==OFF)
{
if(pushbutton==1)
{
pushcount++;
if(pushcount==5
&& flag!=1)
{
led=ON;
pushcount=0;
}
}
}
else
{
int i;
for(i=0;i<1000;i++);
led=OFF;
flag=1;
}
}
int main()
{
led=OFF; //initialize LED to OFF
pushcount=0; //initialize pushcount to
0
flag=0; //initialise
flag to 0
while(1) //continuous loop
{
ledOnOff(button);
ledSwitch(button);
}
return 0;
}