encoder --measureing degrees

/* this program is write from my observatory. im using the arduino 2560 mega with two incremental 600 pulse encoder thats is connect a gear to increase the pulse count from 600 to 24000. i got the altitude encoder working. the azimuth encoder is count from 0 to 359 or 359 to 0 and the where my problem start. there is more to this program that i didnt need help at this point. */

enc_1A = 21;
enc_1B = 20;
enc_2A = 18;
enc_2B = 19;

volatile int lastEncoded1 = 0;
volatile long encoderValue1 = 0;
volatile int lastEncoded2 = 0;
volatile long encoderValue2 = 0;
int azm;
int Alt;

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
pinMode(enc_1A, INPUT_PULLUP);
pinMode(enc_1B, INPUT_PULLUP);
pinMode(enc_2A, INPUT_PULLUP);
pinMode(enc_2B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(enc_1A), Encoder1, CHANGE);
attachInterrupt(digitalPinToInterrupt(enc_1B), Encoder1, CHANGE);
attachInterrupt(digitalPinToInterrupt(enc_2A), Encoder2, CHANGE);
attachInterrupt(digitalPinToInterrupt(enc_2B), Encoder2, CHANGE);

// cos_phi = cos((((latHH * 3600) + (latMM * 60) + latSS) / 3600.0) * pi / 180.0);
//sin_phi = sin((((latHH * 3600) + (latMM * 60) + latSS) / 3600.0) * pi / 180.0);

