Switch on LED if Switch is pressed

Thread Starter

skyr6546

Joined Mar 22, 2019
70
I am trying to write best program to turn on LED if switch is pressed but if the switch is not pressed. don't turn LED

Code:
////8051 and keil compiler
#include <REG51.h> 
                  
sbit Switch   = P0^1;    //  switch connected to P0.1

sbit LED   = P2^0;    // LED connected to p2.0
/* This is delay function */
void Delay (unsigned int n)
{
    unsigned int i;
   
  for  (i = 0; i <n; i++);
  {
  }
}
void main (void)
{    
    while (1)   //infinite loop
       
    {
        if(Switch  == 0)   //If the switch is pressed
    {
       Delay(40);      //Switch Debounce
           
       if(Switch == 0)  //If the switch is still pressed
       {
         LED = 1;       //LED ON
         Delay(1000);    // Delay
         LED = 0;        //LED OFF
       }
       
       }
     }
}
Code:
#include <REG51.h> 
                  
sbit Switch   = P0^1;    //  switch connected to P0.1

sbit LED   = P2^0;      // LED connected to p2.0

#define TRUE  1

#define FALSE  0

void Delay (unsigned int n)
{
    unsigned int i;
   
  for  (i = 0; i <n; i++);
  {
  }
}

bit check_Switch(void)
    {  
   if(Switch == 0)        //If the switch is pressed
         {            
        Delay(40);        /*  */           
   
             if(Switch == 0)           /* check switch again */
      
             return TRUE;         /* switch is really pressed */     
   }
   return FALSE;
}
void main (void)
{    
    while (1)   //infinite loop
       
    {
       if (check_Switch == TRUE)
         {
               LED = 1;       //LED ON
         Delay(1000);    // Delay
         LED = 0;        //LED OFF
         }
       
      
     }
}
I have written code in two way which is best way to read the switch ?
 

djsfantasi

Joined Apr 11, 2010
5,909
I am trying to write best program to turn on LED if switch is pressed but if the switch is not pressed. don't turn LED

Code:
////8051 and keil compiler
#include <REG51.h>
                 
sbit Switch   = P0^1;    //  switch connected to P0.1

sbit LED   = P2^0;    // LED connected to p2.0
/* This is delay function */
void Delay (unsigned int n)
{
    unsigned int i;
  
  for  (i = 0; i <n; i++);
  {
  }
}
void main (void)
{   
    while (1)   //infinite loop
      
    {
        if(Switch  == 0)   //If the switch is pressed
    {
       Delay(40);      //Switch Debounce
          
       if(Switch == 0)  //If the switch is still pressed
       {
         LED = 1;       //LED ON
         Delay(1000);    // Delay
         LED = 0;        //LED OFF
       }
      
       }
     }
}
Code:
#include <REG51.h>
                 
sbit Switch   = P0^1;    //  switch connected to P0.1

sbit LED   = P2^0;      // LED connected to p2.0

#define TRUE  1

#define FALSE  0

void Delay (unsigned int n)
{
    unsigned int i;
  
  for  (i = 0; i <n; i++);
  {
  }
}

bit check_Switch(void)
    { 
   if(Switch == 0)        //If the switch is pressed
         {           
        Delay(40);        /*  */          
  
             if(Switch == 0)           /* check switch again */
     
             return TRUE;         /* switch is really pressed */    
   }
   return FALSE;
}
void main (void)
{   
    while (1)   //infinite loop
      
    {
       if (check_Switch == TRUE)
         {
               LED = 1;       //LED ON
         Delay(1000);    // Delay
         LED = 0;        //LED OFF
         }
      
     
     }
}
I have written code in two way which is best way to read the switch ?
Your first program always turns the LED off, after turning it on briefly when the switch is pressed.

I like your second program better, but including the following two lines,
Code:
Delay(1000)
LED=0
Once you press the switch, the led is only on for a brief time and then turns off, even if the switch is pressed!

You need to add an ‘else’ clause to your ‘if’ statement. Like this,
Code:
if (check_Switch == TRUE)
  {
   LED=1
}
else
   {
   LED=0
}
 
Top