Concept
For this week’s assignment, I designed a simple switch to be positioned in front of a screen, like a laptop. This switch uses an HC-SR04 distance sensor and an Arduino board to control either a red or green LED. When the user is at a healthy distance from the screen—set to 50 cm or more—the green LED lights up. If the user gets too close, the red LED turns on as a warning to move back. This setup helps promote good screen-distance habits to protect eye health.
To implement the logic, I modified an example code for Sensors from the Arduino IDE. Below is a demonstration video for my switch.
Code
void setup() { // initialize serial communication: Serial.begin(9600); pinMode(trigPin,OUTPUT); pinMode(echoPin,INPUT); pinMode(GreenLED, OUTPUT); pinMode(RedLED, OUTPUT); } void loop() { long duration, inches, cm; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); cm = microsecondsToCentimeters(duration); Serial.print(cm); Serial.print("cm"); Serial.println(); // Controlling LEDs from Distances if(cm<50 ) { digitalWrite(GreenLED, LOW); digitalWrite(RedLED, HIGH); }else { digitalWrite(GreenLED, HIGH); digitalWrite(RedLED, LOW); } delay(100); } long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }
Reflection for future works
I have gained knowledge on how Simple switches are fundamental devices used to control the flow of current in a circuit. It was also interesting to use the HC-SR04 sensor and see how it works. Going forward, I hope to use switches in combination with other logics to design complex and interesting projects.