We’re rewarding the question askers & reputations are being recalculated! Read more.
10

This tutorial I gave at the Embedded Linux Conference tries to answer the questions, providing links to more detailed description of the topics addressed and using the practical example of driving a 4WD drone, where an Arduino Mini Pro acts as slave and controls the 4 independent wheels. The original document can be found here. Note: This answer is ...


10

Interrupts are what you would use in this situation. The rates that you have mentioned are slow enough where you probably would be able to count it inside of a loop, however this is not recommended, as a typical loop will take many clock cycles to complete, and depending on the number of instuctiuons per loop, you may miss some counts. Interrupts are made ...


5

I have a page about rotary encoders - in that I show how you can detect encoder changes (and direction) using one interrupt pin and another pin, like this: // Wiring: Connect common pin of encoder to ground. // Connect pin A (one of the outer ones) to a pin that can generate interrupts (eg. D2) // Connect pin B (the other outer one) to another free pin (eg. ...


5

Not only must cnt variable be declared volatile, but accessing it from the main loop must also be protected because: it can be modified by an ISR it is longer than 1 byte In AVR architecture (8-bits), read/write operations on bytes are guaranteed to be atomic, i.e. no function can access a byte while it is read/written by an ISR. However, operations on ...


5

Library and schematics are at https://github.com/maxgerhardt/rotary-encoder-over-mcp23017. To read a rotary encoder (i.e., detect state changes of the knob), there are two possible implementations: poll the state of the two output pins and check for changes set up an interrupt which will be called when an output pin changes, then check for changes Using ...


4

You will have to use interrupts or otherwise the encoder might be rotating but your Arduino is doing something else than reading the encoder. By using interrupts, the encoder counter is always updated when the encoder moves. This also means that the interrupt function has to be very short so that it only takes a few cycles to complete and thus leaves the ...


4

Shown below is a KY-040 test program that in my tests doesn't lose any counts and is more accurate than some other software; it picks up the counts between detents as well as those at detents. You may be able to adapt it to your DigiKeyboard ATtiny system. /* roto_jw4.ino -- JW, 29 September 2015 -- * A 4-state state-machine implementation of rotary * ...


4

PORTx is the latched value last written there. The external value is found in PINx.


3

A serial mouse is designed to connect to a PC's true serial port - it's an actual RS-232 device using ±5V. As such it won't connect directly to the Arduino's RX/TX pins, you'll have to go through an RS-232 transceiver chip. It also gets its power from the RTS line, but I never knew what the current draw of one of those things was - be careful trying to ...


3

At 115200 baud your serial prints will take 1/11520 seconds per byte. Serial.print(""); What does that do? I count 14 bytes here: Serial.print(dataOut); // 5 bytes, say Serial.print(""); // zero bytes, lol Serial.print(" "); // one byte Serial.println(val); // 5 bytes + cr/lf = 7 bytes Serial.print(" "); // 1 byte Multiply ...


3

Adding some references to already-written libraries and examples, to enable comparison between different approaches, and experiences with speed versus susceptibility to missing steps. Reading rotary encoders: http://playground.arduino.cc/Main/RotaryEncoders Quadrature Encoder too Fast for Arduino (with Solution): http://www.hessmer.org/blog/2011/01/30/...


3

This is probably too fast to have have software involved in the loop. You should try to use a hardware-only solution. Note the Arduino timers should be usable as hardware frequency dividers: configure a timer to be clocked off the input signal, so that it behaves like a counter. configure it for delivering a PWM output with a period of 4 timer clock cycles ...


3

Here: CRGB leds[NUM_LEDS]; you are defining an array of NUM_LEDS objects of the CRGB type. Valid indices for this array range from 0 to NUM_LEDS-1. Here: for (int i = 0; i <= NUM_LEDS; i++) { leds[i].setRGB( r, g, b ); } you are accessing the array elements at indices 0 to NUM_LEDS. The last iteration is an out-of-bounds access, which is causing ...


3

The encoder attaches to the motor (or other) shaft and sends a 10-bit rotational position for a precision of 0.35 deg, and a claimed accuracy of +/- 0.7 degrees. It will be up to the application to keep track of number of rotations, detecting when the shaft has completed a rotation in either direction. In a lead-screw application there will always be a ...


2

It looks like it could be a caching issue. If you're accessing a variable from the main loop and from an interrupt handler, then you need to ensure it's declared as volatile. For example: volatile int cnt = 0; That tells the compiler that the contents of the variable could change unexpectedly, i.e. when an Interrupt Service Routine is triggered. The result ...


2

