Pages: [1]   Go Down
Author Topic: Large code making Rotary encoder not working properly (anymore)  (Read 734 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi folks,

does anyone got a unresponsive encoder due to large code?

My device:

https://picasaweb.google.com/109759819469296361624/Multisupply

is a fully controlled LM338T based constant voltage/constant current as charger/discharger for batteries. Thats why so many relays

I am using:
- Arduino like Mini pro 328 based
- 16x4 LCD with backpack
- Keyes Encoder with 0,1uF debounce capacitors (attached at A0/A1)
- Allegro 2,5A Hall current sensor (already working)
- 24v x 1.5A power supply
- AD5290 (in implementation phase - not show);

Libraries:
* Wire + LCDI2C
* myenc (http://www.pjrc.com/teensy/td_libs_Encoder.html - using std configurations)
* eeprom (for saving and retrieving parameters in future)

The Problem: when uploaded my full code, it got not correctly responsive. When uploaded a simple light code, it works fine.

« Last Edit: November 18, 2013, 06:36:57 am by luloian » Logged

Helsingborg, Sweden
Offline Offline
Sr. Member
****
Karma: 24
Posts: 499
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you assuming that everybody has a facebook account??
Logged

Helsingborg, Sweden
Offline Offline
Sr. Member
****
Karma: 24
Posts: 499
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not interested in pictures, more in code to see if its improvable. Hence my comment
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my code is near 18k, because of the many functions of the characteristyc of the power supply.

I will upload the code as it is when I get home, but i think the problem isnt about the code itself, i guess. But, is regarding much code placed in memory making the uController loosing precision on detecting the encoder.

The library used in the reading the encoder says it uses interrupt, but it seems not.

Did you see sometimes this behaviour? Large code making slowness of rotary encoding?


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will place 2 codes, one it´s the main code, and another one, the one I tested to see if the encoder was missing debounce.
And it´s not. The simple code, read and show on the display works perfectly.
Logged

Helsingborg, Sweden
Offline Offline
Sr. Member
****
Karma: 24
Posts: 499
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A encoder sends out a number of pulses proportional to the rotating angle. The frequency of these pulses depends on
  • The rotating speed
  • the number of pulses per rotation

When the scancycle time of your program (~the time it takes for the loop() function to complete one iteration) exceeds the time between two pulses you will miss pulses thus causing erratic behavior (unless you use interrupts). Since you do not provide any code or any information about the encoder or its rotating speed its impossible to help
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you are perfect in your analisys, but the library is intended to use interrupts (http://www.pjrc.com/teensy/td_libs_Encoder.html).

Even in the void loop { }  that is the most simple part of the code

it´s just a switch case with 5 options. an lcd.clear / print  with  a If then to confirm.... and SO and only in this moment it changes void modules...., even in the loop, it gets lost.

Sorry about not having the code.

Oh, in the library (http://www.pjrc.com/teensy/td_libs_Encoder.html) I coudnt enable optimized mode.
I think I should place the encoder on the "int" channels, pin 2 and 3.... and not in A0 and A1, dont you think?

At home I will load the same code on my nano in the solderlessboard to check this, but place the encoder on 2 and 3 pins.

Logged

Helsingborg, Sweden
Offline Offline
Sr. Member
****
Karma: 24
Posts: 499
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I know about the library. If you have your encoder on pins A0 and A1 you are NOT using interrupts. Not to detect encoder pulses at least.
This is from pjrc website
Quote
Low Performance Polling Mode
 If neither pin has interrupt capability, Encoder can still work. The signals are only checked during each use of the read() function. As long as your program continues to call read() quickly enough, you will get accurate results. Low resolution rotary encoders used for dials or knobs turned only by human fingers are good candidates for low performance polling mode. High resolution encoders attached to motors usually require interrupts!

Since this is obviously the case it is VERY important that you write non-blocking code or you WILL loose pulses

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That might be the key. Changing the pins.

Fortunately, 2 and 3 might be managed at my board with not much design impact and difficulty;

you think the code below (a remembering of mine from the loop code....) might introduce so much latency that the encoder will slow down as that point become unresponsive??

void loop{
menuselector = myenc.read();
switch case  (menuselector)

          Case1: lcd.clear(); lcd.print(" Option 1 bla bla bla"); if (key pressed) { constant voltage();} ;
                       break;
          Case2: lcd.clear(); lcd.print("Option 2  bla bla bla"); if (key pressed) { constant voltage();} ;          
                       break;
          Case3: lcd.clear(); lcd.print(" Option 3 bla bla bla"); if (key pressed ) { constant voltage();} ;
                       break;
}

note: key pressed -> (A3 ==0) it´s the push bottom in the encoder

The strange is that having  more void/code blocks, seems to slow down the microcontroller till it cannot handle the non interrupt pins.....

my main code have 5 or more void blocks... and will get bigger.... more void blocks also.

tody

void setup
void loop
void constantv
void constanti
void setup
void longbeep
void shortbeep

as far as I remember.

Tonight (here is almost lunch) will post the full code, also. But before, will made a test on pin2 / 3.. as we spoke before.
Thanks.
« Last Edit: November 18, 2013, 08:18:43 am by luloian » Logged

Helsingborg, Sweden
Offline Offline
Sr. Member
****
Karma: 24
Posts: 499
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your code and i'll have a look
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the code is quite buggy regarding data conversion for reading amps and volts, display set cursors, etc. But i will post it....  smiley-red
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nilton, it s working!!!   smiley-cool  smiley I tested the code on my nano like clone (she is my betatester) and it is working like a charm!!!
I thought that the encoder library internally made arrangements to the pins I´ve using to be Iinterrupted, but it´s not correct. Have to use pin 2 and 3., or there is some way to A0/A1 get interrupted also?
If not... Will have to proceed with the modifications on the main PCB!!!

I will put the code later, because there was an error:
    
The following error or errors occurred while posting this message:
The message exceeds the maximum allowed length (9500 characters)

I will refine the code and place it in a few days.]


Thank you.;....!
Logged

Helsingborg, Sweden
Offline Offline
Sr. Member
****
Karma: 24
Posts: 499
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There is no way i know of to get a interrupt on the A0 and A1 pins.
There are a number of things you can do:
  • Check for blocking code like delays or while() statements that wait for some input and rewrite them to non blocking
  • Try to call the read() function of the encoder more often, maybe from other places in your code.

Logged

Pages: [1]   Go Up
Jump to: