Type Here to Get Search Results !

How To Do Setting For Led Speed In Arduino Projects

Ok great, we used the serial monitor to test our sensors, printed the results to our computer screen, and now we know that our speed sensor provides values between 0 to 1023. Now this process is useful for many other sensors. However while using the build-in delay function "speed” will be measured in milliseconds and the relation is opposite, more delay is less speed. Let’s say that we would like that speed range to be between 100 to 700 milliseconds and not between 0 to 1023, in that case some range normalization or mapping will be needed.

The good news is that there is some simple to use function that will help us to perform such mapping. Using the "analogread" function, we will read the value from the analog input pin correlated to the voltage our sensor is putting out, which in our case should indicate the needed leds speed and we saw that the value range is between 0 to 1023.


Now for mapping or normalize this range to other range we have a dedicated build-in function, this function re-map a number from one range to the other so actually it is one of the most useful function for mapping sensor values. It is called "map" and use 4 input parameters

The first parameter is the actual value that we would like to map, the second and third parameters are the original low and high values. And this is where the analog serial monitor will help us to check the values coming from our sensors.

Finally, the fourth and fifth parameters are the new low and high value range that we want to map the original value. In our case, we just want to make sure the maximum speed will be 100 milliseconds and the minimum speed will be 700 milliseconds. You will see that this is going to be useful also for the second sensor. We used to setup the leds brightness because on one side the analogread returns a value in the range of 0-1023, but analogwrite function that we are planning to use can only take a value from 0-255.

In addition, another thing that comes up frequently is the need to reverse a range of values. Again, in our case, speed and delay are opposite, less delay is more speed, so it is make sense to reverse the range of values, So that instead of outputting 100–700 we would flip it to 700–100 and the way to perform it is just by flipping the last two values.

Easy and very useful. Something that is less relevant for our specific case right now, but can be relevant to other sensors types that you may use in the future. We saw that the map() function is useful for changing the start and end values of a variable, however it does not limit them to only those value, leaving values outside of the map() function might be useful if we only want to map a smaller part of a range of values.

Taking for example, the need to ignore readings from a proximity sensor that detects something that is maybe too far away, or ignore a sound below some threshold level and so on.

So, if we want to force or filter the values to a very specific limited range, we could use the constrain() function. The syntax for this function will be: "constrain" and three parameters . The first parameter is the value that we want to constrain to a specified range. The next two parameters represent the needed minimum and maximum values in that new range.

Now let's say that after analyzing our data, looking on the range being returned by our sensors. We find that the minimum reported value average is around 40 and the maximum is around 1000. We could constrain these values before remapping them to a new range.

So taking for example the following code:

  1. This code would limit the readings to anything between 40 and 1000.
  2. Ignoring any readings that exceed this minimum or maximum, followed by remapping analogvalue to a more useable range of 100 to 700.

Ok that's all, that's the way to use the "constrain". However in our case it is not needed but maybe you will use that in other sensors. That you are going to use in some future project.



Post a Comment

0 Comments