// TSL = poleAR_HH * 3600 + poleAR_MM * 60 + poleAR_SS + poleH_HH * 3600 + poleH_MM * 60 + poleH_SS;
//while (TSL >= 86400) TSL = TSL - 86400;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
/*
t_ciclo = millis();
if (t_ciclo_acumulado >= seg_sideral) {
TSL++;
t_ciclo_acumulado = t_ciclo_acumulado - seg_sideral;
if (TSL >= 86400) {
TSL = TSL - 86400;
}
}
*/
read_sensors();
/*AZ_to_EQ();

if (Serial.available() > 0) communication();

t_ciclo = millis() - t_ciclo;
t_ciclo_acumulado = t_ciclo_acumulado + t_ciclo;
*/
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void communication()
{
/*
int i = 0;
input[i++] = Serial.read();
delay(5);
while ((input[i++] = Serial.read()) != ‘#’) {
delay(5);
}
input = ‘\0’;

  • if (input[1] == ‘:’ && input[2] == ‘G’ && input[3] == ‘R’ && input[4] == ‘#’) {*
  • Serial.print(txAR);*
  • }*
  • if (input[1] == ‘:’ && input[2] == ‘G’ && input[3] == ‘D’ && input[4] == ‘#’) {*
  • Serial.print(txDEC);*
    _ }/_
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------------------
    void read_sensors() {
    // encoder pulse is 24000and66.6667 pulses per degree
    alt = encoderValue1 / 66.6667; //this work
    _/
    this part will count from 0 to 359 clockwise but if count clockwise it goes -1 to - 359. when it reaches 359 it keep counting 360, 361, 362, ect_
    this is when im missing something or doing it wrong*/
    if encoderValue2 =0 || encoderValue2 = 24000{ // count from 0 to 359 clockwise
  • azm = encoderValue2 / 66.6667;*
    }
    *if encoderValue2 <= -1{ counts from 359 to 0 counter clockwies *
  • azm = 359*
  • azm = encoderValue2 / 66.6667;*
    }
    if encoderValue2 >= 24001{ //when the count reach 359 then reset 0 zero and starts count from 0 to 359
  • azm = 0*
  • azm = encoderValue2 / 66.6667;*
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------------------
    void Encoder1() {
  • int encoded1 = (digitalRead(enc_1A) << 1) | digitalRead(enc_1B);*
  • int sum = (lastEncoded1 << 2) | encoded1;*
  • if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue1 ++;*
  • if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue1 --;*
  • lastEncoded1 = encoded1;*
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------------------
    void Encoder2() {
  • int encoded2 = (digitalRead(enc_2A) << 1) | digitalRead(enc_2B);*
  • int sum = (lastEncoded2 << 2) | encoded2;*
  • if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue2 ++;*
  • if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue2 --;*
  • lastEncoded2 = encoded2;*
    }

Use code tags, your code got garbled (see the italics?)

One thing I see:

if encoderValue2 =0 || encoderValue2 = 24000{ // count from 0 to 359 clockwise

Should be:

if encoderValue2 == 0 || encoderValue2 == 24000{ // count from 0 to 359 clockwise

= vs ==.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

edgemoron:
One thing I see:

if encoderValue2 =0 || encoderValue2 = 24000{ // count from 0 to 359 clockwise

Should be:

if encoderValue2 == 0 || encoderValue2 == 24000{ // count from 0 to 359 clockwise

= vs ==.

Neither of those will work since if expressions are enclosed in parentheses:

if (encoderValue2 == 0 || encoderValue2 == 24000)
{ // count from 0 to 359 clockwise[/code] = vs ==

There were MANY invalid characters and typographical errors in the code you posted. It was not even close to being compilable. Your functionality problem was due to logic errors. I have fixed everything for you:

/* this program is write from my observatory. im using the arduino 2560 mega with two incremental 600 pulse encoder thats is connect a gear to increase the pulse count from 600 to 24000. i got the altitude encoder working. the azimuth encoder is count from 0 to 359 or 359 to 0 and the where my problem start. there is more to this program that i  didnt need help at this point. */

const byte enc_1A = 21;
const byte enc_1B = 20;
const byte enc_2A = 18;
const byte enc_2B = 19;

volatile int lastEncoded1 = 0;
volatile long encoderValue1 = 0;
volatile int lastEncoded2 = 0;
volatile long encoderValue2 = 0;
int azm;
int Alt;

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  pinMode(enc_1A, INPUT_PULLUP);
  pinMode(enc_1B, INPUT_PULLUP);
  pinMode(enc_2A, INPUT_PULLUP);
  pinMode(enc_2B, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(enc_1A), Encoder1, CHANGE);
  attachInterrupt(digitalPinToInterrupt(enc_1B), Encoder1, CHANGE);
  attachInterrupt(digitalPinToInterrupt(enc_2A), Encoder2, CHANGE);
  attachInterrupt(digitalPinToInterrupt(enc_2B), Encoder2, CHANGE);

  // cos_phi = cos((((latHH * 3600) + (latMM * 60) + latSS) / 3600.0) * pi / 180.0);
  //sin_phi = sin((((latHH * 3600) + (latMM * 60) + latSS) / 3600.0) * pi / 180.0);

  // TSL = poleAR_HH * 3600 + poleAR_MM * 60 + poleAR_SS + poleH_HH * 3600 + poleH_MM * 60 + poleH_SS;
  //while (TSL >= 86400) TSL = TSL - 86400;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
  /*
      t_ciclo = millis();
      if (t_ciclo_acumulado >= seg_sideral) {
       TSL++;
       t_ciclo_acumulado = t_ciclo_acumulado - seg_sideral;
       if (TSL >= 86400) {
         TSL = TSL - 86400;
       }
     }
  */
  read_sensors();
  /*AZ_to_EQ();

     if (Serial.available() > 0) communication();

     t_ciclo = millis() - t_ciclo;
     t_ciclo_acumulado = t_ciclo_acumulado + t_ciclo;
  */
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void communication()
{
  /*  
    int i = 0;
     input[i++] = Serial.read();
     delay(5);
     while ((input[i++] = Serial.read()) != '#') {
       delay(5);
     }
     input[i] = '\0';

     if (input[1] == ':' && input[2] == 'G' && input[3] == 'R' && input[4] == '#') {
       Serial.print(txAR);
     }

     if (input[1] == ':' && input[2] == 'G' && input[3] == 'D' && input[4] == '#') {
       Serial.print(txDEC);
     }*/
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void read_sensors() {
  // encoder pulse is 24000 and 66.6667 pulses per degree
  Alt = encoderValue1 / 66.6667; //this work


  /* this part will count from 0 to 359 clockwise but if count clockwise it goes -1 to - 359. when it reaches 359 it keep  counting 360, 361, 362, ect
    this is when im missing something or doing it wrong*/
  azm = encoderValue2 / 66.6667;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void Encoder1() {
  int encoded1 = (digitalRead(enc_1A) << 1) | digitalRead(enc_1B);
  int sum = (lastEncoded1 << 2) | encoded1;
  if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
    encoderValue1++;
    if (encoderValue1 >= 24000)
      encoderValue1 -= 24000;
  }
  if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
    encoderValue1 --;
    if (encoderValue1 < 0)
      encoderValue1 += 24000;
  }
  lastEncoded1 = encoded1;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
void Encoder2() {
  int encoded2 = (digitalRead(enc_2A) << 1) | digitalRead(enc_2B);
  int sum = (lastEncoded2 << 2) | encoded2;
  if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
    encoderValue2 ++;
    if (encoderValue2 >= 24000)
      encoderValue2 -= 24000;
  }
  if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
    encoderValue2 --;
    if (encoderValue2 < 0)
      encoderValue2 += 24000;
  }
  lastEncoded2 = encoded2;
}