Date: Oct. 16. 2023
Group members: Letitia, Qingqing, Youer
We landing Letitia’s idea of the Satan’s Garden. In this idea, we are using distance sensor to control the videos playing in P5 with specific range setting. We have been through the multiple sensors’ testing to find the correct sensor to display our idea.
First, we tried ultrasonic distance sensor.
In this code, we toggled different LED demand in different distance range.
the wire connections of 4 LED with ultrasonic sensor
// constants won't change
const int trigPin = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int echoPin = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int ledPin1 = 2; // Arduino pin connected to LED's pin
const int ledPin2 = 3; // Arduino pin connected to LED's pin
const int ledPin3 = 4; // Arduino pin connected to LED's pin
const int ledPin4 = 5; // Arduino pin connected to LED's pin
//const int DISTANCE_THRESHOLD = 100; // centimeters
long duration = 0;
int cm = 0;
int in = 0;
// variables will change:
//float duration_us, distance_cm;
void setup() {
Serial.begin (9600); // initialize serial port
pinMode(trigPin, OUTPUT); // set arduino pin to output mode
pinMode(echoPin, INPUT); // set arduino pin to input mode
//LED
pinMode(ledPin1, OUTPUT); // set arduino pin to output mode
pinMode(ledPin2, OUTPUT); // set arduino pin to output mode
pinMode(ledPin3, OUTPUT); // set arduino pin to output mode
pinMode(ledPin4, OUTPUT); // set arduino pin to output mode
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
int duration = pulseIn (echoPin, HIGH);
cm = duration*0.034/2;
in = duration*0.0133/2;
//LED1
if(in >= 120) {
digitalWrite(ledPin1, HIGH); // turn on LED1
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
delay(10);
}
//LED2
if(in <= 90 && in > 60) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH); // turn on LED2
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
delay(10);
}
//LED3
if(in <= 60 && in > 30) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH); // turn on LED3
digitalWrite(ledPin4, LOW);
delay(10);
}
//LED4
if(in <= 30) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH); // turn on LED4
delay(10);
}
// print the value to Serial Monitor
Serial.print("distance: ");
Serial.print(in);
Serial.println(" cm");
}
// under 30cm works the best and stable
Youer working with the code
Qingqing working with the wires