1

My question is related to my previous question. These encoder programs outputs two line per pule/tick.

Here is the part of the code I want to use:

void loop()
{
 // Read the status of the inputs
 debouncerA.update();
 debouncerB.update();
 int8_t EncVariation = 0;
 if (debouncerA.rose())
 { // if input A changed from low to high, it was CW if B is high too
 EncVariation = (debouncerB.read()) ? 1 : -1;
 }
 else if (debouncerA.fell())
 { // if input A changed from high to low, it was CW if B is low too
 EncVariation = (debouncerB.read()) ? -1 : 1;
 }
 else if (debouncerB.rose())
 { // if input B changed from low to high, it was CCW if A is high too
 EncVariation = (debouncerA.read()) ? -1 : 1;
 }
 else if (debouncerB.fell())
 { // if input B changed from high to low, it was CCW if B is low too
 EncVariation = (debouncerA.read()) ? 1 : -1;
 }
//
 if (EncVariation != 0) {
 encoder0Pos = encoder0Pos + EncVariation;
 int degs = (encoder0Pos * 6) % 360 ;
 if(degs<0){ degs = 360 + degs; }
 Serial.println (degs);
 }
}

For three inputs/ticks this outputs like:

6

12

18

24

30

36

But I only need the second ones like:

12

24

36

How can I do that? (I need it to send the relevant data do a PC.)

I spend so long time on it couldnt find any solution.

asked Jan 26, 2017 at 13:38
2
  • This line looks like its a mistake if(degrees<0){ deg = 360 + degs; } is it correct? You do the calculation of degrees and then start using degs. Commented Jan 26, 2017 at 13:51
  • was a mistype now i corrected Commented Jan 26, 2017 at 13:53

2 Answers 2

1

IF and only IF you want exactly what you have asked for which is alternate lines not to be output then this is a very crude and quick solution.

Outside of loop() add this declaration:

bool skipNextLine = true; // true skips the odd lines, false skips even lines

And change the end of your code like this:

 if (EncVariation != 0) {
 encoder0Pos = encoder0Pos + EncVariation;
 int degrees = (encoder0Pos * 6) % 360 ;
 if(degrees<0){ deg = 360 + degs; }
 if (!skipNextLine)
 { // This will skip alternate lines
 Serial.println (deg);
 skipNextLine = true;
 }
 else
 {
 skipNextLine = false;
 }
 }
Mark Smith
2,1811 gold badge11 silver badges14 bronze badges
answered Jan 26, 2017 at 13:50
4
  • Just tried it didnt work. It doesnt output anything Commented Jan 26, 2017 at 13:57
  • Can it be there maybe another Serial.println (deg); needed before the last if? Commented Jan 26, 2017 at 14:06
  • should skipNextLine be false at some place? Commented Jan 26, 2017 at 14:34
  • Blush Whoops. Thanks Mark for editing it. Commented Jan 26, 2017 at 14:53
0

Use a static variable to count loop execution and only prohibit the very first of every 3 counts.

answered Jan 26, 2017 at 14:03
1
  • I tried to use a variable and odd number selection but couldnt achive. Commented Jan 26, 2017 at 14:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.