Experiment 9
Experiment 9
Experiment No: 09
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()
{
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)
}
}