0% found this document useful (0 votes)
8 views2 pages

Led 2

f
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)
8 views2 pages

Led 2

f
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/ 2

#include <SoftwareSerial.

h>

// Create software serial connection for HC-05


SoftwareSerial BTSerial(10, 11); // RX | TX for Arduino Nano

// Define LED pins for each shelf


const int ledShelf1 = 2;
const int ledShelf2 = 3;
const int ledShelf3 = 4;
const int ledShelf4 = 5;
const int ledShelf5 = 6;

// Store the time when the LED was turned on


unsigned long ledOnTime[5] = {0, 0, 0, 0, 0};
bool ledState[5] = {false, false, false, false, false}; // Track LED states

// Duration for which the LED should remain ON


const unsigned long duration = 20000; // 20 seconds

void setup() {
// Start the Bluetooth serial communication
BTSerial.begin(9600);

// Define LED pins as OUTPUT


pinMode(ledShelf1, OUTPUT);
pinMode(ledShelf2, OUTPUT);
pinMode(ledShelf3, OUTPUT);
pinMode(ledShelf4, OUTPUT);
pinMode(ledShelf5, OUTPUT);

// Initialize all LEDs as OFF


digitalWrite(ledShelf1, LOW);
digitalWrite(ledShelf2, LOW);
digitalWrite(ledShelf3, LOW);
digitalWrite(ledShelf4, LOW);
digitalWrite(ledShelf5, LOW);
}

void loop() {
// Check if any data is available from the Bluetooth module
if (BTSerial.available()) {
char command = BTSerial.read(); // Read the incoming data

// Control LEDs based on the received command


switch (command) {
case '1':
turnOnLED(0); // Turn ON LED 1
break;
case '2':
turnOnLED(1); // Turn ON LED 2
break;
case '3':
turnOnLED(2); // Turn ON LED 3
break;
case '4':
turnOnLED(3); // Turn ON LED 4
break;
case '5':
turnOnLED(4); // Turn ON LED 5
break;
case '6':
turnOffLED(0); // Turn OFF LED 1
break;
case '7':
turnOffLED(1); // Turn OFF LED 2
break;
case '8':
turnOffLED(2); // Turn OFF LED 3
break;
case '9':
turnOffLED(3); // Turn OFF LED 4
break;
case '0':
turnOffLED(4); // Turn OFF LED 5
break;
}
}

// Check if any LED needs to be turned off after the duration


for (int i = 0; i < 5; i++) {
if (ledState[i] && (millis() - ledOnTime[i] >= duration)) {
turnOffLED(i); // Turn off the LED if the duration has elapsed
}
}
}

// Function to turn on an LED


void turnOnLED(int ledIndex) {
digitalWrite(ledIndex + 2, HIGH); // Turn ON the specified LED
ledOnTime[ledIndex] = millis(); // Record the time when LED was turned ON
ledState[ledIndex] = true; // Update the LED state
}

// Function to turn off an LED


void turnOffLED(int ledIndex) {
digitalWrite(ledIndex + 2, LOW); // Turn OFF the specified LED
ledState[ledIndex] = false; // Update the LED state
}

You might also like