// Pin assignment
// Input pins
const int hallSensorFrontPin = 7; // Hall sensor front pin (white light) //replaced with sliding switch
const int hallSensorBackPin = 8; // Hall sensor back pin (red light) //replaced with sliding switch
const int mainSwitchPin = 5; // Main switch pin //green push button
const int irSensorPin = 6; // Infrared sensor pin //replaced with sliding switch
//output pins
const int whiteLedPin = 3; // White LED pin
const int redLedPin = 2; // Red LED pin
const int yellowLedPin = 4; // Yellow LED pin
// Variables
unsigned long pressStartTime = 0; // Time when main switch was pressed
bool isAmbientMode = false; // Status for ambient light mode
bool whiteLedOnBySwitch = false; // Status of white LED controlled by main switch
bool yellowLedOnBySwitch = false; // Status of yellow LED controlled by main switch
void setup() {
// Set the pin modes
// Input pins
pinMode(hallSensorFrontPin, INPUT);
pinMode(hallSensorBackPin, INPUT);
pinMode(mainSwitchPin, INPUT);
pinMode(irSensorPin, INPUT);
//output pins
pinMode(whiteLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
//Start serial communication
Serial.begin(9600); //set baud rate to standart 9600
Serial.println("Arduino program started");
}
// void loop runs in a loop and runs all three functions after one another
void loop() {
handleMainSwitch(); // jumps to function handelMainSwitch
handleHallSensors(); // jumps to function handelHallSensor
handleIRSensor(); // jumps to function handelIRSensor
delay(200); // Short wait time
}
void handleMainSwitch() {
int mainSwitchState = digitalRead(mainSwitchPin);
unsigned long currentTime = millis();
// Check if the main switch is pressed for at least 500 ms and no Hall sensors are active
if (mainSwitchState == HIGH && digitalRead(hallSensorFrontPin) == HIGH && digitalRead(hallSensorBackPin) == HIGH) {
if (pressStartTime == 0) {
pressStartTime = currentTime;
} else if (currentTime - pressStartTime >= 500) {
if (isAmbientMode) {
// Turn off the yellow LED if main switch is pressed and lamp is in ambient mode
yellowLedOnBySwitch = false;
deactivateAllLEDs();
Serial.println("Yellow LED turned off by main switch");
} else {
// Toggle white LED using the main switch
whiteLedOnBySwitch = !whiteLedOnBySwitch;
digitalWrite(whiteLedPin, whiteLedOnBySwitch ? HIGH : LOW);
if (whiteLedOnBySwitch) {
Serial.println("White LED turned on by main switch");
} else {
Serial.println("White LED turned off by main switch");
}
}
pressStartTime = 0;
delay(300); // Debounce
}
} else {
pressStartTime = 0;
}
}
*/
void handleHallSensors() {
// Front Hall sensor controls the white LED
if (digitalRead(hallSensorFrontPin) == LOW) {
deactivateAllLEDs();
digitalWrite(whiteLedPin, HIGH);
isAmbientMode = false;
whiteLedOnBySwitch = false;
//Serial.println("Front Hall sensor activated - White LED on");
} else if (!isAmbientMode && !whiteLedOnBySwitch) {
digitalWrite(whiteLedPin, LOW);
//Serial.println("Front Hall sensor not activated - White LED off");
}
// Rear Hall sensor controls the red LED
if (digitalRead(hallSensorBackPin) == LOW) {
deactivateAllLEDs();
digitalWrite(redLedPin, HIGH);
isAmbientMode = false;
//Serial.println("Rear Hall sensor activated - Red LED on");
} else {
digitalWrite(redLedPin, LOW);
//Serial.println("Rear Hall sensor not activated - Red LED off");
}
}
void handleIRSensor() {
if (digitalRead(irSensorPin) == LOW && whiteLedOnBySwitch) {
delay(500); // Wait time of 500 ms
if (digitalRead(irSensorPin) == LOW) {
digitalWrite(whiteLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
isAmbientMode = true;
whiteLedOnBySwitch = false;
//Serial.println("IR sensor activated - Yellow LED on (Ambient Mode)");
}
}
}
void deactivateAllLEDs() {
digitalWrite(whiteLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, LOW);
isAmbientMode = false;
whiteLedOnBySwitch = false;
//Serial.println("All LEDs turned off");
}