You never tell the motor to stop. void loop(){ while(counter< 360*5){ analogWrite(motor,255); } analogWrite(motor, 0); //add something like this or whatever could stop it //you may want to disable the counter now too } However, even with that change you are likely a long way from having a working, or at least reliable system - see other ...


2

If the encoder only gives +1 pulse per 90 degrees, you aren't going to get better than 90 degree accuracy out of it. In the example code at the link, the val{x,y,z} are measured analog variables of some other thing that are sampled at each encoder lcoation: My project is a data loger where three analogue inputs are sampled each time a rotary encoder ...


2

int pinA = 0; // Connected to CLK on KY-040 ... Serial.begin (9600); Hardware Serial uses pins 0 and 1. I would try other pins. (Oh, it's an ATtiny - well I'm not sure about HardwareSerial on that, but it looks suspicious). See my page about rotary encoders for more ideas. I only changed the pin numbers in the code and the Serial.println (... to ...


2

When turning one way, A is asserted, then B is asserted, then A is deasserted then B is deasserted. You can detect this because A is asserted first, even if you can't tell when B has changed. But if you turn the opposite way then you fail to detect the initial assertion of B, and the code fails. What you may be able to do is use PinChangeInt to capture the ...


2

Is it possible to implement the reset of the main pulse count with a second interrupt as described? Yes, and it's quite straightforward. Your index signal goes to pin 19, which is interrupt 4 on the Mega, so you can simply attachInterrupt(4, reset, RISING); in setup(), where reset() is a function that resets the counter: void reset() { count = 0; } ...


2

You're confusing interrupt events and pin states here. A pin can be either HIGH or LOW. Never anything else. An interrupt can trigger on one of four events: The pin transitions from HIGH to LOW (FALLING) The pin transitions from LOW to HIGH (RISING) The pin transitions from one state to another (CHANGE) The pin remains low (LOW) Your code should be (as ...


2

Using polling to read multiple encoders may work if you program quite carefully and if you dedicate the processor just to reading encoders; but generally, interrupt-driven encoder processing has a higher likelihood of working ok. The example below uses pin-change interrupts to read from three rotary encoders that are attached to PD2...PD7, ie, six ...


2

I've looked at rotary encoders, but even those with no detents have about 24 pulses per rotation. It sounds like you have been looking at encoders intended for user input controls. Optical Rotary Encoders for machinery applications are readily available with up to several thousand pulses per revolution. Prices vary depending on if the wheel is glass or ...


2

Try a spirit level (also called bubble level) app on your phone or tablet. It can get to sub-degree accuracy, but it is relative and not absolute accuracy. If that is good enough for you, then you could buy a modern accurate accelerometer and average the values. It might even be possible to have more than one accelerometer for more accuracy. Good quality (...


2

I was wondering if there is a way to change the program immediately after turning the rotary encoder, instead of having to wait for the program to complete. There is, but it's not completely trivial. The reason it is not trivial is each animation is implemented as a function that takes a while to execute. For doing what you ask, each of these animations ...


2

This is a quite high-level question, so I will answer in general terms, skipping the fine details. You can ask here more specific questions once you are implementing the stuff. Yours is a 10-bit absolute encoder. It can tell you its shaft position with a resolution of 1/1024 revolution. It's way overkill for your application. I suggest you connect only the ...


2

Add Serial.begin(115200); in setup() and use Serial.println() or Serial.print() to print to Serial. In Arduino IDE open Serial Monitor and set baud rate to 115200. EDIT: The question turned out to be about Serial Plotter. Maximilian Gerhardt answered in a comment : If you want multiple data streams (graphs) you need to seperate them by a comma: Serial....


2

It's just SPI. SPI.begin(); SPI.setDataMode(SPI_MODE2); // ... uint8_t h = SPI.transfer(0x00); uint8_t l = SPI.transfer(0x00); delayMicroseconds(20); // Indicate EOT uint16_t v = (h << 8) | l; v >>= 4; // for 12 bit resolution, 5 for 11 bit, 6 for 10 bit etc. For 8 bit resolution or less you only need one SPI.transfer, and subtract 8 from the ...


2

In your onReset() function you have an assignment instead of a comparison: if (i = 1) // This is always true { encoder0Position = 0; } i = 1 should be i == 1


2

First of all, I would question the usefulness of debouncing a rotary encoder. I would expect to have contact bounce only when the contact is transitioning from HIGH to LOW or vice-versa. At this transition point, both the HIGH and LOW readings are in a sense correct. A non-debounced reading should then display a rotary angle that briefly changes back and ...


Only top voted, non community-wiki answers of a minimum length are eligible