Maker Pro
Maker Pro

Program to check three Switch button's

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Hello

I have three button's and two light's and one Fan. I want to make light1 ON if switch 1 is pressed otherwise turn of light 1. turn ON light2 ON if switch 2 is pressed otherwise turn of light 2. turn ON Fan 1 if switch 3 is pressed otherwise turn of Fan 1.

Code:
#include<89v51rx2.h>

sbit Light1 = P1^0;      /*set bit P1^0 to Light 1*/
sbit Light2 = P1^1;      /*set bit P1^1 to Light 2*/
sbit Fan1   = P1^2;      /*set bit P1^2 to Fan 1*/

sbit  Switch1 = P2^0;     /*set bit P2^0 to Switch 1*/
sbit  Switch2 = P2^1;     /*set bit P2^1 to Switch 2*/
sbit  Switch3 = P2^2;     /*set bit P2^2 to Switch 3*/
 
#define ON                 1
#define OFF                0
 
#define Switch_Closed      1
#define Switch_Open        0

void main (void)
{
    while (1)
    {
        
      if(Switch1 == ON)
      {
           Light1 == ON;
      }
      else
      {
           Light1 == OFF;
      }
      if(Switch2 == ON)
      {
           Light2 == ON;
      }
      else
      {
           Light2 == OFF;
      }
      if(Switch3 == ON)
      {
           Fan1 == ON;
      }
      else
      {
           Fan1 == OFF;
      }
    }
}

I just compiled program. there is no error. I am not sure Is my program doing that work?
 

Doug3004

Sep 5, 2014
119
Joined
Sep 5, 2014
Messages
119
I am not familiar with the microcontroller that this is written for ( 89v51rx2.h ? I found p89v51rx2.h) so I could not test this... (looks like a Philips / NXP P89V51RD2, in unknown environment)

It appears that the error of your logic is that each button is evaluated in every loop of the program, even if it is LOW. That is not correct, you should only test if each button is HIGH, and then have another inner if() statement to change the setting for the item that this button affects.

Also you need some way to make sure that once a button is pressed, it must return to LOW again before another HIGH signal will be accepted. A time delay of 1 second or so would do well enough.

Like so:

Code:
#include<89v51rx2.h>

sbit Light1 = P1 ^ 0;    /*set bit P1^0 to Light 1*/
sbit Light2 = P1 ^ 1;    /*set bit P1^1 to Light 2*/
sbit Fan1   = P1 ^ 2;    /*set bit P1^2 to Fan 1*/

sbit  Switch1 = P2 ^ 0;   /*set bit P2^0 to Switch 1*/
sbit  Switch2 = P2 ^ 1;   /*set bit P2^1 to Switch 2*/
sbit  Switch3 = P2 ^ 2;   /*set bit P2^2 to Switch 3*/

#define ON                 1
#define OFF                0

#define Switch_Closed      1
#define Switch_Open        0

void main (void)
{
  while (1)
  {
    /*
       You should only evaluate Light1 when Switch1 is pressed.
       You must ignore Switch1 when it is not pressed, since that is the normal state that it will be in.
    */
    if (Switch1 == ON)
    {
      /*
         The code below will toggle Light1 off or on, depending on what it is already set to.
      */
      if (Light1 == OFF)
      {
        Light1 == ON;
      }
      else
      {
        Light1 == OFF;
      }
      /*
         You also need some kind of delay here to prevent switch bouncing,
         but I don't know the microcontroller that this is written for
         so I cant do that part...
      */
    }

    /*
       the code for Switch2 and the fan are written the same...
    */

  }
}
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
It appears that the error of your logic is that each button is evaluated in every loop of the program, even if it is LOW. That is not correct, you should only test if each button is HIGH, and then have another inner if() statement to change the setting for the item that this button affects..
Thanks. think like There is a switch board in your house that has three buttons if you press button 1 the light 1 turn ON,if you press button 2 ,the light 2 turn ON and if your press button 3 Fan turn off. so any of them button press then turn or their respective device otherwise turn of device.

