change a value with ultrasonic data

ey!

I am trying to change the amount of red on a rgb led with the data that comes from an ultrasonic sensor HC-SR04.

#include <Ultrasonic.h>
#include <FastLED.h>
#define NUM_LEDS 1
#define DATA_PIN 37
    
Ultrasonic ultrasonic(3,2);    
CRGB leds[NUM_LEDS];

int red;

void setup() {
  Serial.begin(9600);   
  FastLED.addLeds <WS2811, DATA_PIN, BRG>(leds, NUM_LEDS);
}


void loop() {
  leds[0].setRGB (red,0,0);
  FastLED.show(); 
  delay(30); 

  Serial.print(ultrasonic.Ranging(CM));
  Serial.println("cm");
  delay(100);
}

but I dont know where to get the value of “red” from. I tried Serial.read, Serial.println…

Serial.print(ultrasonic.Ranging(CM));

Instead of just printing the value you get from the ssensor, try saving it into a variable.

int someVar = ultrasonic.Ranging(CM);

This assumes it returns an int, you have the library and I don't so that's on you to check out.

Now once, you have the distance value, you can do whatever sort of math you envision that turns that into a value you can use to set your red led.

thank you very much!

that was what I was missing. it is anyway still not working correctly; it set the value of red at the beggining, but it doesnt update it when I move my hand over the sensor.

I might be messing the order of the functions...

camilozk:
thank you very much!

that was what I was missing. it is anyway still not working correctly; it set the value of red at the beggining, but it doesnt update it when I move my hand over the sensor.

I might be messing the order of the functions…

Might be, but right now you’re the only one who can see the current code so I can’t be of much help with what you might have done wrong. Maybe if you posted that code… Ya think?

sorry, here it goes

#include <Ultrasonic.h>
#include <FastLED.h>
#define NUM_LEDS 1
#define DATA_PIN 37
    
Ultrasonic ultrasonic(3,2);    
CRGB leds[NUM_LEDS];

int red = ultrasonic.Ranging(CM);

void setup() {
  Serial.begin(9600);   
  FastLED.addLeds <WS2811, DATA_PIN, BRG>(leds, NUM_LEDS);
}


void loop() {
  leds[0].setRGB (red,0,0);
  FastLED.show(); 
  
  Serial.print(ultrasonic.Ranging(CM));
  Serial.println("");
  delay(100);
}

oh, I have it

int red = ultrasonic.Ranging(CM);

has to be inside of the loop...

thanks!

int red = ultrasonic.Ranging(CM);

You have this at global scope. That means red gets set before things get started (incidentally while the distance read function won't work) and then never gets touched again.

You can't do this. That line needs to go either inside the setup function if you want it to be set only once, or inside the loop function if you want it to be checked over and over.

Also, is the thing returned by ultrasonic.Ranging(CM) an appropriate number to write to analogWrite? analogWrite only takes numbers from 0 to 255. What is the distance you are getting back? Should you do some math before you write to your LED?

I have it in the loop and it work alright

I receive values from 0 to 51, so I have to do the mathematics to scale this

Hmmm, What's 51 * 5?

You weren't one of those kids that blew off math class and used to say, "Oh I'll never need this stuff" are you?

I was in the mathematics olympics

51 * 5 is there already!