Study the Digital Input, Digital Output and Data Communication functions of the
ID: 3788849 • Letter: S
Question
Study the Digital Input, Digital Output and Data Communication functions of the microcontroller with the help of the two built-in example programs available in File > Examples > Basics menu, as shown below. Combine the two programs shown below into a single program so that digital input is used to control the digital output function. The LED and buzzer output devices are turned on by pressing the switch and are turned off by releasing the switch. Implement data communication between the microcontroller and the PC and display digital input values (logic states of the switch) in the Serial Monitor window on the PC.Explanation / Answer
2)
int pushButton=2;
int led=13;
int buzzer=8; //connecting buzzer to pin number 8
void setup()
{
Serial.Begin(9600);
pinMode(pushButton,INPUT);
pinMode(led,OUTPUT);
pinMode(buzzer,OUTPUT)
}
void loop()
{
int ButtonState=digitalRead(pushButton)
if(ButtonState)
{
digitalWrite(led,HIGH);
tone(buzzer,100,500) //this function will vibrate the buzzer attached to pin 8 with 100 hertz untill 500milli seconds
}
else
{
digitalWrite(led,LOW)
}
delay(1000);
}
3)
int pushButton=2;
void setup()
{
serial.begin(9600);
pinMode(pushButton,INPUT);
}
void loop()
{
int buttonstate=digitalRead(pushButton);
if(pushButton)
serial.println("Switch is ON");
else
serial.println("Switch is OFF");
delay(1000);
}