General > General Technical Chat
Schmitt Buffer/Trigger with Encoder and buttons - Inverting...
<< < (5/8) > >>
MarkF:

--- Quote from: elcrni on March 18, 2021, 01:59:35 pm ---ok, i took this the other way around, haha. First did the encoder but now hang at timing, still not sure how to pull this off.
my current code now goes as follows:


--- Code: ---void setup() {

  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
 
  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoderA, CHANGE);      // NO.  NO. NO.  DELETE.
  // encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, doEncoderB, CHANGE);      // NO.  NO. NO.  DELETE.

}

--- End code ---

Many thanks,
Alek

--- End quote ---

Sorry.
Now you get to re-write it.

To start, you DO NOT do interrupt-on-change.
Remove all code related to interrupt-on-change.

Your interrupt ISR() (which is missing) is called at a fixed rate (I suggest 500Hz).
Inside the ISR(), it would be best the have both encoder lines on the same Mega port and to read the Port to get both bits simultaneously. 
Single read of the port instead of two pin reads.  (i.e. read the port --> then mask the two bits --> decode gray code --> increment/decrement turn count.)
 
To get started, view the video and setup the ISR() routine.  The interrupt and setup in the video (near the end of thee video) is for an UNO and needs to be slightly modified for the Mega. 
Download the Mega datasheet and refer to section 17.
Then, test the code to insure the ISR() is called at 500Hz (or the rate you decide upon).
nctnico:

--- Quote from: elcrni on March 17, 2021, 09:41:41 am ---Many thanks for all your help guys! Lots of helpful tips.

As for the encoder, i have always and exclusively used it on interrupt pins, which was ok for most of the projects but now i have a radio signal encoder and analyzer, and once i activate encoder and start turning the knob, i get radio signal decoding errors, i would guess this is due to the fact that interrupt is stopping other processes to execute encoder input. So, i would gladly try the other way suggested by "Buriedcode"... but not sure how the code would look like, will search for examples.

--- End quote ---
Yeah, you just learned never to use interrupts on GPIO pins the hard way. Never ever use interrupts on GPIO pins because you can't predict when the interrupts will happen and thus the timing of your software is no longer predictable. There have been lengthy threads on this forum about handling a rotary encoder in software.

https://www.eevblog.com/forum/microcontrollers/using-a-rotary-encoder/
MarkF:
Also, remove void doEncoderA() and void doEncoderB() functions.

They will be replaced by an ISR(TIMER1_CMPA_vect) function.
This ISR() function will both read the encoder pins and the encoder switch (if it has one) and then setting a global turn count variable and a global switch depressed/released flag.

But as I said a few times now, you need to implement a timer interrupt by configuring the timer and enabling its interrupt in the setup() function and writing the ISR() function to read the encoder pins.
elcrni:
DOH!

Oh well, back tp the drawing board, i have a feeling this will be a long weekend.
Trying to find a close example of a code to go buy, a bit out of my skill level to start from scratch, but maybe a time to learn something new.


Thanks guys!
Alek
MarkF:

--- Quote from: elcrni on March 18, 2021, 09:50:24 pm ---DOH!

Oh well, back tp the drawing board, i have a feeling this will be a long weekend.
Trying to find a close example of a code to go buy, a bit out of my skill level to start from scratch, but maybe a time to learn something new.


Thanks guys!
Alek

--- End quote ---

I gave you some pseudo code.  It's 80% of the code.
Just fill in the functions from the example video with small changes related to the Mega. 
(i.e. the register names for Timer1 are slightly different and the register bits may be different)
Plus, you will need to calculate new times to get 500Hz.

You're making a mountain out of a mole hill.
Navigation
Message Index
Next page
Previous page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod