This project’s concept is to utilize a combination of hardware components (Arduino, ultrasonic sensor, LEDs, and a slide switch) to create an interactive system that responds to physical proximity and manual control.
Setup:
Video:
Code:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const int ledPin = 8; // LED connected to digital pin 8
const int trigPin = 12; // Ultrasonic Sensor Trigger pin
const int echoPin = 11; // Ultrasonic Sensor Echo pin
const int ledPin1 = 9; // First LED pin(PWM capable)
const int ledPin2 = 4; // Second LED pin
const int switchPin = 2; // Slide switch pin
bool ledEnabled = false; // State to keep track of LEDs response state
long duration; // Variable to store the time it takes for the echo to return
float distance; // Variable to store the calculated distance
void setup(){
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin1, OUTPUT); // Sets the first LED pin as an Output
pinMode(ledPin2, OUTPUT); // Sets the second LED pin as an Output
pinMode(switchPin, INPUT_PULLUP); // Sets the switchPin as an Input with internal pull-up resistor
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop(){
// Read the state of the switch
ledEnabled = digitalRead(switchPin) == LOW; // Check if switch is active
// Measure the distance from the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2(go andreturn)
Serial.print("Distance: ");
Serial.println(distance);
if(ledEnabled){
// Pulse the first LED based on distance
int brightness = map(distance, 0, 200, 255, 10);
brightness = constrain(brightness, 10, 255);
analogWrite(ledPin1, brightness);
// Toggle the second LED based on distance threshold
digitalWrite(ledPin2, distance <30 ? HIGH : LOW);
}else{
// Turn off LEDs when switch is off
analogWrite(ledPin1, 0);
digitalWrite(ledPin2, LOW);
}
delay(100); // Short delay to stabilize loop and reduce sensor noise
}
const int ledPin = 8; // LED connected to digital pin 8
const int trigPin = 12; // Ultrasonic Sensor Trigger pin
const int echoPin = 11; // Ultrasonic Sensor Echo pin
const int ledPin1 = 9; // First LED pin (PWM capable)
const int ledPin2 = 4; // Second LED pin
const int switchPin = 2; // Slide switch pin
bool ledEnabled = false; // State to keep track of LEDs response state
long duration; // Variable to store the time it takes for the echo to return
float distance; // Variable to store the calculated distance
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin1, OUTPUT); // Sets the first LED pin as an Output
pinMode(ledPin2, OUTPUT); // Sets the second LED pin as an Output
pinMode(switchPin, INPUT_PULLUP); // Sets the switchPin as an Input with internal pull-up resistor
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
// Read the state of the switch
ledEnabled = digitalRead(switchPin) == LOW; // Check if switch is active
// Measure the distance from the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and return)
Serial.print("Distance: ");
Serial.println(distance);
if (ledEnabled) {
// Pulse the first LED based on distance
int brightness = map(distance, 0, 200, 255, 10);
brightness = constrain(brightness, 10, 255);
analogWrite(ledPin1, brightness);
// Toggle the second LED based on distance threshold
digitalWrite(ledPin2, distance < 30 ? HIGH : LOW);
} else {
// Turn off LEDs when switch is off
analogWrite(ledPin1, 0);
digitalWrite(ledPin2, LOW);
}
delay(100); // Short delay to stabilize loop and reduce sensor noise
}
const int ledPin = 8; // LED connected to digital pin 8
const int trigPin = 12; // Ultrasonic Sensor Trigger pin
const int echoPin = 11; // Ultrasonic Sensor Echo pin
const int ledPin1 = 9; // First LED pin (PWM capable)
const int ledPin2 = 4; // Second LED pin
const int switchPin = 2; // Slide switch pin
bool ledEnabled = false; // State to keep track of LEDs response state
long duration; // Variable to store the time it takes for the echo to return
float distance; // Variable to store the calculated distance
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin1, OUTPUT); // Sets the first LED pin as an Output
pinMode(ledPin2, OUTPUT); // Sets the second LED pin as an Output
pinMode(switchPin, INPUT_PULLUP); // Sets the switchPin as an Input with internal pull-up resistor
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
// Read the state of the switch
ledEnabled = digitalRead(switchPin) == LOW; // Check if switch is active
// Measure the distance from the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and return)
Serial.print("Distance: ");
Serial.println(distance);
if (ledEnabled) {
// Pulse the first LED based on distance
int brightness = map(distance, 0, 200, 255, 10);
brightness = constrain(brightness, 10, 255);
analogWrite(ledPin1, brightness);
// Toggle the second LED based on distance threshold
digitalWrite(ledPin2, distance < 30 ? HIGH : LOW);
} else {
// Turn off LEDs when switch is off
analogWrite(ledPin1, 0);
digitalWrite(ledPin2, LOW);
}
delay(100); // Short delay to stabilize loop and reduce sensor noise
}
Reflection:
Reflecting on this project, I’ve realized it was a substantial learning experience and a test of my problem-solving skills. Integrating various components—Arduino, ultrasonic sensor, LEDs, and a slide switch—presented several challenges that enhanced my understanding of electronic systems and their programming. One of the main difficulties encountered was ensuring stable and reliable readings from the ultrasonic sensor. Interferences occasionally led to erratic LED behaviors, requiring adjustments in the placement and coding to filter out spurious signals effectively. Another challenge was debouncing the slide switch to achieve consistent results, underscoring software stability’s importance in hardware projects. Managing multiple outputs based on input from a single sensor also pushed me to think critically about how different components interact within an embedded system. This project bolstered my technical skills and deepened my appreciation for the meticulous detail required in electronic design and the creative potential of combining simple elements to produce complex interactions.