//*Expiment-1 Q1 pushButton-LED State Control
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the Pin number of the pushbutton connected pin
const int ledPin = 13; // the Pin number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
------------------------------------------------X ------------------------------------------------
/* Q2 Blink -builtinLED
Turns an LED on for one second, then off for one second, repeatedly.
On-board LED you can control . it is attached to digital pin 13,
Change Delay to 1) delay(2000) 2) delay(250)
3) unequal delay delay(1000) and delay(250) Compile, upload and observe
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage
level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage
LOW
delay(1000); // wait for a second
}
------------------------------------------------X ------------------------------------------------
/* Q3 toggleSwitch-LED State Control Press-ON-Press-OFF
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the Pin number of the pushbutton connected pin
const int ledPin = 13; // the Pin number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
------------------------------------------------X ------------------------------------------------
// Experiment-4 Q4 Led State contro by Serial Port and Display on Serial
const int ledPin = 13;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("enter '1' or '0' from Keyboard");
}
void loop() { // put your main code here, to run repeatedly:
if(Serial.available()>0) {
char ch=Serial.read();
if(ch=='1') {
digitalWrite(ledPin, HIGH);
Serial.println(" LED ON");
}
else if(ch=='0') {
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
delay(1000); }
}
------------------------------------------------X ------------------------------------------------