13
When turning the knob on the rotary angle sensor, the brightness of the led will change. When you
turn to the limit in one direction, if the led is off, then if you turn to the limit in the other direction, the
brightness of the led is the maximum.
What will you see
1. Declare the pin of led and rotary angle sensor. Declare some constants to be used.
2. Initialize the serial monitor and initialize the rotary angle sensor to input and led to output.
3. Use getDegree() function to get the degrees of rotary angle sensor.
4. Print the degrees of rotary angle sensor and map the degrees as led brightness.
Code overview
Remap numbers from one range to another. That is, if the value is "fromLow", the mapped value will
be "toLow". If the value is "fromHigh", then the mapped value will be "toHigh". When "value" is from
other values from fromLow to fromHigh, it is also mapped to between toLow and toHigh in equal
proportions. So here we map the value of pmeterValue (between 0 and 1023) to gapValue (between
0 and 255). For example, if the value of pmeter is 200, after using the map() function, it will become
50 and be assigned to the variable: gapValue.
Arduino math function map():int gapValue = map(value, fromLow, fromHigh, toLow,
toHigh)
For analog pin, we use the analogWrite() function to write the value on an analog pin. Similar to
digitalWrite() function, it takes two parameters, but the second parameter is no longer only two high
and low states, it can be any number you want to write, and each value you write will give it the
corresponding state. But in fact, there is a premise, that is, the hardware must be able to divide so
many levels of effect. For example, most of the led are 256.
Analog Output: analogWrite(LED,brightness);
Serial print has two printing methods, one is Serial.print(), the other is Serial.println(). The difference
between them is that after “print” method prints the content, the information that needs to be printed
can continue to be displayed in this line, and println method will open a new line after printing the
content, that is, the information that needs to be printed can’t continue to be displayed in this line.
Serial.print() is used to print the string or variable that you want to output, If you want to output a
string, you need to wrap the string with " ". If you want to output the variable, just write the variable
name in parentheses.
Serial print: Serial.println("The angle between the mark and the starting position:");
Serial.println(degrees);
Code usage
Содержание Crowtail
Страница 48: ......