Hi all,
I’m trying to create a simple count counter, or so i thought it would be.
I have an encoder, which has 2 digital sensors, with each sensor connecting to a different digital pin, lets say pins 11 & 12 for the purpose of this discussion.
So lets say for the purposes of this example, each time a sensor is coverer it returns a value of 1 and when it’s uncovered it returns a value of 0
As far as i can see the encoder only has 3 transitions of state.
State 1
(SensorA=1 - SensorB=0)
State 2
(SensorA=1 - SensorB=1)
State 3
(SensorA=0 - SensorB=1)
I thought thatit would be simple to create a counter with a simple IF statement, but it’s not proving that easy.
i thought something like this would work, but it doesn’t
if (SensorA == 1 & SensorB == 0){i++;}
if (SensorA == 0 & SensorB == 1){i--}
I’ve tried resetting the sensors before and after and still no joy. I’ve attached my code below.
#include <avr/io.h>
#include <avr/interrupt.h>
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Wire.h>
#include <inttypes.h>
// quadrature encoder
#include <QuadratureEncoder.h>
#define encLtA 10
#define encLtB 0
#define encRtA 13
#define encRtB 0
QuadratureEncoder encoder = QuadratureEncoder(encLtA, encLtB, encRtA, encRtB);
int32_t encLt, encRt;
int i=0;
void setup()
{
encoder.init();
Serial.begin(9600);
lcd.begin(20, 4);
}
void loop()
{
int enc_count;
int tot_count = 0;
int enc_lt, enc_rt, enc;
if (encLt == 1 & encRt == 1)
{ encLt == 0;
encRt == 0;}
if (encLt == 1 & encRt == 0){i++;}
if (encLt == 0 & encRt == 1){i--;}
/* reset encoder */
enc_lt = 0;
enc_rt = 0;
encLt = 0;
encRt = 0;
enc = encoder.readLt();
enc_lt += abs(enc); // read a single step encoder
encLt += enc_lt;
enc = encoder.readRt();
enc_rt += abs(enc); // read a single step encoder
encRt += enc_rt;
tot_count += enc_lt;
tot_count += enc_rt;
tot_count += abs(enc);
lcd.setCursor(0,0);
lcd.print(tot_count, DEC);
lcd.print(" ");
lcd.print(enc_count, DEC);
lcd.print(" ");
lcd.print(encLt, DEC);
lcd.print(" ");
lcd.print(encRt, DEC);
lcd.print(" ");
lcd.print(i);
delay (50);
}
Rich.