#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#define PIN 9
#define NUM_PIXELS 8
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
int executionCount = 0;
char previousInput = '0';
const int buzzerPin = 10;
// Define LCD pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Define keypad pins and keys
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
pixels.begin();
Serial.begin(9600);
Serial.println("Enter a number to select a light mode:");
Serial.println("1. Static color");
Serial.println("2. Fading colors");
Serial.println("3. Blinking colors");
pinMode(buzzerPin, OUTPUT);
lcd.begin(16, 2); // Initialize the LCD
lcd.print("Select a mode:"); // Display initial message
}
void loop() {
if (executionCount >= 3) {
Serial.println("Program execution completed.");
while (true) {}
}
char key = keypad.getKey(); // Check if a key is pressed
if (key) {
Serial.println(key);
if (key == previousInput) {
Serial.println("Already in the selected mode. Enter a different mode.");
showMenu();
return;
}
switch (key) {
case '1':
countdown();
staticColor();
executionCount++;
previousInput = '1';
break;
case '2':
countdown();
fadeColors();
executionCount++;
previousInput = '2';
break;
case '3':
countdown();
blinkColors();
executionCount++;
previousInput = '3';
break;
default:
Serial.println("Invalid input. Try again.");
showMenu();
break;
}
}
}
void showMenu() {
lcd.clear();
lcd.print("Select a mode:");
}
void countdown() {
lcd.clear();
lcd.print("Countdown:");
for (int i = 3; i >= 1; i--) {
lcd.setCursor(0, 1);
lcd.print(i);
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000);
}
lcd.setCursor(0, 1);
lcd.print(" ");
}
void staticColor() {
int red, green, blue;
lcd.clear();
lcd.print("Enter RGB values:");
while (!Serial.available()) { delay(100); } // Wait for input
red = Serial.parseInt();
while (!Serial.available()) { delay(100); } // Wait for input
green = Serial.parseInt();
while (!Serial.available()) { delay(100); } // Wait for input
blue = Serial.parseInt();
Serial.print("Setting static color to (");
Serial.print(red);
Serial.print(",");
Serial.print(green);
Serial.print(",");
Serial.print(blue);
Serial.println(")");
uint32_t color = pixels.Color(red, green, blue);
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, color);
}
pixels.show();
showMenu();
}
void fadeColors() {
int speed;
lcd.clear();
lcd.print("Enter speed:");
while (!Serial.available()) { delay(100); } // Wait for input
speed = Serial.parseInt();
Serial.print("Fading colors with speed ");
Serial.println(speed);
for (int j = 0; j < 256; j++) {
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(j * 65536L / 256)));
}
pixels.show();
delay(speed);
}
showMenu();
}
void blinkColors() {
int red, green, blue, duration;
lcd.clear();
lcd.print("Enter RGB & duration:");
while (!Serial.available()) { delay(100); } // Wait for input
red = Serial.parseInt();
while (!Serial.available()) { delay(100); } // Wait for input
green = Serial.parseInt();
while (!Serial.available()) { delay(100); } // Wait for input
blue = Serial.parseInt();
while (!Serial.available()) { delay(100); } // Wait for input
duration = Serial.parseInt();
Serial.print("Blinking colors with (");
Serial.print(red);
Serial.print(",");
Serial.print(green);
Serial.print(",");
Serial.print(blue);
Serial.print(") for ");
Serial.print(duration);
Serial.println("ms");
uint32_t color = pixels.Color(red, green, blue);
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, color);
}
pixels.show();
delay(duration);
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, 0);
}
pixels.show();
showMenu();
}