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
Is there any logic error in my code that may be improved ?
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;
}
}