Open wjanahi opened 6 years ago
Which sensor are you using? I can take a look. Can you link me your code?
Hi @reneric
I'm using ELEGOO UNO R3 Controller Board with HC-SR04 Ultrasonic Sensor. and the code is the one available here. the only change I made is pin number. but later even after I changed the pin location to match the code same issue.
I'll share the code if you want after few hours.
Regards
Hi @reneric
Sorry for the delay, I'm using this code exactly.
`#include "FastLED.h"
/ HC-SR04 Ping distance sensor] VCC to arduino 5v GND to arduino GND Echo to Arduino pin 13 Trig to Arduino pin 12 Red POS to Arduino pin 11 Green POS to Arduino pin 10 /
// Define the array of leds CRGB leds[NUM_LEDS];
// Module Pins
// Thresholds (in CM) long SECTION_SIZE = 30; long STOP_DISTANCE = 40; long WARNING_THRESHOLD = 30; long MAX_THRESHOLD = 275;
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);
// Fast LED Setup FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // FastLED.setBrightness(84);
// 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");
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::Green); }
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);
}
`
Hmm first thing I'd look at is your sensor distance calculation/output. Print that out and make sure it's outputting the correct distances. Since red is true when the distance is 0, that could be the issue.
Hi, Thank you for your effort
I have used the code and changed the pin number (LED data, Echo and trig) based on my setup. once I upload the code to my arduino the LED strip light up red and stays like that regardless of the sensor.