#include <LiquidCrystal.h>
int Foto = A0; // Declare a variable for the photocell input pin
int Avlesning = 0; // Declare a variable to hold the photocell reading
int rod_pin= 4; // Declare a variable for the red LED pin
int gronn_pin = 3; // Declare a variable for the green LED pin
int bla_pin = 2; // Declare a variable for the blue LED pin
const int ledPin = 4;// the number of the LED pin
bool switchState = false; // variable for storing the switch state
unsigned long timer = 0; // variable for storing the timer value
const unsigned long interval = 15000; // interval for turning off the LED (15 seconds in milliseconds)
const int buttonPin = 5; // Declare a variable for the pushbutton pin
int buttonState = 0; // Declare a variable to hold the pushbutton state
int lightCycle = 0; // Declare a variable to hold the current light cycle
int flashInterval = 1000; // Declare a variable to hold the flash interval
int ledState = 0; // Declare a variable to hold the LED state
const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; // Declare the LCD pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Create an LCD object
// Define a function to set the color of the LEDs
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(rod_pin, red_light_value); // Set the red LED to the specified value
analogWrite(gronn_pin, green_light_value); // Set the green LED to the specified value
analogWrite(bla_pin, blue_light_value); // Set the blue LED to the specified value
}
void setup() {
pinMode(ledPin, OUTPUT);
// initialize the button pin as an input:
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2); // Initialize the LCD
lcd.print("Hei, PellePoliti"); // Print a label for the light percentage reading
lcd.setCursor(0,2);
lcd.print("Alpha Detected");
pinMode(rod_pin, OUTPUT); // Set the red LED pin as an output
pinMode(gronn_pin, OUTPUT); // Set the green LED pin as an output
pinMode(bla_pin, OUTPUT); // Set the blue LED pin as an output
pinMode(buttonPin, INPUT); // Set the pushbutton pin as an input
// Start sequence with LED lights
RGB_color(0, 255, 0); // Set the LEDs to red (255, 0, 0)
delay(500); // Wait for 150 milliseconds
RGB_color(0, 0, 255); // Set the LEDs to yellow (255, 255, 0)
delay(500); // Wait for 150 milliseconds
RGB_color(0, 255, 0); // Set the LEDs to green (0, 255, 0)
delay(500); // Wait for 150 milliseconds
RGB_color(0, 0, 255); // Set the LEDs to cyan (0, 255, 255)
delay(500); // Wait for 150 milliseconds
RGB_color(0, 255, 0); // Set the LEDs to blue (0, 0, 255)
delay(500); // Wait for 150 milliseconds
RGB_color(0, 0, 255); // Set the LEDs to magenta (255, 0, 255)
delay(500); // Wait for 150 milliseconds
RGB_color(0, 255, 0); // Set the LEDs to white (255, 255, 255)
delay(500); // Wait for 150 milliseconds
RGB_color(0, 0, 0); // Set the LEDs to off (0, 0, 0)
}
// Define a function to flash the LEDs
void flashLights() {
// Check if the current time is a multiple of the flash interval
if (millis() % flashInterval == 0) {
// If it is, toggle the LED state
ledState = !ledState;
// Set the LEDs to the new state
RGB_color(ledState * 255, ledState * 255, ledState * 255);
}
}
void loop() {
lcd.clear();
lcd.print("Lys Prosent:");
Avlesning = analogRead(Foto); // Read the photocell value
if (Avlesning/10.23 > 90) {
lcd.setCursor(15, 0); // Move the cursor to the first column of the third row
lcd.print("H"); // Print a message
}
int buttonState = digitalRead(buttonPin);
// If the button is pressed, reset the timer
if (buttonState == HIGH) {
timer = millis();
}
// If the button has not been pressed within the interval, turn the LED off
if (millis() - timer > interval) {
digitalWrite(ledPin, LOW);
} else {
// If the button has been pressed within the interval, turn the LED on
digitalWrite(ledPin, HIGH);
}
// If the button is pressed, turn the LED on
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
// If the button is not pressed, turn the LED off
digitalWrite(ledPin, LOW);
}
// Check if the light level is below 10%
if (Avlesning/10.23 < 10) {
lcd.setCursor(14, 0); // Move the cursor to the first column of the third row
lcd.print("L"); // Print a message
}
{
// read the state of the button value:
buttonState = digitalRead(buttonPin);
// check if the button is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// toggle the switch state:
switchState = !switchState;
// check the switch state and turn LED on or off:
if (switchState) {
digitalWrite(ledPin, HIGH);
// reset the timer when the LED is turned on:
timer = millis();
}
}
// check if the LED is on and the timer has reached the interval:
if (switchState && (millis() - timer >= interval)) {
// turn the LED off:
digitalWrite(ledPin, LOW);
// reset the switch state:
switchState = false;
}
lcd.setCursor(12, 0); // Move the cursor to the first column of the second row
lcd.print(Avlesning/10.23, 0); // Print the photocell value as a percentage
lcd.print("%"); // Print the percent symbol
lcd.setCursor(7, 1); // Move the cursor to the eighth column of the second row
lcd.print("Lys: "); // Print a label for the light cycle value
// Check if the light cycle is 6, which indicates flashing mode
if (lightCycle == 6) {
// Read the pushbutton state
buttonState = digitalRead(buttonPin);
// If the pushbutton is pressed, increase the flash interval
if (buttonState == HIGH)
{
flashInterval += 100; // Increase the interval by 100 milliseconds
}
// Flash the LEDs at the current interval
flashLights();
}
buttonState = digitalRead(buttonPin); // Read the pushbutton state
if (buttonState == HIGH) { // If the pushbutton is pressed
if (lightCycle < 4) { // If the current light cycle is less than 4
lightCycle += 1; // Increment the light cycle by 1
} else { // If the current light cycle is 4 or higher
lightCycle = 0; // Set the light cycle to 0
}
}
if (lightCycle == 0) { // If the light cycle is 0
RGB_color(0, 0, 0); // Set the LEDs to off (0, 0, 0)
lcd.setCursor(12, 1); // Move the cursor to the thirteenth column of the second row
lcd.print("Null"); // Print the value of the light cycle
}
if (lightCycle == 1) { // If the light cycle is 1
RGB_color(255, 255, 255); // Set the LEDs to white (255, 255, 255)
lcd.setCursor(12, 1); //
lcd.print("Hvit");
}
if (lightCycle == 2) { // If the light cycle is 2
RGB_color(255, 0, 0); // Set the LEDs to red (255, 0, 0)
lcd.setCursor(12, 1); // Move the cursor to the thirteenth column of the second row
lcd.print("Gronn"); // Print the value of the light cycle
}
if (lightCycle == 3) { // If the light cycle is 3
RGB_color(0, 255, 0); // Set the LEDs to green (0, 255, 0)
lcd.setCursor(12, 1); // Move the cursor to the thirteenth column of the second row
lcd.print("Rodt"); // Print the value of the light cycle
}
if (lightCycle == 4) { // If the light cycle is 4
RGB_color(0, 0, 255); // Set the LEDs to blue (0, 0, 255)
lcd.setCursor(12, 1); // Move the cursor to the thirteenth column of the second row
lcd.print("Blaa"); // Print the value of the light cycle
}
delay(200); // Wait for 250 milliseconds
}
}