Main Content

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

[count,time] = readCount(encoder) returns the current count value from the encoder along with the elapsed time since the Arduino® server started running.

example

[count,time] = readCount(encoder,Name,Value) also returns options specified by one or more Name,Value pair arguments.

example

Examples

collapse all

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

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

collapse all

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')

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

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

collapse all

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.

Elapsed time since the Arduino server started running in seconds, returned as a number.

Version History

Introduced in R2017a