Encoder pulses

Hi, I have put this program together to read the pulses from wheel encoders, it works fine but the count goes from 0 to 255 then starts again at 0 (not a problem) it will count down from 255 to 0 then starts again at 255 down. what I would like is for the count to go from zero to 255 when the wheels are turned forward and then back to zero when reversed, then from zero to -255 and back to zero in the other direction. I just can't think of the code I need if it is possible; any help would be appreciated.

//============================= Reads the pulses up and down ======================
#include <PinChangeInt.h>
#define L_EncA 12 // Left Encoder PhaseA Pin
#define L_EncB 11 // Left Encoder PhaseB Pin
#define R_EncA 7 // Right Encoder PhaseA Pin
#define R_EncB 6 // Right Encoder PhaseA Pin
#define TOTAL_PINS 19 // use all pins on arduino
#define Encoder_Print;

volatile uint16_t latest_interrupted_pin;
volatile uint16_t interrupt_count[TOTAL_PINS] = {0};
volatile uint16_t interrupt_count1[TOTAL_PINS] = {0}; // possible arduino pins

volatile uint8_t L_Enc_A_Pos = 0;// counter for Left encoder
volatile uint8_t R_Enc_A_Pos = 0;// counter for Right encoder
uint8_t i;
uint8_t j;

void setup() {//======================================
Serial.begin(115200); // debug
pinMode(L_EncA, INPUT_PULLUP);
digitalWrite(L_EncA, HIGH);
attachPinChangeInterrupt(L_EncA, quicfunc0, RISING); // attach interrupt to left encoder on rising edge
pinMode(R_EncA, INPUT_PULLUP);
digitalWrite(R_EncA, HIGH);
attachPinChangeInterrupt(R_EncA, quicfunc1, RISING);
}//======================================================
void quicfunc0() {
if (digitalRead(L_EncA) == digitalRead(L_EncB)) { //true (1) if L_EncA is equal to L_EncB. If not equal is false (0).(x = y == z;)
L_Enc_A_Pos++;
} else {
L_Enc_A_Pos--;
}
#ifdef Encoder_Print;
Serial.print ("L_Enc");
Serial.print (" ");
Serial.println (L_Enc_A_Pos, DEC); // debug
#endif
}
//=======================================================
void quicfunc1() {
if (digitalRead(R_EncA) == digitalRead(R_EncB)) {
R_Enc_A_Pos--;
} else {
R_Enc_A_Pos++;
}
#ifdef Encoder_Print;
Serial.print (" ");
Serial.print ("R_Enc");
Serial.print (" ");
Serial.println (R_Enc_A_Pos, DEC);
#endif

}//=======================================================
void loop() {
uint8_t count;
uint8_t count1;

delay(1);
for (i = 0; i < TOTAL_PINS; i++) { //Less than TotalPins
if (interrupt_count != 0) { // look at all the interrupted pins
count = interrupt_count*; // store its count since the last iteration*
interrupt_count*= 0; // and reset it to 0*
* }*
* }*
* for (j = 0; j < TOTAL_PINS; j++) {
if (interrupt_count1[j] != 0) {
count1 = interrupt_count1[j];
interrupt_count1[j] = 0;
_ }
}
}*_

Sure looks like you are using byte for the counter instead of integers.

Paul

Please use code tags ("</>" button) when posting code.

Would +/-127 work for you instead? Use int8_t

Bytes are integers. They're 8 bit unsigned integers, uint8_t.

Bytes are not ints. Ints are 16 bit signed integers, int16_t.

If you're happy with counting from 0 to 255, that's when you use bytes and never see a negative value.

If you change the counter(s) to int you may even see when the encoder turns backwards as negative values.

There's integers and floats. Byte is not floating point.

Thanks the reason is, I want to put the readings on a gauge, +/- 127 I have tried but i want it to center at zero. I'll try changing the counters and see what happens.

Changed the counters to int and YES I get negative counts, great thanks for your help.

Type char is signed 8 bit integer that can store -128 to 127. If you try to add 1 to 127 you will get -128.

If you can keep within -127 to 127 then char instead of int instead of byte will work and save some RAM.