0% found this document useful (0 votes)
16 views3 pages

IOT Practical

The document contains several Arduino sketches demonstrating button and LED interactions. Each practical example shows different functionalities such as toggling an LED based on button presses, handling short and long presses, and controlling multiple LEDs. The code snippets include setup and loop functions with varying logic for reading button states and controlling LED outputs.

Uploaded by

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

IOT Practical

The document contains several Arduino sketches demonstrating button and LED interactions. Each practical example shows different functionalities such as toggling an LED based on button presses, handling short and long presses, and controlling multiple LEDs. The code snippets include setup and loop functions with varying logic for reading button states and controlling LED outputs.

Uploaded by

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

Practical-5.

1
int buttonstate=0;
void setup()
{
pinmode(2,INPUT);
pinmode(13,OUTPUT);
}
void loop()
{
buttonstate = digitalRead(2);
if(buttonstate==HIGH);
delay(100);
digitalwrite(2,LOW);
delay(100);
}
}
Practical-5.2:
int buttonstate=0;
void setup()
{
pinmode(2,INPUT);
pinmode(LED_BUILTIN,OUTPUT);
}
void loop()
{
buttonstate=digitalRead(2);
if(buttonstate==HIGH)
{
digitalWrite(LED_BUILTIN,HIGH);
}
else{
digitalWrite(LED__BUILTIN,LOW);
}
delay(10);
}
Practical-5.3:
int buttonstate=0;
void setup()
{
pinmode(2,INPUT);
pinmode(LED_BUILTIN,OUTPUT);
}
void loop()
{
buttonstate=digitalRead(2);
if(buttonstate==HIGH)
{
digitalWrite(LED_BUILTIN,LOW);
}
else{
digitalWrite(LED__BUILTIN,HIGH);
}
delay(10);
}
Practical-5.4:
int buttonstate=0;
void setup()
{
pinmode(2,INPUT);
pinmode(3,OUTPUT);
pinmode(12,OUTPUT);
}
void loop()
{
buttonstate=digitalread(2);
if(buttonstate==HIGH)
{
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}
else{
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
}
}
Practical-6.1:-
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
int lastButtonState = 0;
unsigned long pressStart = 0;
unsigned long pressDuration = 0;
bool ledState = false;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
pressStart = millis(); // Record the time when button is pressed
}
if (buttonState == HIGH && lastButtonState == LOW) {
pressDuration = millis() - pressStart; if (pressDuration < 1000) {
ledState = !ledState; // Toggle LED state
digitalWrite(ledPin, ledState ? HIGH : LOW); }
}
lastButtonState = buttonState;
}

6.2
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
int lastButtonState = 0;
unsigned long pressStart = 0;
unsigned long pressDuration = 0;
bool ledState = false;
const unsigned long longPressThreshold = 2000;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
pressStart = millis();
}
if (buttonState == HIGH && lastButtonState == LOW) {
pressDuration = millis() - pressStart;
if (pressDuration >= longPressThreshold) {
ledState = !ledState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
}
lastButtonState = buttonState;
}

You might also like