RGBLED 2

const int trigPin = 9;
const int echoPin = 10;
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  float distance = getDistance();
  setColorByDistance(distance);
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(200);
}

float getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  long duration = pulseIn(echoPin, HIGH);
  return duration * 0.01715;
}

void setColor(int red, int green, int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

void setSmoothColorByDistance(float distance) {
  // Map distance to color spectrum (0-100cm range)
  distance = constrain(distance, 0, 100);
  
  if (distance <= 25) {
    // Red to Yellow (0-25cm)
    int green = map(distance, 0, 25, 0, 255);
    setColor(255, green, 0);
  }
  else if (distance <= 50) {
    // Yellow to Green (25-50cm)
    int red = map(distance, 25, 50, 255, 0);
    setColor(red, 255, 0);
  }
  else if (distance <= 75) {
    // Green to Blue (50-75cm)
    int blue = map(distance, 50, 75, 0, 255);
    int green = map(distance, 50, 75, 255, 0);
    setColor(0, green, blue);
  }
  else {
    // Blue for far distances
    setColor(0, 0, 255);
  }
}

void setColorByDistance(float distance) {
  if (distance < 5) {
    // Blink red for very close objects
    setColor(255, 0, 0);
    delay(100);
    setColor(0, 0, 0);
    delay(100);
  }
  else {
    // Normal color coding
    setSmoothColorByDistance(distance);
  }
}

1. Distance Range Sensitivity

Modify the distance thresholds in your color functions:

cpp
void setColorByDistance(float distance) {   if (distance < 3) {  // Changed from 5 to 3 (more sensitive)     // Blink red for very close objects     setColor(255, 0, 0);     delay(100);     setColor(0, 0, 0);     delay(100);   }   else {     setSmoothColorByDistance(distance);   } }  void setSmoothColorByDistance(float distance) {   distance = constrain(distance, 0, 50); // Changed from 100 to 50 (more sensitive range)   
  if (distance <= 10) {        // Changed from 25 to 10     // Red to Yellow (0-10cm)     int green = map(distance, 0, 10, 0, 255);     setColor(255, green, 0);   }   else if (distance <= 20) {   // Changed from 50 to 20     // Yellow to Green (10-20cm)     int red = map(distance, 10, 20, 255, 0);     setColor(red, 255, 0);   }   else if (distance <= 35) {   // Changed from 75 to 35     // Green to Blue (20-35cm)     int blue = map(distance, 20, 35, 0, 255);     int green = map(distance, 20, 35, 255, 0);     setColor(0, green, blue);   }   else {     // Blue for far distances     setColor(0, 0, 255);   } }

2. Reading Sensitivity (Noise Filtering)

Add more sophisticated filtering:

cpp
float getFilteredDistance() {   const int numReadings = 5;   float readings[numReadings];   
  // Take multiple readings   for(int i = 0; i < numReadings; i++) {     readings[i] = getRawDistance();     delay(10);   }   
  // Remove outliers and average   float sum = 0;   int validReadings = 0;   float average = 0;   
  // Calculate initial average   for(int i = 0; i < numReadings; i++) {     sum += readings[i];   }   average = sum / numReadings;   
  // Remove readings that are too far from average   sum = 0;   validReadings = 0;   for(int i = 0; i < numReadings; i++) {     if(abs(readings[i] - average) < 5) { // Within 5cm of average       sum += readings[i];       validReadings++;     }   }   
  return validReadings > 0 ? sum / validReadings : average; }

3. Response Speed Sensitivity

Control how quickly the system responds to changes:

cpp
float lastDistance = 0; float smoothingFactor = 0.3; // 0.1 = very smooth, 0.9 = very responsive  float getSmoothedDistance() {   float newDistance = getRawDistance();   
  // Exponential smoothing   lastDistance = (smoothingFactor * newDistance) + ((1 - smoothingFactor) * lastDistance);   
  return lastDistance; }

4. Detection Threshold Sensitivity

Set minimum change required to trigger a response:

cpp
float lastReportedDistance = 0; float changeThreshold = 2.0; // Minimum 2cm change to update  void loop() {   float distance = getFilteredDistance();   
  // Only update if change is significant   if(abs(distance - lastReportedDistance) > changeThreshold) {     setColorByDistance(distance);     lastReportedDistance = distance;     
    Serial.print("Distance: ");     Serial.print(distance);     Serial.println(" cm");   }   
  delay(100); }

5. Complete Adjustable Sensitivity System

cpp
// Sensitivity settings - adjust these values const float CLOSE_THRESHOLD = 5.0;      // Distance for red blinking const float MAX_RANGE = 100.0;          // Maximum effective range const int FILTER_SAMPLES = 3;           // Number of readings to average const float CHANGE_THRESHOLD = 1.5;     // Minimum change to respond const float SMOOTHING = 0.4;            // Response speed (0.1-0.9)  float lastDistance = 0; float lastReportedDistance = 0;  void loop() {   float rawDistance = getRawDistance();   float smoothedDistance = applySmoothingFilter(rawDistance);   
  if(abs(smoothedDistance - lastReportedDistance) > CHANGE_THRESHOLD) {     setColorByDistance(smoothedDistance);     lastReportedDistance = smoothedDistance;   }   
  delay(50); }  float applySmoothingFilter(float newDistance) {   lastDistance = (SMOOTHING * newDistance) + ((1 - SMOOTHING) * lastDistance);   return lastDistance; }