So I've made a parking sensor with the Ultrasonic sensor and a ledstrip. But the ledstrip doesn't turn yellow or green. I think it has to do with the Thresholds. Can anyone help me to set this up? I also want a longer distance greater than 1 meter. This is my code:
#include "FastLED.h"
#include <math.h>
#define NUM_LEDS 32
#define BRIGHTNESS 10
#define DATA_PIN 4
// Define the array of leds
CRGB leds[NUM_LEDS];
// Module Pins
#define trigPin 11
#define echoPin 12
// Thresholds (in CM)
long SECTION_SIZE = 80;
long STOP_DISTANCE = 60;
long WARNING_THRESHOLD = 50;
long MAX_THRESHOLD = 375;
long RED_THRESHOLD = STOP_DISTANCE;
long YELLOW_THRESHOLD = RED_THRESHOLD + SECTION_SIZE;
long GREEN_THRESHOLD = YELLOW_THRESHOLD + SECTION_SIZE;
const int numReadings = 5;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup() {
Serial.begin (9600);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(10);
// HC-SR04 Setup
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
long distance, lastRun;
bool red, yellow, green;
total = total - readings[readIndex];
readings[readIndex] = calculateDistance();
total = total + readings[readIndex];
readIndex++;
if (readIndex >= numReadings) readIndex = 0;
distance = abs(total / numReadings);
yellow = distance <= YELLOW_THRESHOLD && distance > RED_THRESHOLD;
red = distance <= RED_THRESHOLD;
green = distance > YELLOW_THRESHOLD && distance <= GREEN_THRESHOLD;
if (red) {
setRed();
} else if (yellow) {
setYellow(distance);
} else if (green) {
setGreen(distance);
} else {
setGreenStandby();
}
Serial.print(distance);
Serial.println(" cm");
FastLED.show();
delay(1);
}
void lightItUp(const CRGB& pColor, const CRGB& sColor, uint16_t travel) {
uint16_t travelFix = travel * (NUM_LEDS/SECTION_SIZE);
uint16_t leftStop = travelFix/2;
uint16_t rightStop = NUM_LEDS - leftStop;
for (int i = 0; i < NUM_LEDS; i++) {
if (i < leftStop || i > rightStop) {
leds[i] = pColor;
} else {
leds[i] = sColor;
}
}
FastLED.show();
}
long calculateDistance() {
long duration;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return (duration/2) / 29.1;
}
void setGreenStandby() {
return fill_solid(leds, NUM_LEDS, CRGB::Black);
}
void setRed() {
return fill_solid(leds, NUM_LEDS, CRGB::Red);
}
void setGreen(long distance) {
long travel = GREEN_THRESHOLD - distance;
return lightItUp(CRGB::Yellow, CRGB::Green, travel);
}
void setYellow(long distance) {
long travel = YELLOW_THRESHOLD - distance;
return lightItUp(CRGB::Red, CRGB::Yellow, travel);
}
1 Answer 1
I believe your mistake is in this part of your code:
yellow = distance <= YELLOW_THRESHOLD && distance > RED_THRESHOLD;
red = distance <= RED_THRESHOLD;
green = distance > YELLOW_THRESHOLD && distance <= GREEN_THRESHOLD;
if (red) {
setRed();
} else if (yellow) {
setYellow(distance);
} else if (green) {
setGreen(distance);
} else {
setGreenStandby();
}
In particular, you are defining three boolean variables independently and then switching based on their values.
Assuming you are seeing the right values output on serial monitor (like 90 cm
, 60 cm
, etc...), which is something I'm unsure of as I'm not getting how you are determining the distance, then the following should be easier to follow/understand:
if (distance > GREEN_THRESHOLD) {
setGreenStandby();
} else if (distance > YELLOW_THRESHOLD) {
setGreen(distance);
} else if (distance > RED_THRESHOLD) {
setYellow(distance);
} else {
setRed();
}
You have only one variable determining the led strip configuration, no need to go to 3 and then try to reduce again ;-)
Please note that your YELLOW_THRESHOLD
is defined as 140
, if you don't ever see a message in the serial monitor greater than that value you don't have a chance of seeing any colour other than red.... I'm asking because you mentioned I also want a longer distance greater than 1 meter that might imply your sensor is unable to go farther than 1 meter...
With the code above I'm checking the distance in a greater to smaller order: the first matching is the only one activated and it's impossible for the distance
variable to match more than one condition. Generally speaking, simpler is better.
longer distance greater than 1 meter
? .... please describe exactly what you want the code to do ..... also describe exactly what it does right now ..... add the information to the question above .... DO NOT add info to comments