Ultrasonic sensor and ledstrip color problem

Posted on
  • 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](http://forum.espruino.com/sea­rch/?q=%23include) "FastLED.h"
     [#include](http://forum.espruino.com/sea­rch/?q=%23include) <math.h>
    
    
        [#define](http://forum.espruino.com/sear­ch/?q=%23define) NUM_LEDS 32
        [#define](http://forum.espruino.com/sear­ch/?q=%23define) BRIGHTNESS 10
        [#define](http://forum.espruino.com/sear­ch/?q=%23define) DATA_PIN 4
    
        // Define the array of leds
        CRGB leds[NUM_LEDS];
    
        // Module Pins
        [#define](http://forum.espruino.com/sear­ch/?q=%23define) trigPin 11
        [#define](http://forum.espruino.com/sear­ch/?q=%23define) 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);  
        }
    
  • Sun 2018.11.18

    Hello @user95613, Welcome to Espruino, a 'Javascript' microcontroller.

    May I ask what board you are using, and would you post here the results of running process.env in the IDE please?

  • I'm using the Arduino Uno

  • Wouldn't your post serve you better in the 'Arduino' community as this forum is for those that support Espruino?

  • Yes, I'm afraid you have the wrong forum.

    This forum is purely for JavaScript code on the Espruino microcontrollers - what you've posted is Arduino code for an Arduino

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Ultrasonic sensor and ledstrip color problem

Posted by Avatar for user95613 @user95613

Actions