readCount
Read current count value from encoder with X4 decoding
Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.
Description
Examples
Read the Current Absolute Count Value
Create an Arduino object with the 'rotaryEncoder'
library.
a = arduino('COM4','Uno','Libraries','rotaryEncoder');
Connect to the encoder that is connected to your Arduino board.
encoder = rotaryEncoder(a,'D2','D3');
Read the current absolute count value that was incremented since the creation of the encoder object.
[count,time] = readCount(encoder)
count = 0
time = 0.1440
Read the Current Incremental Count Value
Create an Arduino object with the 'rotaryEncoder'
library.
a = arduino('COM4','Uno','Libraries','rotaryEncoder');
Connect to the encoder that is connected to your Arduino board.
encoder = rotaryEncoder(a,'D2','D3');
Set 'Reset'
to true
to read the current count value while also resetting the count value to zero on the Arduino board.
[count,time] = readCount(encoder,'Reset',true)
count = 0
time = 0.1580
If you need the current incremental count value, consecutively call the readCount
function with 'Reset'
set to true
.
Input Arguments
encoder
— Quadrature encoder connection
rotaryEncoder object
Quadrature encoder connection, specified as a rotaryEncoder object.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: [count, time] = readCount(rotaryEncoderObj, 'reset',
true)
Example: [count, time] = readCount(rotaryEncoderObj, 'reset', true, 'OutputFormat',
'duration')
Reset
— Flag to reset count value to 0
false
(default) | true
Flag to reset count value to 0 after each
read operation, specified as the comma-separated
pair consisting of 'Reset'
and
either false
or
true
.
Note
Set 'Reset'
to
true
to get the incremental
count value from the last read.
Data Types: logical
OutputFormat
— Output format of the time elapsed since the Arduino server started
duration
Flag to control the output format of time.
Note
Only duration
argument is
supported. If you need the output as double, don't use
the OutputFormat
name-value
pair.
Data Types: duration
Output Arguments
count
— Current count value of the encoder with X4 decoding
numeric
Current count value of the encoder with X4 decoding, returned as a number.
Note
If the current count value is greater than 2^32-1, an overflow
warning occurs. To prevent this overflow, use resetCount
function
to reset count
to 0.
time
— Elapsed time
numeric
Elapsed time since the Arduino server started running in seconds, returned as a number.
Version History
Introduced in R2017a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)