drive motor while switch is pressed

Thread Starter

skyr6546

Joined Mar 22, 2019
73
I am trying to write program to drive motor while switch is pressed but if the switch is not pressed. don't turn motor instead sound buzzer for few seconds

Code:
//8051 and keil compiler 

#include <REG51.h> 
                  
sbit Switch   = P0^1;    //  switch connected to P0.1

sbit Motor    = P2^0;    // motor  connected to p2.0

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

#define  ON    1

#define  OFF   0

/* This is delay function */
void Delay (unsigned int n)
{
    unsigned int i;
   
  for  (i = 0; i <n; i++);
  {
  }
}
void main (void)
{    
        while(1) 
       {
             while (Switch == ON)      // switch is pressed
                {
                   Motor = ON;         // Turn ON Motor
                }
             
                              Buzzer = ON;
      
                                Delay(5000); 

                                Buzzer = OFF;
                            
       }
}
Is there any logic error in my code that may be improved ?
 

AlbertHall

Joined Jun 4, 2014
12,345
As it is, consider what will happen when the switch is not pressed. The loop will always enter the 'buzzer on, delay, buzzer off' code so you will get almost continuous buzzer sound. You want the buzzer to sound only once when the button is released. You could use a boolean variable which is set if the button is pressed, then only run the buzzer code if this variable is set and then clear it.
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
You want the buzzer to sound only once when the button is released. You could use a boolean variable which is set if the button is pressed, then only run the buzzer code if this variable is set and then clear it.
Hi @AlbertHall

I am using keil compiler I searched for boolean data type but I don't understand how to use boolean data type in keil compiler
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
You could achieve the same by setting a variable to '0' and '1'.
I am not familiar with the keil compiler. It seems you need to include <stdbool.h>.
http://www.keil.com/support/man/docs/armcc/armcc_chr1359124241645.htm

You could achieve the same by setting a variable to '0' and '1'.
okay I am trying here
Code:
#include <REG51.h> 
                  
sbit Switch   = P0^1;    //  switch connected to P0.1

sbit Motor    = P2^0;    // motor  connected to p2.0

sbit Buzzer   = P2^0;    // Buzzer connected to p2.0
#define  ON    1

#define  OFF   0

/* This is delay function */
void Delay (unsigned int n)
{
    unsigned int i;
   
  for  (i = 0; i <n; i++);
  {
  }
}
void main (void)
{    
   
     int boolean_variable;
   
        while(1) 
       {
             while (Switch == ON)      // switch is pressed
                {
                   Motor = ON;         // Turn ON Motor
                       
                                    boolean_variable = 1;
                }
             
                       if (boolean_variable == 1 )
                           {
                                Buzzer = ON;
      
                                  Delay(5000); 

                                 Buzzer = OFF;
                                
                             }
                             boolean_variable = 0;
                            
       }
}
 

AlbertHall

Joined Jun 4, 2014
12,345
'boolean_variable = 0' needs to be inside the braces not outside.
'Motor = ON' turns on the motor. It will then stay on until 'Motor = OFF' which should be in the 'switch not pressed' section.
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
'boolean_variable = 0' needs to be inside the braces not outside.
'Motor = ON' turns on the motor. It will then stay on until 'Motor = OFF' which should be in the 'switch not pressed' section.
I hope this is okay
Code:
#include <REG51.h>
                 
sbit Switch   = P0^1;    //  switch connected to P0.1

sbit Motor    = P2^0;    // motor  connected to p2.0

sbit Buzzer   = P2^0;    // Buzzer connected to p2.0
#define  ON    1

#define  OFF   0

/* This is delay function */
void Delay (unsigned int n)
{
    unsigned int i;
  
  for  (i = 0; i <n; i++);
  {
  }
}
void main (void)
{   
  
     int boolean_variable;
  
        while(1)
       {
             while (Switch == ON)      // switch is pressed
                {
                   Motor = ON;         // Turn ON Motor
                      
                    boolean_variable = 1;
                }
            
                       if (boolean_variable == 1 )
                           {
                                  Motor = OFF;
                                  boolean_variable = 0;
                               
                                Buzzer = ON;
     
                                  Delay(5000);

                                 Buzzer = OFF;
                               
                             }                         
                           
       }
}
 
Top