readCount

Read current count value from encoder with X4 decoding

Description

example

[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.

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 Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: [count,time] = readCount(encoder,'Reset',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

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.

Introduced in R2017a