0% found this document useful (0 votes)
15 views6 pages

Practice Programs

The document contains several Arduino sketches demonstrating different ways to control LEDs using inputs like light sensors, buttons, and potentiometers. The sketches include turning an LED on or off based on light levels, fading an LED brightness gradually, turning an LED on when a button is held, and controlling two LED brightnesses using a potentiometer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Practice Programs

The document contains several Arduino sketches demonstrating different ways to control LEDs using inputs like light sensors, buttons, and potentiometers. The sketches include turning an LED on or off based on light levels, fading an LED brightness gradually, turning an LED on when a button is held, and controlling two LED brightnesses using a potentiometer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ON OFF LED USING LDR

19 December 2023 12:08 PM

int led = 33;


int lightSensor = A0;
int lightReading = 0;
void setup () {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop () {
lightReading = analogRead(lightSensor);
Serial.println(lightReading, DEC);

if (lightReading < 200)


{
digitalWrite(led, LOW);
Serial.println("light OFF");
}
else
{
digitalWrite(led, HIGH);
Serial.println("light ON");
}

delay(100);
}

Fading of led

int led = 33;


int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(10);
}

New Section 2 Page 1


HOLD TO TURN ON LED IN PUSH BUTTON

#define LED_PIN 33
#define BUTTON_PIN 32
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(LED_PIN, LOW);
}
else {
digitalWrite(LED_PIN, HIGH);
}
}

DECREASING BLINKING SPEED GRADUALLY

int ledPin = 33;


void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
for (int x = 0; x < 10000; x++)
{
digitalWrite(ledPin, HIGH);
delay(x);
digitalWrite(ledPin, LOW);
delay(x);
}
}

New Section 2 Page 2


INCREASING BLINKING SPEED GRADUALLY

int ledPin = 33; void setup()


{
pinMode(ledPin, OUTPUT);
}
void loop()
{
for (int x = 250; x > 0; x--)
{
digitalWrite(ledPin, HIGH);
delay(x);
digitalWrite(ledPin, LOW);
delay(x);
}
}

Bluetooth with led


BluetoothSerial SerialBT;
const int ledPin = 33;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
SerialBT.begin("ESP32_led_control");
Serial.println("Bluetooth Device is Ready to Pair");
}
void loop() {
if (SerialBT.available()) {
char command = SerialBT.read();
if (command == '1') {
digitalWrite(ledPin, HIGH);
SerialBT.println("LED ON");
Serial.println("LED ON");
} else if (command == '0') {
digitalWrite(ledPin, LOW);
SerialBT.println("LED OFF");
Serial.println("LED OFF");
}
}
delay(100);
}

New Section 2 Page 3


POTENTIOMETER COMBINATION
WITH LED WRT ITS VALUES

#define POTENTIOMETER_PIN 36
#define LED1_PIN 33
#define LED2_PIN 32
void setup() {
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int analogValue = analogRead(POTENTIOMETER_PIN);
Serial.print("Potentiometer Value: ");
Serial.println(analogValue);

if (analogValue < 1000) {


digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, HIGH);
Serial.println("LED 1 ON, LED 2 ON");
} else if (analogValue >= 1001 && analogValue <= 2000) {
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, LOW);
Serial.println("LED 1 ON, LED 2 OFF");
} else if (analogValue >= 2001 && analogValue <= 3000) {
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, HIGH);
Serial.println("LED 1 OFF, LED 2 ON");
} else {
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
Serial.println("LED 1 OFF, LED 2 OFF");
}
delay(1000);
}

4 PRESS ON 3 PRESS OFF


New Section 2 Page 4
const int buttonPin = 32;
const int ledPin = 33;
int buttonState = HIGH;
int lastButtonState = HIGH;
int buttonPressCount = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
buttonPressCount++;
Serial.println("Button Pressed");
if (buttonPressCount % 7 == 1 || buttonPressCount % 7 == 2 ||
buttonPressCount % 7 == 3 ) {
digitalWrite(ledPin, HIGH);
Serial.println("Turning OFF LED");
} else {
digitalWrite(ledPin, LOW);
Serial.println("Turning ON LED");
}
}
lastButtonState = buttonState;
delay(50); // Debounce delay

DECREASING THE BRIGHTNESS


GRADUALLY
const int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness);
delay(10);
}
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness);
delay(10);
}
}

CONTROL BRIGHTNESS WITH POT


New Section 2 Page 5
CONTROL BRIGHTNESS WITH POT

const int potPin=36;


const int ledPin1=33;
const int ledPin2=4;
void setup()
{

pinMode(33, OUTPUT);
pinMode(4, OUTPUT);
pinMode(36, INPUT);
Serial.begin(115200);
}
void loop()
{
int potValue = analogRead(potPin);
int brightness = map(potValue, 0, 2000, 0, 255);
analogWrite(ledPin1, brightness);
delay(10);
Serial.println(potValue);
Serial.println(brightness);
if (potValue==2000)
{
digitalWrite(4,HIGH);
}
else
{
digitalWrite(4,LOW);
}
}

New Section 2 Page 6

You might also like