-2

What I want to achieve is that between 10 and 60 cm of my HCSR04 sensor the color fades from red to blue on my RGB LED in steps of 1 cm between 10 and 60 cm.

I have tried creating some own functions which calculate how much the analogWrite has to change every cm. Yet have had no succes..

I want to implement this function in the following code:

enter image description here

I hope my question is understandable...

  • 2
    Try using the Arduino map() function... – Majenko Oct 3 '16 at 13:15
  • 2
    You should include the code as text rather than an image. Also, I assume that the ranging works (and prints the distance), is that true? – Paul Oct 3 '16 at 14:12
  • @paul Indeed, the code above works as it should, I just have to implement the function I described. – Jordakoes Oct 4 '16 at 19:44
0
analogWrite(Kleur1,distance);
analogWrite(Kleur2,1024-distance);

Theoretically, this is all you need.

You'll color the LED "kleur1" with the analog value of the distance, so it will become brighter when the distance is higher. And you'll color the LED "kleur2" with the "inverted" analog value of the distance, so it will become less brighter when the distance is higher.

But this kind of depends on what the value is on your maximal distance etc.

  • Thanks for the answer! I am going to try this out somewhere this week! – Jordakoes Oct 4 '16 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.