I have doubt I don't think the procedure you told me is correct for this situation.

Are you sure ?
 

Bluejets

Oct 5, 2014
7,095
Joined
Oct 5, 2014
Messages
7,095
Just wondering why you need a microcontroller when 3 momentary contact switches would do the same thing.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Just wondering why you need a microcontroller when 3 momentary contact switches would do the same thing.
I am learning programming for microcontroller I wrote program for that situation. I can use LED's instead of Light and Fan. My question is mainly based on coding.

Is my code working as I want to do ?
 

Doug3004

Sep 5, 2014
119
Joined
Sep 5, 2014
Messages
119
I am learning programming for microcontroller I wrote program for that situation. I can use LED's instead of Light and Fan. My question is mainly based on coding.

Is my code working as I want to do ?
Probably not. Do you have a microcontroller to run it on?

Also I think your #include statement is incorrect.
Google cannot find any file named "89v51rx2.h", but it finds a lot of files named "p89v51rd2.h".
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Probably not. Do you have a microcontroller to run it on?.
I done few changes I checked program on simulator. program is doing what I want to it
Code:
#include<REGX51.h>
 
sbit  Switch1 = P2^1;       //set bit P2^1 to Switch 1

sbit  Switch2 = P3^0;       //set bit P3^0 to Switch 2

sbit  Switch3 = P3^7;       // set bit P3^7 to Switch 3
 
sbit LED1 = P1^1;           //set bit P1^1 to LED 1

sbit LED2 = P1^4;           //set bit P1^4 to LED 2

sbit LED3 = P1^7;           //set bit P1^7 to LED 3
 
 
#define     Switch_Closed        1

#define     Switch_Open          0

#define     LED_ON               1

#define     LED_OFF              0

void initialize (void);
 
void initialize (void)
{
   LED1 = LED2 = LED3 = LED_OFF;
   Switch1 = Switch2 = Switch3 =  Switch_Open;
}

void main (void)
{
    initialize();
    
   while (1)
   {
      if (Switch1  ==  Switch_Closed)
      {
            LED1  =  LED_ON;
      }
      else
      {
           LED1  =  LED_OFF;
      }

      if (Switch2  ==  Switch_Closed)
      {
            LED2  =  LED_ON;
      }
      else
      {
           LED2  =  LED_OFF;
      }

      if (Switch3  ==  Switch_Closed)
      {
            LED3  =  LED_ON;
      }
      else
      {
           LED3  =  LED_OFF;
      }
   }
}
 
Last edited:

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
This can be done in another way

Code:
#include<REGX51.h>
 
sbit  Switch1 = P2^1;       //set bit P2^1 to Switch 1

sbit  Switch2 = P3^0;       //set bit P3^0 to Switch 2

sbit  Switch3 = P3^7;       // set bit P3^7 to Switch 3
 
sbit LED1 = P1^1;           //set bit P1^1 to LED 1

sbit LED2 = P1^4;           //set bit P1^4 to LED 2

sbit LED3 = P1^7;           //set bit P1^7 to LED 3
 
 
#define     Switch_Closed        1

#define     Switch_Open          0

#define     LED_ON               1

#define     LED_OFF              0

void initialize (void);
 
void initialize (void)
{
     LED1 = LED2 = LED3 = LED_OFF;
     Switch1 = Switch2 = Switch3 =  Switch_Open;
}

void main (void)
{
    initialize();
     
   while (1)
   {
         (Switch1  ==  Switch_Closed ) ?  ( LED1  =  LED_ON ): ( LED1  =  LED_OFF );
   
         (Switch2  ==  Switch_Closed ) ?  ( LED2  =  LED_ON ): ( LED2  =  LED_OFF );

         (Switch3  ==  Switch_Closed ) ?  ( LED3  =  LED_ON ): ( LED3  =  LED_OFF );
     
   }
}
 
Top