0% found this document useful (0 votes)
20 views8 pages

Experiment 9

Uploaded by

darshitbhuva1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Experiment 9

Uploaded by

darshitbhuva1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Name: Darshit Bhuva

Roll No: CE022


ID No: 20CEUOF014

Experiment No: 09

AIM: Applications using Push Button with Arduino


Task-1: Turn on the onboard LED using push button.

Code:
int ledPin = 13;
int inPin = 8;
int value;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);

void loop()
{
value = digitalRead(inPin);

if(value == HIGH){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}

}
Task-2 : Turn on external LED using push button.

Code:
int inPin = 8;
int outPin = 2;
void setup()
{
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
}

void loop()
{
int value = digitalRead(inPin);

if (HIGH == value)
{
digitalWrite(outPin, HIGH);
}
else
{
digitalWrite(outPin, LOW);
}
}
Task-3: When the Button is pressed LED should be ON if button is
pressed again LED should be off

Code:
int inPin = 8;
int outPin = 2;
int button_push = 0;
void setup()
{
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
}

void loop()
{

int value = digitalRead(inPin);

if (1 == value)
{
if (button_push == 0)
{
digitalWrite(outPin, HIGH);
button_push = 1;
}
else
{
digitalWrite(outPin, LOW);
button_push = 0;
}
}
}
Task-4: Make the two push buttons a “gas” and “brake” button. The “gas” button should
speed up the blinking rate of the LED, and the “brake” button should slow it down.

Code:
// C++ code
//
int inPin1 = 8;
int inPin2 = 7;
int outPin = 2;
int gas = 0;
int brk = 0;
int time = 1000;

void setup()
{
pinMode(outPin, OUTPUT);
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
}

void loop()
{
int value1 = digitalRead(inPin1);
int value2 = digitalRead(inPin2);

if (value1 == 1)
{
if (gas == 0)
{
time = 200;
gas = 1;
}
else
{
time = 200;
}
}

else if (value2 == 1)
{
if (brk == 0)
{
time = 1000;
brk = 1;
}
else
{
time = 1000;
}
}

digitalWrite(outPin, HIGH);
delay(time);
digitalWrite(outPin, LOW);
delay(time);
}

Task-5 and 6: Connect two push buttons in series and parallel - take
observations .

First-Series:
Code:
// C++ code
//
int outpin = 7;
int inpin = 3;
void setup()
{
pinMode(outpin, OUTPUT);
pinMode(inpin, INPUT);
}

void loop()
{
bool value = digitalRead(inpin);
if (HIGH == value)
{
digitalWrite(outpin, HIGH);
}
else
{
digitalWrite(outpin, LOW);
}
}

Second- Parallel:
Code:
// C++ code
//
void setup()
{
pinMode(2, OUTPUT);
pinMode(8, INPUT);
}

void loop()
{
int button1 = digitalRead(8);
int button2 = digitalRead(8);

if (button1 || button2)
{
digitalWrite(2, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(2, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
}

You might also like