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;
_ }
}
}*_